Android开发之--读取文件夹下图片生成略缩图并点击显示大图

来源:互联网 发布:java中do while的用法 编辑:程序博客网 时间:2024/05/17 04:11

这是一个简单的Demo,目的是:读取文件夹下图片生成略缩图并点击显示大图。

先新建一个工程,创建一个ThumbnailsWindows的类,继承LinearLayout。代码如下:

[java] view plaincopy
  1. <span style="font-size:16px;"><span style="font-size: 16px; ">package org.winplus.thum.view;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.InputStream;  
  7. import java.util.ArrayList;  
  8. import java.util.Collections;  
  9. import java.util.Iterator;  
  10. import java.util.Map;  
  11. import java.util.TreeMap;  
  12.   
  13. import android.content.Context;  
  14. import android.graphics.Bitmap;  
  15. import android.graphics.BitmapFactory;  
  16. import android.util.AttributeSet;  
  17. import android.view.LayoutInflater;  
  18. import android.view.MotionEvent;  
  19. import android.view.View;  
  20. import android.widget.ImageButton;  
  21. import android.widget.ImageView;  
  22. import android.widget.LinearLayout;  
  23. import cn.embel.thum.R;  
  24.   
  25. public class ThumbnailsWindows extends LinearLayout {  
  26.   
  27.     private static final String TAG = "ThumbnailsWindows";  
  28.     private Context mContext;  
  29.     private static ArrayList<String> paths = new ArrayList<String>();  
  30.       
  31.     private ImageView imageView;  
  32.       
  33.     public ThumbnailsWindows(Context context) {  
  34.         super(context);  
  35.         mContext = context;  
  36.         setupViews();  
  37.     }  
  38.   
  39.     public ThumbnailsWindows(Context context, AttributeSet attrs) {  
  40.         super(context, attrs);  
  41.         mContext = context;  
  42.         setupViews();  
  43.     }  
  44.   
  45.     public void setupViews() {  
  46.           
  47.         /** 
  48.          * 显示大图时需要使用,当然可以直接在此类中定义!这样还好控制一些~到时候再改吧,赶这过年呢 
  49.          */  
  50.         final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext());  
  51.         View v = mLayoutInflater.inflate(R.layout.original_photo, null);  
  52.         imageView =  (ImageView) v.findViewById(R.id.original);  
  53.           
  54.         Map<String,Bitmap> maps = new TreeMap<String, Bitmap>();  
  55.         try {  
  56.             maps = buildThum();  
  57.         } catch (FileNotFoundException e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.           
  61.         Iterator<String> it = maps.keySet().iterator();  
  62.         int i = 0;  
  63.         while (it.hasNext()) {  
  64.             String path = (String) it.next();    
  65.             Bitmap bm = maps.get(path);    
  66.               
  67.             ImageButton image = new ImageButton(mContext);   
  68.             image.setImageBitmap(bm);  
  69.             image.setId(i++);  
  70.             addView(image);  
  71.             image.setOnTouchListener(listener);  
  72.         }  
  73.           
  74.         addView(v);  
  75.     }  
  76.       
  77.     /** 
  78.      * 定义按钮控件的Touch事件 
  79.      */  
  80.     OnTouchListener listener = new OnTouchListener() {  
  81.         @Override  
  82.         public boolean onTouch(View v, MotionEvent event) {  
  83.             /** 
  84.              * 控件按下的时候显示当前略缩图的大图 
  85.              */  
  86.             if(event.getAction() == MotionEvent.ACTION_DOWN){  
  87.                 String path = paths.get(v.getId());  
  88.                 InputStream inputStream = null;  
  89.                 try {  
  90.                     inputStream = new FileInputStream(path);  
  91.                 } catch (FileNotFoundException e) {  
  92.                     e.printStackTrace();  
  93.                 }  
  94.                 Bitmap bitmap = BitmapFactory.decodeStream(inputStream);  
  95.                 imageView.setImageBitmap(bitmap);  
  96.             }  
  97.             return false;  
  98.         }  
  99.     };  
  100.       
  101.     /** 
  102.      * 获取图片地址列表 
  103.      * @param file 
  104.      * @return 
  105.      */  
  106.     private static ArrayList<String> imagePath(File file) {  
  107.         ArrayList<String> list = new ArrayList<String>();  
  108.   
  109.         File[] files = file.listFiles();  
  110.         for (File f : files) {  
  111.             list.add(f.getAbsolutePath());  
  112.         }  
  113.         Collections.sort(list);  
  114.         return list;  
  115.     }  
  116.   
  117.     /** 
  118.      * 读取sdcard文件夹中的图片,并生成略缩图 
  119.      * @return 
  120.      * @throws FileNotFoundException 
  121.      */  
  122.     private Map<String,Bitmap> buildThum() throws FileNotFoundException {  
  123.         File baseFile = new File("/mnt/sdcard/tflash/image/");  
  124.         // 使用TreeMap,排序问题就不需要纠结了  
  125.         Map<String,Bitmap> maps = new TreeMap<String, Bitmap>();  
  126.         if (baseFile != null && baseFile.exists()) {  
  127.             paths = imagePath(baseFile);  
  128.   
  129.             if (!paths.isEmpty()) {  
  130.                 for (int i = 0; i < paths.size(); i++) {  
  131.                      BitmapFactory.Options options = new BitmapFactory.Options();  
  132.                      options.inJustDecodeBounds = true// 设置了此属性一定要记得将值设置为false  
  133.                      Bitmap bitmap =BitmapFactory.decodeFile(paths.get(i),options);  
  134.                      options.inJustDecodeBounds = false;  
  135.                      int be = options.outHeight/40;  
  136.                      if (be <= 0) {  
  137.                          be = 10;  
  138.                      }  
  139.                      options.inSampleSize = be;  
  140.                      bitmap = BitmapFactory.decodeFile(paths.get(i),options);  
  141.                      maps.put(paths.get(i), bitmap);  
  142.                 }  
  143.             }  
  144.         }  
  145.   
  146.         return maps;  
  147.     }  
  148. }  
  149. </span></span>  

修改mail.xml文件
[html] view plaincopy
  1. <span style="font-size: 16px; "><?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.   
  7.     <org.winplus.thum.view.ThumbnailsWindows  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent" />  
  10.       
  11. </LinearLayout></span>  

本Demo还有Bug,稍后在修改吧。原文地址:http://blog.csdn.net/tangcheng_ok

原创粉丝点击