Android 中的缩略图的加载
来源:互联网 发布:冷冰网络歌手 编辑:程序博客网 时间:2024/04/29 23:05
//Android 中的缩略图加载
/* 1、使用inJustDecodeBounds,读Bitmap的长和宽 2、根据bitmap的长宽和目标缩略图的长和宽。计算出inSampleSize的大小 3、使用inSampleSize,载入一个大一点的缩略图A 4、使用createScaseBitmap,将缩略图A,生成我们需要的缩略图B 5、回收缩略图A*//* Notice createScaseBitmap如果原图和目标缩略图大小一致,那么不会生成一个新的Bitmap直接返回bitmap,因此,回收的时候,要判断缩略图A是否就是缩略图B,如果说是的话,不要回收*///代码:
public class BitmapUtils{ //计算inSampleSize的大小 private static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){ final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if(height>reqHeight||width>reqWidth){ final int halfHeight = height/2; final int halfWidth = width/2; while((halfHeight/inSampleSize)>reqHeight&&(halfWidth/inSampleSize)>reqWidth){ inSampleSize*=2; } } return inSampleSize; } //创建新的Bitmap 图片对象 private static Bitmap createScaleBitmap(Bitmap src,int dstWidth.int dstHeight){ Bitmap dst=Bitmap.createScaleBitmap(src,dstWidth,dstHeight,false); if(src!=dst){ //如果没有缩放,那么不回收 src.recycle(); //释放Bitmap的native像素数组 } return dst; } //从Resources中加载图片 public static Bitmap decodeSampleBitmapFromResource(Resource res,int resId,int reqWidth,int reqHeight){ final BitmapFactory.Options options= new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res,resId,options); //读取图片长宽 options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);//计算inSampleSize options.inJustDecodeBounds = false; Bitmap src = BitmapFactory.decodeResource(res,resId,options); //载入一个稍大的缩略图 return createScaleBitmap(src,reqWidht,reqHeight); //进一步得到目标大小的缩略图 } //从sd卡上加载图片 public static Bitmap decodeSampleBitmapFromFd(String pathName,int reqWidth,int reqHeight){ final BitmapFactory.Options options= new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathName,options); //获取图片的长宽 options.inSampleSize = calculateSampleSize(options,reqWidth,reqHeight); options.inJustDecodeBounds= false; Bitmap src = BitmapFactory.decodeFile(pathName,options); return createScaleBitmap(src,reqWidth,reqHeight); } }
0 0
- Android 中的缩略图的加载
- Android中的缩略图加载-不浪费一点多余的内存
- Android中的缩略图加载-不浪费一点多余的内存
- Android中的缩略图加载-不浪费一点多余的内存
- Android中的缩略图加载-不浪费一点多余的内存
- Android中的缩略图加载-不浪费一点多余的内存
- android 视频的缩略图 缓存机制和 异步加载缩略图
- Android 加载大图的缩略图显示
- Android加载缩略图的几种方法
- Android中的缩略图制作
- Android中的缩略图
- android 异步加载图片缩略图
- android 异步加载图片缩略图
- android 异步加载图片缩略图
- android 异步加载图片缩略图
- android中的缩略图工具类
- Android 图片如何高效加载与缓存 (4.5) —— 缩略图生成的优化
- LruCache缓存girdview 加载的缩略图
- android socket 编程 值得注意的地方
- PMP是什么
- assert 未定义的引用
- bzoj4407 于神之怒加强版 莫比乌斯函数
- Hadoop之父祝贺黄色小象十岁生日快乐
- Android 中的缩略图的加载
- IO库
- BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)
- Maven在Eclipse中的实用小技巧
- hdu2588 GCD(欧拉函数)
- Javascript模块等高级知识
- 最小表示法
- OpenProcess()函数
- Ceph部署