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

jopen 12年前發布 | 58K 次閱讀 Android Android開發 移動開發

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

先新建一個工程,創建一個ThumbnailsWindows的類,繼承LinearLayout。代碼如下:
 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;
}

} </pre> 修改mail.xml文件

<span style="font-size: 16px; "><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&quot;
    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></pre>0_13267077305hgt.gif</span></span>

本Demo還有Bug,稍后在修改吧,看能否經過修改,改成像Ihone圖片瀏覽器一樣的效果.

源碼下載==》

原創文章,轉載請注明出處:http://blog.csdn.net/tangcheng_ok

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!