Android之ImageSwitcher

一. 简单示例

src

 

[java] view plain copy
 
  1. public class AndroidUIActivity extends Activity {  
  2.   
  3.     // 当前显示的图片索引  
  4.     private int index;  
  5.   
  6.     // 图片数组  
  7.     private int[] images = { R.drawable.image1, R.drawable.image2,  
  8.             R.drawable.image3, R.drawable.image4, R.drawable.image5 };  
  9.   
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.   
  14.         super.onCreate(savedInstanceState);  
  15.   
  16.         // 全屏设置  
  17.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  18.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  19.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  20.   
  21.         setContentView(R.layout.main);  
  22.   
  23.         // 得到ImageSwitcher对象  
  24.         final ImageSwitcher is = (ImageSwitcher) findViewById(R.id.imageSwitcher1);  
  25.   
  26.         // 实现并设置工厂内部接口的makeView方法,用来显示视图。  
  27.         is.setFactory(new ViewFactory() {  
  28.   
  29.             public View makeView() {  
  30.                 return new ImageView(AndroidUIActivity.this);  
  31.             }  
  32.         });  
  33.   
  34.         // 设置图片来源  
  35.         is.setImageResource(images[index]);  
  36.   
  37.         // 设置点击监听器  
  38.         is.setOnClickListener(new View.OnClickListener() {  
  39.   
  40.             public void onClick(View v) {  
  41.                 // 点击会切换图片  
  42.                 index++;  
  43.                 if (index >= images.length) {  
  44.                     index = 0;  
  45.                 }  
  46.                 is.setImageResource(images[index]);  
  47.             }  
  48.         });  
  49.   
  50.         // 设置切入动画  
  51.         is.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(),  
  52.                 android.R.anim.slide_in_left));  
  53.         // 设置切出动画  
  54.         is.setOutAnimation(AnimationUtils.loadAnimation(  
  55.                 getApplicationContext(), android.R.anim.slide_out_right));  
  56.   
  57.     }  
  58. }  

 

 

main.xml

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageSwitcher  
  8.         android:id="@+id/imageSwitcher1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content" >  
  11.     </ImageSwitcher>  
  12.   
  13. </LinearLayout>  

 

 

二. 运行结果

启动


 

点击后切换过程

posted @ 2016-11-28 16:38  天涯海角路  阅读(92)  评论(0)    收藏  举报