Bitmap的高效加载
发布日期:2021-10-07 06:13:02 浏览次数:1 分类:技术文章

本文共 1151 字,大约阅读时间需要 3 分钟。

在任玉刚大神的书上看到的这个方法。其实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;    }}

转载地址:https://blog.csdn.net/mengmengkenanjun/article/details/52955124 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:String 的compareto函数
下一篇:旋转字符串

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月21日 06时20分35秒