Bitmap的高效加载
2016-10-28 12:13
204 查看
在任玉刚大神的书上看到的这个方法。其实Android API上也给了这个类似的代码。用来预防图片OOM的发生
public class BitmapUtil{public BitmapUtil(){}public static Bitmap decodeSampleedBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight){final Options options = new Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(res, resId,options);options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);options.inJustDecodeBounds = false;options.inPreferredConfig = Config.ARGB_8888;options.inDither = true;return BitmapFactory.decodeResource(res, resId, options);}private static int calculateInSampleSize(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高效加载及Android缓存策略
- 高效地加载大Bitmap(位图)
- 高效使用Bitmaps(一) 大Bitmap的加载
- Android_Bitmap的高效加载
- 译:Google官方教程】高效的加载大Bitmap(位图)
- 【Android Training - 09】高效地显示Bitmap图片 [ Lesson 1 - 有效率地加载大尺寸的位图]
- 高效使用Bitmaps(一) 大Bitmap的加载
- 高效显示Bitmap之高效加载较大的 Bitmaps
- Bitmap的高效加载和 Cache
- 高效使用Bitmaps(二) 后台加载Bitmap
- 关于android示例程序(bitmapfun)——高效加载图片的坑爹地方
- android开发之Bitmap的高效加载
- 高效使用Bitmaps(二) 后台加载Bitmap
- Bitmap的高效加载(Android开发艺术探索学习笔记)
- 【Google官方教程】第一课:高效地加载大Bitmap(位图)
- 高效使用Bitmaps(一) 大Bitmap的加载
- Bitmap的高效加载
- Bitmap如何高效加载图片
- Android基础学习总结(十四)——Bitmap应用与高效加载
- 【Google官方教程】第一课:高效地加载大Bitmap(位图)