android ImageSwitcher 切换图片

ImageSwitcher可切换显示图片,实现类似windows图片查看器,上一张,下一张的功能,直接上代码:

  
  
  
  
  1. public class ActivityMain extends Activity implements ViewSwitcher.ViewFactory{ 
  2.  
  3.     private ImageSwitcher switcher; 
  4.     private Button forward; 
  5.     private Button next; 
  6.     //图片索引 
  7.     private int index = 0
  8.     //显示的图片资源 
  9.     private List<Drawable> list = new ArrayList<Drawable>(); 
  10.  
  11.     /** 
  12.      * Called when the activity is first created. 
  13.      */ 
  14.     @Override 
  15.     public void onCreate(Bundle savedInstanceState) { 
  16.         super.onCreate(savedInstanceState); 
  17.         setContentView(R.layout.main); 
  18.  
  19.         forward = (Button) findViewById(R.id.forward); 
  20.         next = (Button) findViewById(R.id.next); 
  21.  
  22.         switcher = (ImageSwitcher) findViewById(R.id.image); 
  23.  
  24.         if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 
  25.             String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/test"
  26.             File folder = new File(path); 
  27.             for(File file : folder.listFiles()){ 
  28.                 list.add(Drawable.createFromPath(file.getAbsolutePath())); 
  29.             } 
  30.         } 
  31.        //必须设置switcher的ViewFactory 
  32.         switcher.setFactory(this); 
  33.         if(list.size() > 0){ 
  34.             switcher.setImageDrawable(list.get(0)); 
  35.         } 
  36.  
  37.        //上一张 
  38.         forward.setOnClickListener(new View.OnClickListener(){ 
  39.  
  40.             @Override 
  41.             public void onClick(View view) { 
  42.                 index -- ; 
  43.                 if(index < 0) { 
  44.                   index = list.size() - 1
  45.                 } 
  46.                 switcher.setImageDrawable(list.get(index)); 
  47.             } 
  48.         }); 
  49.         //下一张 
  50.         next.setOnClickListener(new View.OnClickListener(){ 
  51.  
  52.             @Override 
  53.             public void onClick(View view) { 
  54.                 index ++ ; 
  55.                 if(index >= list.size()) { 
  56.                   index = 0
  57.                 } 
  58.                 switcher.setImageDrawable(list.get(index)); 
  59.             } 
  60.         }); 
  61.     } 
  62.     //用于显示图片 
  63.     @Override 
  64.     public View makeView() { 
  65.         return new ImageView(this); 
  66.     }
  67. }

布局文件:

 

  
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
  3.             android:orientation="vertical" 
  4.             android:layout_width="fill_parent" 
  5.             android:layout_height="fill_parent"> 
  6.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  7.                   android:orientation="vertical" 
  8.                   android:layout_width="fill_parent" 
  9.                   android:layout_height="fill_parent" 
  10.             > 
  11.         <ImageSwitcher 
  12.                 android:id="@+id/image" 
  13.                 android:layout_width="fill_parent" 
  14.                 android:layout_height="wrap_content" 
  15.                 /> 
  16.         <Button 
  17.                 android:id="@+id/forward" 
  18.                 android:layout_width="fill_parent" 
  19.                 android:layout_height="wrap_content" 
  20.                 android:text="forward" 
  21.                 /> 
  22.         <Button 
  23.                 android:id="@+id/next" 
  24.                 android:layout_width="fill_parent" 
  25.                 android:layout_height="wrap_content" 
  26.                 android:text="next" 
  27.                 /> 
  28.  
  29.     </LinearLayout> 
  30. </ScrollView> 

 

你可能感兴趣的:(android,移动开发,ImageSwitcher,休闲,切换图片)