Android開發之–讀取文件夾下圖片生成略縮圖並點擊顯示大圖

這是一個簡單的Demo,目的是:讀取文件夾下圖片生成略縮圖並點擊顯示大圖。

先新建一個工程,創建一個ThumbnailsWindows的類,繼承LinearLayout。代碼如下:

[java]
<span style="font-size:16px;"><span style="font-size: 16px; ">package org.winplus.thum.view; 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Iterator; 
import java.util.Map; 
import java.util.TreeMap; 
 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import cn.embel.thum.R; 
 
public class ThumbnailsWindows extends LinearLayout { 
 
    private static final String TAG = "ThumbnailsWindows"; 
    private Context mContext; 
    private static ArrayList<String> paths = new ArrayList<String>(); 
     
    private ImageView imageView; 
     
    public ThumbnailsWindows(Context context) { 
        super(context); 
        mContext = context; 
        setupViews(); 
    } 
 
    public ThumbnailsWindows(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        mContext = context; 
        setupViews(); 
    } 
 
    public void setupViews() { 
         
        /**
         * 顯示大圖時需要使用,當然可以直接在此類中定義!這樣還好控制一些~到時候再改吧,趕這過年呢
         */ 
        final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext()); 
        View v = mLayoutInflater.inflate(R.layout.original_photo, null); 
        imageView =  (ImageView) v.findViewById(R.id.original); 
         
        Map<String,Bitmap> maps = new TreeMap<String, Bitmap>(); 
        try { 
            maps = buildThum(); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
        } 
         
        Iterator<String> it = maps.keySet().iterator(); 
        int i = 0; 
        while (it.hasNext()) { 
            String path = (String) it.next();   
            Bitmap bm = maps.get(path);   
             
            ImageButton image = new ImageButton(mContext);  
            image.setImageBitmap(bm); 
            image.setId(i++); 
            addView(image); 
            image.setOnTouchListener(listener); 
        } 
         
        addView(v); 
    } 
     
    /**
     * 定義按鈕控件的Touch事件
     */ 
    OnTouchListener listener = new OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
            /**
             * 控件按下的時候顯示當前略縮圖的大圖
             */ 
            if(event.getAction() == MotionEvent.ACTION_DOWN){ 
                String path = paths.get(v.getId()); 
                InputStream inputStream = null; 
                try { 
                    inputStream = new FileInputStream(path); 
                } catch (FileNotFoundException e) { 
                    e.printStackTrace(); 
                } 
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
                imageView.setImageBitmap(bitmap); 
            } 
            return false; 
        } 
    }; 
     
    /**
     * 獲取圖片地址列表
     * @param file
     * @return
     */ 
    private static ArrayList<String> imagePath(File file) { 
        ArrayList<String> list = new ArrayList<String>(); 
 
        File[] files = file.listFiles(); 
        for (File f : files) { 
            list.add(f.getAbsolutePath()); 
        } 
        Collections.sort(list); 
        return list; 
    } 
 
    /**
     * 讀取sdcard文件夾中的圖片,並生成略縮圖
     * @return
     * @throws FileNotFoundException
     */ 
    private Map<String,Bitmap> buildThum() throws FileNotFoundException { 
        File baseFile = new File("/mnt/sdcard/tflash/image/"); 
        // 使用TreeMap,排序問題就不需要糾結瞭 
        Map<String,Bitmap> maps = new TreeMap<String, Bitmap>(); 
        if (baseFile != null && baseFile.exists()) { 
            paths = imagePath(baseFile); 
 
            if (!paths.isEmpty()) { 
                for (int i = 0; i < paths.size(); i++) { 
                     BitmapFactory.Options options = new BitmapFactory.Options(); 
                     options.inJustDecodeBounds = true; // 設置瞭此屬性一定要記得將值設置為false 
                     Bitmap bitmap =BitmapFactory.decodeFile(paths.get(i),options); 
                     options.inJustDecodeBounds = false; 
                     int be = options.outHeight/40; 
                     if (be <= 0) { 
                         be = 10; 
                     } 
                     options.inSampleSize = be; 
                     bitmap = BitmapFactory.decodeFile(paths.get(i),options); 
                     maps.put(paths.get(i), bitmap); 
                } 
            } 
        } 
 
        return maps; 
    } 

</span></span> 

修改mail.xml文件
[html]
<span style="font-size: 16px; "><?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <org.winplus.thum.view.ThumbnailsWindows 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 
     
</LinearLayout></span> 

 

本Demo還有Bug,稍後在修改吧,看能否經過修改,改成像Ihone圖片瀏覽器一樣的效果.好瞭,本文就寫到這裡!

源碼下載:http://up.aiwalls.com/2012/0208/20120208093646461.rar

摘自 唐成的技術專欄

發佈留言