Android 中文 API ——ImageSwitcher

利用ImageSwitcher类,通过设置一个ViewFactory工厂,实现其makeView()方法来创建显示图片的View。

方法setImageResource用来显示指定的图片资源。

 

 

1. 将8张图片放入/res/drawable目录下,同时命名为sample_0,sample_1,....sample_7等;

2. 在代码中定义资源id数组;

[java] view plaincopy

  1. static final Integer[] imagelist =   

  2.         {  

  3.             R.drawable.sample_0,  

  4.             R.drawable.sample_1,  

  5.             R.drawable.sample_2,  

  6.             R.drawable.sample_3,  

  7.             R.drawable.sample_4,  

  8.             R.drawable.sample_5,  

  9.             R.drawable.sample_6,  

  10.             R.drawable.sample_7,  

  11.         };  

 

 

Activity.java

[java] view plaincopy

  1. package com.luoye.allview;  

  2.   

  3. import android.app.Activity;  

  4. import android.os.Bundle;  

  5. import android.view.View;  

  6. import android.view.View.OnClickListener;  

  7. import android.widget.Button;  

  8. import android.widget.ImageSwitcher;  

  9. import android.widget.ImageView;  

  10. import android.widget.ViewSwitcher.ViewFactory;  

  11.   

  12. public class SecondActivity extends Activity implements OnClickListener, ViewFactory{  

  13.   

  14.       

  15.     ImageSwitcher imageswitcher;  

  16.     private Button button_pre;  

  17.     private Button button_next;  

  18.       

  19.     static final Integer[] imagelist =   

  20.         {  

  21.             R.drawable.sample_0,  

  22.             R.drawable.sample_1,  

  23.             R.drawable.sample_2,  

  24.             R.drawable.sample_3,  

  25.             R.drawable.sample_4,  

  26.             R.drawable.sample_5,  

  27.             R.drawable.sample_6,  

  28.             R.drawable.sample_7,  

  29.         };  

  30.     private static int index = 0;  

  31.       

  32.       

  33.       

  34.     @Override  

  35.     protected void onCreate(Bundle savedInstanceState) {  

  36.         // TODO Auto-generated method stub  

  37.         super.onCreate(savedInstanceState);  

  38.         setContentView(R.layout.secondactivity);  

  39.           

  40.         imageswitcher = (ImageSwitcher)findViewById(R.id.imageswitch);  

  41.         imageswitcher.setFactory(this);  

  42.         imageswitcher.setImageResource(imagelist[index]);  

  43.           

  44.         button_pre = (Button)findViewById(R.id.button_pre);  

  45.         button_next = (Button)findViewById(R.id.button_next);  

  46.           

  47.         button_pre.setOnClickListener(this);  

  48.         button_next.setOnClickListener(this);  

  49.     }  

  50.   

  51.     @Override  

  52.     public View makeView() {  

  53.         // TODO Auto-generated method stub  

  54.         return new ImageView(this);  

  55.     }  

  56.   

  57.     @Override  

  58.     public void onClick(View v) {                      //实现button的onClick方法  

  59.         // TODO Auto-generated method stub  

  60.           

  61.         switch(v.getId())  

  62.         {  

  63.             case R.id.button_next:  

  64.                 index++;  

  65.                 if(index == imagelist.length)  

  66.                 {  

  67.                     index = 0;  

  68.                 }  

  69.                 imageswitcher.setImageResource(imagelist[index]);  

  70.                 break;  

  71.             case R.id.button_pre:  

  72.                 index--;  

  73.                 if(index < 0)  

  74.                 {  

  75.                     index =  imagelist.length - 1;  

  76.                 }  

  77.                 imageswitcher.setImageResource(imagelist[index]);  

  78.                 break;  

  79.             default:  

  80.                 break;  

  81.         }  

  82.           

  83.     }  

  84.   

  85. }  

布局文件:

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:orientation="vertical" >  

  6.       

  7.     <ImageSwitcher   

  8.         android:id="@+id/imageswitch"  

  9.   android:layout_width="wrap_content"  

  10.   android:layout_height="100dp"  

  11.         ></ImageSwitcher>  

  12.       

  13.     <Button   

  14.         android:id="@+id/button_next"  

  15.         android:layout_width="wrap_content"  

  16.         android:layout_height="wrap_content"  

  17.         android:text="上一张"  

  18.         />  

  19.       

  20.     <Button   

  21.         android:id="@+id/button_pre"  

  22.         android:layout_width="wrap_content"  

  23.         android:layout_height="wrap_content"  

  24.         android:text="下一张"  

  25.         />  

  26.   

  27. </LinearLayout>  



效果:

Android 中文 API ——ImageSwitcher

点击下一张后,实现了切换

Android 中文 API ——ImageSwitcher


转载于:https://my.oschina.net/sunxichao/blog/346155