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> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章