Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android入門第十三篇之Gallery + ImageSwitcher

Android入門第十三篇之Gallery + ImageSwitcher

編輯:Android開發實例

上次講了如何使用Gallery控件,這次就講Gallery 與ImageSwitcher的結合使用,本文實現一個簡單的浏覽圖片的功能。先貼出程序運行截圖:

除了Gallery可以拖拉切換圖片,我在ImageSwitcher控件加入了setOnTouchListener事件實現,使得ImageSwitcher也可以在拖拉中切換圖片。本例子依然使用JAVA的反射機制來自動讀取資源中的圖片。

 

main.xml的源碼如下:     

 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent">   
  5.       
  6.     <ImageSwitcher android:id="@+id/switcher" 
  7.         android:layout_width="match_parent" android:layout_height="match_parent"/> 
  8.       
  9.     <Gallery android:id="@+id/gallery" 
  10.         android:background="#55000000" 
  11.         android:layout_width="match_parent" 
  12.         android:layout_alignParentBottom="true" 
  13.         android:layout_alignParentLeft="true" 
  14.           
  15.         android:gravity="center_vertical" 
  16.         android:spacing="16dp" android:layout_height="100dp"/> 
  17. </RelativeLayout> 
  18.    

程序的源碼如下:

 

 
  1. package com.testImageView;     
  2. import java.lang.reflect.Field;     
  3. import java.util.ArrayList;     
  4. import android.app.Activity;     
  5. import android.content.Context;     
  6. import android.os.Bundle;     
  7. import android.view.MotionEvent;     
  8. import android.view.View;     
  9. import android.view.View.OnTouchListener;     
  10. import android.view.ViewGroup;     
  11. import android.view.animation.AnimationUtils;     
  12. import android.widget.AdapterView;     
  13. import android.widget.BaseAdapter;     
  14. import android.widget.Gallery;     
  15. import android.widget.ImageSwitcher;     
  16. import android.widget.ImageView;     
  17. import android.widget.AdapterView.OnItemSelectedListener;     
  18. import android.widget.Gallery.LayoutParams;     
  19. import android.widget.ViewSwitcher.ViewFactory;     
  20. public class testImageView extends Activity implements ViewFactory {     
  21.     private ImageSwitcher is;     
  22.     private Gallery gallery;     
  23.     private int downX,upX;     
  24.     private ArrayList<Integer> imgList=new ArrayList<Integer>();//圖像ID     
  25.          
  26.     @Override    
  27.     protected void onCreate(Bundle savedInstanceState) {     
  28.         // TODO Auto-generated method stub     
  29.         super.onCreate(savedInstanceState);     
  30.         setContentView(R.layout.main);     
  31.         //用反射機制來獲取資源中的圖片ID     
  32.         Field[] fields = R.drawable.class.getDeclaredFields();     
  33.         for (Field field : fields)     
  34.         {     
  35.             if (!"icon".equals(field.getName()))//除了icon之外的圖片     
  36.             {        
  37.                 int index = 0;     
  38.                 try {     
  39.                     index = field.getInt(R.drawable.class);     
  40.                 } catch (IllegalArgumentException e) {     
  41.                     // TODO Auto-generated catch block     
  42.                     e.printStackTrace();     
  43.                 } catch (IllegalAccessException e) {     
  44.                     // TODO Auto-generated catch block     
  45.                     e.printStackTrace();     
  46.                 }     
  47.                 //保存圖片ID     
  48.                 imgList.add(index);     
  49.             }     
  50.         }     
  51.              
  52.         //設置ImageSwitcher控件     
  53.         is = (ImageSwitcher) findViewById(R.id.switcher);     
  54.         is.setFactory(this);     
  55.         is.setInAnimation(AnimationUtils.loadAnimation(this,     
  56.                 android.R.anim.fade_in));     
  57.         is.setOutAnimation(AnimationUtils.loadAnimation(this,     
  58.                 android.R.anim.fade_out));     
  59.         is.setOnTouchListener(new OnTouchListener(){     
  60.             /*    
  61.              * 在ImageSwitcher控件上滑動可以切換圖片    
  62.              */    
  63.             @Override    
  64.             public boolean onTouch(View v, MotionEvent event) {     
  65.                 if(event.getAction()==MotionEvent.ACTION_DOWN)     
  66.                 {     
  67.                     downX=(int) event.getX();//取得按下時的坐標     
  68.                     return true;     
  69.                 }     
  70.                 else if(event.getAction()==MotionEvent.ACTION_UP)     
  71.                 {     
  72.                     upX=(int) event.getX();//取得松開時的坐標     
  73.                     int index=0;     
  74.                     if(upX-downX>100)//從左拖到右,即看前一張     
  75.                     {     
  76.                         //如果是第一,則去到尾部     
  77.                         if(gallery.getSelectedItemPosition()==0)     
  78.                            index=gallery.getCount()-1;     
  79.                         else    
  80.                             index=gallery.getSelectedItemPosition()-1;     
  81.                     }     
  82.                     else if(downX-upX>100)//從右拖到左,即看後一張     
  83.                     {     
  84.                         //如果是最後,則去到第一     
  85.                         if(gallery.getSelectedItemPosition()==(gallery.getCount()-1))     
  86.                             index=0;     
  87.                         else    
  88.                             index=gallery.getSelectedItemPosition()+1;     
  89.                     }     
  90.                     //改變gallery圖片所選,自動觸發ImageSwitcher的setOnItemSelectedListener     
  91.                     gallery.setSelection(index, true);     
  92.                     return true;     
  93.                 }     
  94.                 return false;     
  95.             }     
  96.                  
  97.         });     
  98.              
  99.         //設置gallery控件     
  100.         gallery = (Gallery) findViewById(R.id.gallery);     
  101.         gallery.setAdapter(new ImageAdapter(this));     
  102.         gallery.setOnItemSelectedListener(new OnItemSelectedListener(){     
  103.             @Override    
  104.             public void onItemSelected(AdapterView<?> arg0, View arg1,     
  105.                     int position, long arg3) {     
  106.                 is.setImageResource(imgList.get(position));     
  107.             }     
  108.             @Override    
  109.             public void onNothingSelected(AdapterView<?> arg0) {     
  110.                 // TODO Auto-generated method stub     
  111.             }     
  112.                  
  113.         });     
  114.     }     
  115.     //設置ImgaeSwitcher     
  116.     @Override    
  117.     public View makeView() {     
  118.         ImageView i = new ImageView(this);     
  119.         i.setBackgroundColor(0xFF000000);     
  120.         i.setScaleType(ImageView.ScaleType.CENTER);//居中     
  121.         i.setLayoutParams(new ImageSwitcher.LayoutParams(//自適應圖片大小     
  122.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));     
  123.         return i;     
  124.     }     
  125.     public class ImageAdapter extends BaseAdapter {     
  126.         public ImageAdapter(Context c) {     
  127.             mContext = c;     
  128.         }     
  129.         public int getCount() {     
  130.             return imgList.size();     
  131.         }     
  132.         public Object getItem(int position) {     
  133.             return position;     
  134.         }     
  135.         public long getItemId(int position) {     
  136.             return position;     
  137.         }     
  138.         public View getView(int position, View convertView, ViewGroup parent) {     
  139.             ImageView i = new ImageView(mContext);     
  140.             i.setImageResource(imgList.get(position));     
  141.             i.setAdjustViewBounds(true);     
  142.             i.setLayoutParams(new Gallery.LayoutParams(     
  143.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));     
  144.             return i;     
  145.         }     
  146.         private Context mContext;     
  147.     }     
  148.     
  149. }    
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved