现在的位置: 首页 > 综合 > 正文

Android中ImageSwitcher结合Gallery展示SD卡中的资源图片

2017年12月13日 ⁄ 综合 ⁄ 共 8801字 ⁄ 字号 评论关闭

             本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Drawable目录下的资源图片,这样调用系统的API比较容易实现,但我们在开发项目过程中,但有些图片还不能完全确定下来,例如需要展示相机拍照的图片,SDCard中某个目录下的资源图片等功能。其实系统中也提供相应的API给我们应用去实现该功能,下面就用异于API Demo中例子方式展示下如何实现该功能。

【1】我们先看下该例子代码的结构图:

下面就直接上各个文件的代码了,不在这里详细解释了,最后会看到实现的效果图的..呵呵

【2】res/layout/main.xml 文件源码:

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"   
  6.     android:background="#55000000" >  
  7.       
  8.     <TextView   
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:gravity="center"  
  12.         android:text="Welcome to Andy.Chen's Blog!"  
  13.         android:textSize="20sp"/>  
  14.       
  15.     <ImageSwitcher  
  16.         android:id="@+id/switcher"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="350dip"  
  19.         android:layout_alignParentLeft="true"  
  20.         android:layout_alignParentRight="true" />  
  21.   
  22.     <Gallery  
  23.         android:id="@+id/mygallery"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="80dp"  
  26.         android:layout_alignParentBottom="true"  
  27.         android:layout_alignParentLeft="true"  
  28.         android:gravity="center_vertical"  
  29.         android:spacing="16dp" />  
  30.       
  31. </LinearLayout>  

【3】res/values/attrs.xml 文件源码:

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.       
  4.   <declare-styleable name="Gallery">   
  5.     <attr name="android:galleryItemBackground" />   
  6.   </declare-styleable>   
  7.       
  8. </resources>  

【4】ImageSwitcherAndGalleryDemoActivity.java 源码:(这个类的源码比较多,希望大家耐心看)

[html] view
plain
copy

  1. package com.andyidea.imagedemo;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Context;  
  9. import android.content.res.TypedArray;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.net.Uri;  
  13. import android.os.Bundle;  
  14. import android.os.Environment;  
  15. import android.util.Log;  
  16. import android.view.View;  
  17. import android.view.View.OnClickListener;  
  18. import android.view.ViewGroup;  
  19. import android.view.animation.AnimationUtils;  
  20. import android.widget.AdapterView;  
  21. import android.widget.AdapterView.OnItemClickListener;  
  22. import android.widget.AdapterView.OnItemSelectedListener;  
  23. import android.widget.BaseAdapter;  
  24. import android.widget.Gallery;  
  25. import android.widget.Gallery.LayoutParams;  
  26. import android.widget.ImageSwitcher;  
  27. import android.widget.ImageView;  
  28. import android.widget.Toast;  
  29. import android.widget.ViewSwitcher.ViewFactory;  
  30.   
  31. /**  
  32.  * ImageSwitcher和Gallery如何展示SD卡中的资源图片  
  33.  * @author Andy.Chen  
  34.  * @email:Chenjunjun.ZJ@gmail.com  
  35.  */  
  36. public class ImageSwitcherAndGalleryDemoActivity extends Activity   
  37.              implements OnItemSelectedListener,ViewFactory{  
  38.       
  39.     private List<String> imagePathList;   
  40.     private String[] list;   
  41.     private ImageSwitcher mSwitcher;  
  42.     private Gallery mGallery;  
  43.   
  44.       
  45.     /** Called when the activity is first created. */  
  46.     @Override  
  47.     public void onCreate(Bundle savedInstanceState) {  
  48.         super.onCreate(savedInstanceState);  
  49.         setContentView(R.layout.main);  
  50.           
  51.         imagePathList=getImagePathFromSD();   
  52.         list = imagePathList.toArray(new String[imagePathList.size()]);   
  53.       
  54.         /* 设定Switcher */  
  55.         mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);  
  56.         mSwitcher.setFactory(this);  
  57.         /* 设定载入Switcher的模式 */  
  58.         mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
  59.                 android.R.anim.fade_in));  
  60.         /* 设定输出Switcher的模式 */  
  61.         mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,  
  62.                 android.R.anim.fade_out));  
  63.         mSwitcher.setOnClickListener(new OnClickListener() {  
  64.   
  65.             public void onClick(View v) {  
  66.                 Toast.makeText(ImageSwitcherAndGalleryDemoActivity.this, "你点击了ImageSwitch上的图片",  
  67.                         Toast.LENGTH_SHORT).show();  
  68.   
  69.             }  
  70.   
  71.         });  
  72.   
  73.         mGallery = (Gallery) findViewById(R.id.mygallery);  
  74.         /* 新增几ImageAdapter并设定给Gallery对象 */  
  75.         mGallery.setAdapter(new ImageAdapter(this, getImagePathFromSD()));  
  76.   
  77.         mGallery.setOnItemSelectedListener(this);  
  78.   
  79.         /* 设定一个itemclickListener事件 */  
  80.         mGallery.setOnItemClickListener(new OnItemClickListener() {  
  81.             public void onItemClick(AdapterView<?> parent, View v,  
  82.                     int position, long id) {  
  83.                 Toast.makeText(ImageSwitcherAndGalleryDemoActivity.this, "你点击了Gallery上的图片",  
  84.                         Toast.LENGTH_SHORT).show();  
  85.             }  
  86.         });  
  87.   
  88.     }  
  89.       
  90.     /** 从SD卡中获取资源图片的路径 */  
  91.     private List<String> getImagePathFromSD() {  
  92.         /* 设定目前所在路径 */  
  93.         List<String> it = new ArrayList<String>();  
  94.           
  95.         //根据自己的需求读取SDCard中的资源图片的路径  
  96.         String imagePath = Environment.getExternalStorageDirectory().toString()+"/hknational/image";  
  97.           
  98.         File mFile = new File(imagePath);  
  99.         File[] files = mFile.listFiles();  
  100.   
  101.         /* 将所有文件存入ArrayList中 */  
  102.         for (int i = 0; i < files.length; i++) {  
  103.             File file = files[i];  
  104.             if (checkIsImageFile(file.getPath()))  
  105.                 it.add(file.getPath());  
  106.         }  
  107.         return it;  
  108.     }  
  109.   
  110.     /** 判断是否相应的图片格式  */  
  111.     private boolean checkIsImageFile(String fName) {  
  112.         boolean isImageFormat;  
  113.   
  114.         /* 取得扩展名 */  
  115.         String end = fName  
  116.                 .substring(fName.lastIndexOf(".") + 1, fName.length())  
  117.                 .toLowerCase();  
  118.   
  119.         /* 按扩展名的类型决定MimeType */  
  120.         if (end.equals("jpg") || end.equals("gif") || end.equals("png")  
  121.                 || end.equals("jpeg") || end.equals("bmp")) {  
  122.             isImageFormat = true;  
  123.         } else {  
  124.             isImageFormat = false;  
  125.         }  
  126.         return isImageFormat;  
  127.     }  
  128.   
  129.     /* 改写BaseAdapter自定义一ImageAdapter class */  
  130.     public class ImageAdapter extends BaseAdapter {  
  131.         /* 声明变量 */  
  132.         int mGalleryItemBackground;  
  133.         private Context mContext;  
  134.         private List<String> lis;  
  135.   
  136.         /* ImageAdapter的构造符 */  
  137.         public ImageAdapter(Context c, List<String> li) {  
  138.             mContext = c;  
  139.             lis = li;  
  140.             /*  
  141.              * 使用res/values/attrs.xml中的<declare-styleable>定义 的Gallery属性.  
  142.              */  
  143.             TypedArray mTypeArray = obtainStyledAttributes(R.styleable.Gallery);  
  144.             /* 取得Gallery属性的Index id */  
  145.             mGalleryItemBackground = mTypeArray.getResourceId(  
  146.                     R.styleable.Gallery_android_galleryItemBackground, 0);  
  147.             /* 让对象的styleable属性能够反复使用 */  
  148.             mTypeArray.recycle();  
  149.         }  
  150.   
  151.         /* 重写的方法getCount,传回图片数目 */  
  152.         public int getCount() {  
  153.             return lis.size();  
  154.         }  
  155.   
  156.         /* 重写的方法getItem,传回position */  
  157.         public Object getItem(int position) {  
  158.             return position;  
  159.         }  
  160.   
  161.         /* 重写的方法getItemId,传并position */  
  162.         public long getItemId(int position) {  
  163.             return position;  
  164.         }  
  165.   
  166.         /* 重写方法getView,传并几View对象 */  
  167.         public View getView(int position, View convertView, ViewGroup parent) {  
  168.             /* 产生ImageView对象 */  
  169.             ImageView i = new ImageView(mContext);  
  170.             /* 设定图片给imageView对象 */  
  171.             Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString());  
  172.             i.setImageBitmap(bm);  
  173.             /* 重新设定图片的宽高 */  
  174.             i.setScaleType(ImageView.ScaleType.FIT_XY);  
  175.             /* 重新设定Layout的宽高 */  
  176.             i.setLayoutParams(new Gallery.LayoutParams(136, 88));  
  177.             /* 设定Gallery背景图 */  
  178.             i.setBackgroundResource(mGalleryItemBackground);  
  179.             /* 传回imageView对象 */  
  180.             return i;  
  181.         }  
  182.     }   
  183.   
  184.     @Override  
  185.     public View makeView() {  
  186.         ImageView iv = new ImageView(this);   
  187.         iv.setBackgroundColor(0xFF000000);   
  188.         iv.setScaleType(ImageView.ScaleType.FIT_CENTER);   
  189.         iv.setLayoutParams(new ImageSwitcher.LayoutParams(   
  190.             LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));   
  191.         return iv;   
  192.     }  
  193.   
  194.     @Override  
  195.     public void onItemSelected(AdapterView<?> parent, View view, int position,  
  196.             long id) {  
  197.         // TODO Auto-generated method stub  
  198.         String photoURL = list[position];  
  199.         Log.i("A", String.valueOf(position));  
  200.           
  201.         mSwitcher.setImageURI(Uri.parse(photoURL));  
  202.     }  
  203.   
  204.     @Override  
  205.     public void onNothingSelected(AdapterView<?> parent) {  
  206.         // TODO Auto-generated method stub  
  207.           
  208.     }  
  209. }  

【5】程序运行效果图如下:

        

至此大功告成了!

抱歉!评论已关闭.