Android入门教程(二十四)------之Gallery + ImageSwitcher

   本文来自 http://blog.csdn.net/hellogv/ ,引用必须注明出处!

       上次讲了如何使用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. }  

你可能感兴趣的:(ImageSwitcher)