您的位置:首页 > 其它

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;}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: