Android高级组件ImageSwitcher图像切换器使用方法详解

所属分类: 软件编程 / Android 阅读数: 49
收藏 0 赞 0 分享

图像切换器(ImageSwitcher),用于实现类似于Windows操作系统的“Windows照片查看器”中的上一张、下一张切换图片的功能。在使用ImageSwitcher时,必须实现ViewSwitcher.ViewFactory接口,并通过makeView()方法来创建用于显示图片的ImageView。makeView()方法将返回一个显示图片的ImageView。在使用图像切换器时,还有一个方法非常重要,那就是setImageResource方法,该方法用于指定要在ImageSwitcher中显示的图片资源。

下面通过一个实例来说明图像切换器的用法。

res/layout/main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="horizontal" 
 android:id="@+id/layout" 
 android:gravity="center"> 
 <Button 
  android:text="上一张" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:id="@+id/button1"/> 
 <ImageSwitcher 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_gravity="center" 
  android:id="@+id/imageSwitcher1"/> 
 <Button 
  android:text="下一张" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:id="@+id/button2"/> 
</LinearLayout>

MainActivity:

package com.example.test; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.ViewSwitcher.ViewFactory; 
 
 
public class MainActivity extends Activity{ 
 //声明并初始化一个保存要显示图像id的数组 
 private int[] imageId=new int[]{R.drawable.img01,R.drawable.img02,R.drawable.img03, 
   R.drawable.img04,R.drawable.img05,R.drawable.img06,R.drawable.img07, 
   R.drawable.img08}; 
 private int index=0;//当前显示图像的索引 
 private ImageSwitcher imageSwitcher;//声明一个图像切换器对象 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher1);//获取图像切换器 
  //设置动画效果 
  imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//设置淡入动画 
  imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//设置淡出动画 
  imageSwitcher.setFactory(new ViewFactory() {//设置View工厂 
    
   @Override 
   public View makeView() { 
    ImageView imageView=null; 
    imageView=new ImageView(MainActivity.this);//实例化一个ImageView类的对象 
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//设置保持纵横比居中缩放图像 
    imageView.setLayoutParams(new ImageSwitcher.LayoutParams( 
      LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    return imageView; 
   } 
  }); 
  imageSwitcher.setImageResource(imageId[index]);//显示默认的图片 
   
  //“上一张”和“下一张”按钮的控制 
  Button up=(Button)findViewById(R.id.button1); 
  Button down=(Button)findViewById(R.id.button2); 
  up.setOnClickListener(new OnClickListener() { 
    
   @Override 
   public void onClick(View arg0) { 
    if(index>0){ 
     index--;//图片索引后退一个 
    }else{ 
     index=imageId.length-1;//图片达到最前面一张之后,循环至最后一张 
    } 
    imageSwitcher.setImageResource(imageId[index]);//显示当前图片 
   } 
  }); 
  down.setOnClickListener(new OnClickListener() { 
      
     @Override 
     public void onClick(View arg0) { 
      if(index<imageId.length-1){ 
       index++;//图片索引前进一个 
      }else{ 
       index=0;//图片达到最后面一张之后,循环至第一张 
      } 
      imageSwitcher.setImageResource(imageId[index]);//显示当前图片 
     } 
    }); 
 } 
} 

效果如图所示:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

android开发之Json文件的读写的示例代码

这篇文章主要介绍了android开发之Json文件的读写的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android7.0指纹服务FingerprintService实例介绍

这篇文章主要介绍了Android7.0指纹服务FingerprintService介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Android JNI处理图片实现黑白滤镜的方法

这篇文章主要介绍了Android JNI处理图片实现黑白滤镜的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android引入OpenCV的示例

本篇文章主要介绍了Android引入OpenCV的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android Zip解压缩工具类分享

这篇文章主要为大家详细介绍了Android Zip解压缩工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Interval

这篇文章主要为大家详细介绍了Android RxJava创建操作符Interval的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

5分钟快速实现Android爆炸破碎酷炫动画特效的示例

本篇文章主要介绍了5分钟快速实现Android爆炸破碎酷炫动效的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android 指纹功能实例代码

本文通过一个demo给大家介绍了android指纹功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现倒计时CountDownTimer使用详解

这篇文章主要为大家详细介绍了Android实现倒计时CountDownTimer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Timer的方法

这篇文章主要为大家详细介绍了Android RxJava创建操作符Timer的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多