Android开发之--读取文件夹下图片生成略缩图并点击显示大图
2012-01-16 17:45
232 查看
这是一个简单的Demo,目的是:读取文件夹下图片生成略缩图并点击显示大图。
先新建一个工程,创建一个ThumbnailsWindows的类,继承LinearLayout。代码如下:
修改mail.xml文件
![](https://oscdn.geek-share.com/Uploads/Images/Content/201201/16/63ba9ff146d0344f6a8c96470ecc16c0.gif)
本Demo还有Bug,稍后在修改吧,看能否经过修改,改成像Ihone图片浏览器一样的效果.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/899229cfab2c02d614490485cabb781b.gif)
好了,本文就写到这里,提前预祝各位春节快乐!
源码下载==》
原创文章,转载请注明出处:http://blog.csdn.net/tangcheng_ok
先新建一个工程,创建一个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>
![](https://oscdn.geek-share.com/Uploads/Images/Content/201201/16/63ba9ff146d0344f6a8c96470ecc16c0.gif)
本Demo还有Bug,稍后在修改吧,看能否经过修改,改成像Ihone图片浏览器一样的效果.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/899229cfab2c02d614490485cabb781b.gif)
好了,本文就写到这里,提前预祝各位春节快乐!
源码下载==》
原创文章,转载请注明出处:http://blog.csdn.net/tangcheng_ok
相关文章推荐
- Android开发之--读取文件夹下图片生成略缩图并点击显示大图
- [转载]Android开发之--读取文件夹下图片生成略缩图并点击显示大图
- android TV端如何读取微信网页版二维码显示生成一张图片
- android开发如何用file读出sdcard中含图片的文件夹,并将文件夹显示到listview上????
- Android手机开发:ImageView使用和从内存读取图片显示
- Android点击图片显示大图,长按保存网络图片到本地相册,单击返回。(GestureDetector)(RxJava)(Easypermissions)(AlertView)
- Android开发经验之点击图片判断是否在图片范围之内
- Android 读取sd卡图片并显示
- android如何将生成的图片保存至手机相册并显示出来
- android开发:点击缩略图查看大图
- php自动读取文件夹下所有图片并分页显示
- android 根据SD卡中图片路径读取并显示SD中的图片——源代码
- Android 读取SD卡中相应文件夹下的图片
- Android中高效的显示图片 - 加载大图
- php遍历、读取文件夹中图片并分页显示图片的方法
- IOS开发 两、三个界面相互传值,类似android的onActivityResult(拍照上传、预览图片、点击查看、删除)
- android开发模拟器显示图片大小与真实图片大小不一致问题
- android高仿微信UI点击头像显示大图片效果
- Android手机开发:图片的放大和缩小显示ImageView
- Android开发经验之点击图片判断是否在图片范围之内