由于最近小马要使用下Gallery与ImageSwitch实现照片的预览功能,特此找个时间写了个小DEMO测试了下,完整的哦,直接开始,今天好激动呢,吼吼,先分段讲下,最后贴出源代码,看代码之前先往你的模拟器“/sdcard/”下PUSH几张图片哦,作为工程中使用的图片资源,看代码:

 
  
  1. package com.xiaoma.www; 
  2.  
  3. import android.app.Activity; 
  4. import android.os.Bundle; 
  5.  
  6. import java.io.File; 
  7. import java.util.ArrayList; 
  8. import java.util.List; 
  9.  
  10. import android.app.Activity; 
  11. import android.content.Context; 
  12. import android.content.res.Resources; 
  13. import android.content.res.TypedArray; 
  14. import android.graphics.Bitmap; 
  15. import android.graphics.BitmapFactory; 
  16. import android.net.Uri; 
  17. import android.os.Bundle; 
  18. import android.util.AttributeSet; 
  19. import android.util.Log; 
  20. import android.view.View; 
  21. import android.view.ViewGroup; 
  22. import android.view.WindowManager; 
  23. import android.view.View.OnClickListener; 
  24. import android.view.animation.Animation; 
  25. import android.view.animation.AnimationUtils; 
  26. import android.widget.AdapterView; 
  27. import android.widget.BaseAdapter; 
  28. import android.widget.Gallery; 
  29. import android.widget.ImageSwitcher; 
  30. import android.widget.ImageView; 
  31. import android.widget.Toast; 
  32. import android.widget.ViewSwitcher; 
  33. import android.widget.AdapterView.OnItemClickListener; 
  34. import android.widget.Gallery.LayoutParams; 
  35.  
  36. /**   
  37. * @Title: GalleryAddSDPhotoActivity.java 
  38. * @Package com.xiaoma.www 
  39. * @Description: Gallery与ImageSwitch测试 
  40. * @author MZH 
  41. * @version V2.2 
  42. */ 
  43. public class GalleryAddSDPhotoActivity extends Activity implements 
  44.         AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory { 
  45.  
  46.     private List<String> ImageList; 
  47.     private String[] list; 
  48.     private ImageSwitcher mSwitcher; 
  49.  
  50.     public void onCreate(Bundle savedInstanceState) { 
  51.         super.onCreate(savedInstanceState); 
  52.         //去掉状态栏,全屏显示 
  53.          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
  54.          WindowManager.LayoutParams.FLAG_FULLSCREEN); 
  55.          setContentView(R.layout.main); 
  56.           
  57.          /** 
  58.           * 取得SD卡上的图片文件,并将图片完整路径保存到ImageList中,是完整路径哦 
  59.           */ 
  60.         ImageList = getInSDPhoto(); 
  61.          
  62.         /** 
  63.          * 将取得的路径集合ImageList转换成数组并存入list中 
  64.          * List集合中的toArray()方法经常用在集合与数组转换的,吼吼 
  65.          */ 
  66.         list = ImageList.toArray(new String[ImageList.size()]); 
  67.  
  68.         // 设定Switcher 
  69.         mSwitcher = (ImageSwitcher) findViewById(R.id.switcher); 
  70.         mSwitcher.setFactory(this); 
  71.          
  72.         /** 
  73.          *  设定载入Switcher的模式  
  74.          *  AnimationUtils此类小马我不知道是干吗的,查了源码,用工具,大致意思如果下: 
  75.          *  Defines common utilities for working with animations 
  76.          *  即:定义动画通用的工具类,不懂了一个词一个词的查 
  77.          *   
  78.          *  loadAnimation()此方法见名字就知道是什么意思了 
  79.          *  Loads an {@link Animation} object from a resource 
  80.          *  通过 资源文件载入 
  81.          */ 
  82.         mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this
  83.                 //下面这个参数小马看了下源码,他们把这个int值转化成了十六进制载入?小马不明白为这样一定要 
  84.                 //转换成十六进制啊?看到小马问题的朋友如果知道话请指点一下,小马先谢过 
  85.                 android.R.anim.fade_in)); 
  86.          
  87.         /** 
  88.          * 设定输出Switcher的模式  
  89.          */ 
  90.         mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this
  91.                 android.R.anim.fade_out)); 
  92.         mSwitcher.setOnClickListener(new OnClickListener() { 
  93.  
  94.             public void onClick(View v) { 
  95.                 Toast.makeText(GalleryAddSDPhotoActivity.this,  
  96.                         "点击了ImageSwitch中的图片"
  97.                         Toast.LENGTH_SHORT).show(); 
  98.  
  99.             } 
  100.  
  101.         }); 
  102.  
  103.         Gallery g = (Gallery) findViewById(R.id.mygallery); 
  104.  
  105.         /** 
  106.          *  添加ImageAdapter并设定给Gallery对象 
  107.          */ 
  108.         g.setAdapter(new ImageAdapter(this, getInSDPhoto())); 
  109.          
  110.         /** 
  111.          * 监听器设置 
  112.          */ 
  113.         g.setOnItemSelectedListener(this); 
  114.  
  115.         /* 设定一个itemclickListener事件 */ 
  116.         g.setOnItemClickListener(new OnItemClickListener() { 
  117.             public void onItemClick(AdapterView<?> parent, View v, 
  118.                     int position, long id) { 
  119.                 Toast.makeText(GalleryAddSDPhotoActivity.this
  120.                 //注意下小细节 ,position+1如果不用括号的话,定位就错啦,朋友可以取掉试下 
  121.                         "点击了Gallery画廊中的第 "+(position+1)+"张图片"
  122.                         Toast.LENGTH_SHORT).show(); 
  123.             } 
  124.         }); 
  125.     } 
  126.  
  127.     /** 
  128.      * 获取SD卡中图片文件的方法实现 
  129.      * @return 
  130.      */ 
  131.     private List<String> getInSDPhoto() { 
  132.          
  133.         /** 
  134.          *  设定图片所在路径 
  135.          */ 
  136.         List<String> it = new ArrayList<String>(); 
  137.          
  138.         /** 
  139.          * 此处小马直接把图片push进了SD卡根路径下,如果朋友们要放到其它文件下, 
  140.          * 可先判断下SD卡存在时将路径指定到自己的路径下就可以了,试试吧,吼吼 
  141.          */ 
  142.         File f = new File("/sdcard/"); 
  143.         File[] files = f.listFiles(); 
  144.  
  145.         /** 
  146.          *  将所有文件存入ArrayList中,这个地方存的还是文件路径哦 
  147.          */ 
  148.         for (int i = 0; i < files.length; i++) { 
  149.             File file = files[i]; 
  150.             if (getAllImage(file.getPath())) 
  151.                 it.add(file.getPath()); 
  152.         } 
  153.         return it; 
  154.     } 
  155.      
  156.     /** 
  157.      * 获取所有图片文件的实现 
  158.      * @param fName 
  159.      * @return 
  160.      */ 
  161.     private boolean getAllImage(String fName) { 
  162.         boolean re; 
  163.  
  164.         /* 取得扩展名 */ 
  165.         String end = fName 
  166.                 .substring(fName.lastIndexOf(".") + 1, fName.length()) 
  167.                 .toLowerCase(); 
  168.  
  169.         /* 按扩展名的类型决定MimeType */ 
  170.         if (end.equals("jpg") || end.equals("gif") || end.equals("png"
  171.                 || end.equals("jpeg") || end.equals("bmp")) { 
  172.             re = true
  173.         } else { 
  174.             re = false
  175.         } 
  176.         return re; 
  177.     } 
  178.  
  179.     /** 
  180.      *  改写BaseAdapter自定义一ImageAdapter class 
  181.      * @Title: ImageAdapter.java 
  182.      * @Package com.xiaoma.www 
  183.      * @Description: Gallery 适配器实现 
  184.      * @author MZH 
  185.      * @version V2.2 
  186.      */ 
  187.     //适配器中常用的方法小马就不加注释了 
  188.     public class ImageAdapter extends BaseAdapter { 
  189.         /* 声明变量 */ 
  190.         int mGalleryItemBackground; 
  191.         private Context mContext; 
  192.         private List<String> lis; 
  193.  
  194.         /** 
  195.          * ImageAdapter的构造符  
  196.          * @param c 
  197.          * @param list 
  198.          */ 
  199.         public ImageAdapter(Context context, List<String> list) { 
  200.             mContext = context; 
  201.             lis = list; 
  202.              
  203.             /* 
  204.              * 使用styleable.xml设置Gallery属性 
  205.              * 下面TypedArray类小马也不知道是什么,眼馋跟进去看了下,贴下源码 
  206.              * /** 
  207.              * Container for an array of values that were retrieved with 
  208.              * {@link Resources.Theme#obtainStyledAttributes(AttributeSet,  
  209.              * int[], int, int)} 
  210.              * or {@link Resources#obtainAttributes}.  Be 
  211.              * sure to call {@link #recycle} when done with them. 
  212.              *  
  213.              * The indices used to retrieve values from this structure correspond to 
  214.              * the positions of the attributes given to obtainStyledAttributes. 
  215.              * 看了下,大体意思这句就讲清楚了: Container for an array of values that were retrieved 
  216.              * 是说:存放从数组中读取出的值(而且这些值可以反复使用的哦)的容器 
  217.              */ 
  218.             TypedArray a = obtainStyledAttributes(R.styleable.Gallery); 
  219.             mGalleryItemBackground = a.getResourceId( 
  220.                     R.styleable.Gallery_android_galleryItemBackground, 0); 
  221.              
  222.             //让对象的styleable属性能够反复使用 
  223.             //源码中:Be sure to call {@link #recycle} when done with them. 
  224.             a.recycle(); 
  225.         } 
  226.  
  227.         public int getCount() { 
  228.             return lis.size(); 
  229.         } 
  230.  
  231.         public Object getItem(int position) { 
  232.             return position; 
  233.         } 
  234.  
  235.         public long getItemId(int position) { 
  236.             return position; 
  237.         } 
  238.  
  239.         /* 几定要重写的方法getView,传并几View对象 */ 
  240.         public View getView(int position, View convertView, ViewGroup parent) { 
  241.             ImageView i = new ImageView(mContext); 
  242.             //使用图片工厂载入并设置图片 
  243.             Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString()); 
  244.             i.setImageBitmap(bm); 
  245.              
  246.             /** 
  247.              * 设定ImageView宽高,跟了下FIX_XY,竟然是枚举,好激动,小马好久没看到枚举啦 
  248.              * 试了下ScaleType中除FIT_XY以外的其它选项,效果图分别如果下: 
  249.              */ 
  250.             i.setScaleType(ImageView.ScaleType.FIT_XY); 

贴图小马就一次性贴出来,这样容易看出它们之间的差别:

FIT_XY效果:

FIT_START效果:

FIT_END效果:

FIT_CENTER效果:

CENTER_INSIDE效果:

CENTER_CROP效果:

CENTER效果:

下面来看下半部分代码:

 
  
  1. //重新设定Layout的宽高 
  2.             /** 
  3.              * 这个地方小马有个疑问,我设置Gallery全屏的时候画面会拉伸, 
  4.              * 有什么办法可以不让他拉伸还可以让它全屏啊?指点下小马,谢谢 
  5.              * i.setLayoutParams( new Gallery.LayoutParams( 
  6.              * WindowManager.LayoutParams.MATCH_PARENT, 
  7.              * WindowManager.LayoutParams.MATCH_PARENT)); 
  8.              */ 
  9.             i.setLayoutParams(new Gallery.LayoutParams(13688)); 
  10.              
  11.             // 设定Gallery背景图  
  12.             i.setBackgroundResource(mGalleryItemBackground); 
  13.              
  14.             // 传回imageView对象  
  15.             return i; 
  16.         } 
  17.     } 
  18.  
  19.     public void onItemSelected(AdapterView<?> parent, View view, int position, 
  20.             long id) { 
  21.         String photoURL = list[position]; 
  22.         Log.i("XiaoMa", String.valueOf(position)); 
  23.         mSwitcher.setImageURI(Uri.parse(photoURL)); 
  24.     } 
  25.     public void onNothingSelected(AdapterView<?> parent) { 
  26.     } 
  27.  
  28.      
  29.     /** 
  30.      * 实现ViewSwitcher.ViewFactory接口必须实现的方法 
  31.      */ 
  32.     public View makeView() { 
  33.         ImageView i = new ImageView(this); 
  34.         i.setBackgroundColor(0xFF000000); 
  35.         i.setScaleType(ImageView.ScaleType.FIT_CENTER); 
  36.         i.setLayoutParams(new ImageSwitcher.LayoutParams( 
  37.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
  38.         return i; 
  39.     } 

      为供朋友们学习交流,如果觉得在这儿看代码不太方便的话,可以下载小马上传的附件,里面是所有代码,在这篇文章中如果看到有地方小马有错误的,请直接批评指正,还有小马在中间提的问题,知情人士请指点下小马,先谢谢啦,吼吼。。O_O