您的位置:首页 > 移动开发 > Android开发

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

2012-01-16 17:45 232 查看
这是一个简单的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 org.winplus.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;
	}
}


修改mail.xml文件
<?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>



本Demo还有Bug,稍后在修改吧,看能否经过修改,改成像Ihone图片浏览器一样的效果.

好了,本文就写到这里,提前预祝各位春节快乐!

源码下载==》

原创文章,转载请注明出处:http://blog.csdn.net/tangcheng_ok
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: