温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ImageSwitcher图像切换器的使用方法

发布时间:2020-10-26 14:38:28 来源:亿速云 阅读:117 作者:Leah 栏目:开发技术

ImageSwitcher图像切换器的使用方法?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

描述

在该实例中,提供一个图片切换器和两个点击按钮,用于切换图片,并用一个TextView显示图片信息。其中,当前图片若为最后一张,点击下一张,则跳转到第一张;同理,第一张图片点击上一张,则显示最后一张图片,循环查看当前图片。

目标效果图如下所示:

ImageSwitcher图像切换器的使用方法

ImageSwitcher图像切换器的使用方法

ImageSwitcher图像切换器的使用方法

页面布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/bg67"
  android:orientation="vertical"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >

  <TextView
    android:id="@+id/show"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:text="我是当前图片的信息~"
    android:textSize="24dp" />


  <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageSwitcher 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/image"
      android:layout_gravity="center"
      android:background="#666666">
    </ImageSwitcher>

    <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:gravity="center">

      <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一张"
        android:layout_marginLeft="20dp"
        android:textSize="24dp"
        android:id="@+id/up" />

      <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一张"
        android:layout_marginLeft="20dp"
        android:textSize="24dp"
        android:id="@+id/down" />

    </LinearLayout>

  </LinearLayout>
</LinearLayout>

事件响应

package com.example.imageswitchdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity
{
  TextView show=null;
  Button up,dowm=null;
  ImageSwitcher image=null;
  private int[] images=new int[]{R.drawable.a001,R.drawable.a002,R.drawable.a003,
                  R.drawable.a004,R.drawable.a005,R.drawable.a006,
                  R.drawable.a007,R.drawable.a008,R.drawable.a009};
  private int index=0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //获取控件
    show=(TextView) findViewById(R.id.show);
    up=(Button) findViewById(R.id.up);
    dowm=(Button) findViewById(R.id.down);
    image=(ImageSwitcher) findViewById(R.id.image);

    //为获取到的控件添加显示效果:淡入动画和淡出动画
    image.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
    image.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

    //为图像切换器设置一个ViewFactory,并重写makeView方法
    image.setFactory(new ViewFactory()
    {

      @Override
      public View makeView()
      {
        //指定视图切换工程
        return new ImageView(MainActivity.this);
      }
    });
    image.setImageResource(images[index]);
    show.setText("一共有"+images.length+"张图片,当前是第"+(index+1)+"张图片");

    //当点击按钮时,图像切换并显示相应的信息
    up.setOnClickListener(new OnClickListener()
    {

      @Override
      public void onClick(View arg0)
      {
        if(index>0)
          index--;
        else
          index=images.length-1;

        image.setImageResource(images[index]);
        show.setText("一共有"+images.length+"张图片,当前是第"+(index+1)+"张图片");
      }
    });

    //同理,当点击按钮时,图像切换并显示相应的信息
    dowm.setOnClickListener(new OnClickListener()
    {
      public void onClick(View arg0)
      {
        if(index<images.length-1)
          index++;
        else
          index=0;

        image.setImageResource(images[index]);
        show.setText("一共有"+images.length+"张图片,当前是第"+(index+1)+"张图片");
      }
    });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI