设为首页 加入收藏

TOP

Android内存优化之Bitmap最优加载(二)
2015-07-16 12:55:11 来源: 作者: 【 】 浏览:2
Tags:Android 内存 优化 Bitmap 加载
的图片
? ? */
? ? public static Bitmap compressBitmapFromResourse(Context context, int resId, int reqWidth, int reqHeight) {
? ? ? ? final BitmapFactory.Options options = new BitmapFactory.Options();
? ? ? ? /*
? ? ? ? * 第一次解析时,inJustDecodeBounds设置为true,
? ? ? ? * 禁止为bitmap分配内存,虽然bitmap返回值为空,但可以获取图片大小
? ? ? ? */
? ? ? ? options.inJustDecodeBounds = true;
? ? ? ? BitmapFactory.decodeResource(context.getResources(), resId, options);


? ? ? ? final int height = options.outHeight;
? ? ? ? final int width = options.outWidth;
? ? ? ? int inSampleSize = 1;
? ? ? ? if (height > reqHeight || width > reqWidth) {
? ? ? ? ? ? final int heightRatio = Math.round((float) height / (float) reqHeight);
? ? ? ? ? ? final int widthRatio = Math.round((float) width / (float) reqWidth);
? ? ? ? ? ? inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
? ? ? ? }
? ? ? ? options.inSampleSize = inSampleSize;
? ? ? ? // 使用计算得到的inSampleSize值再次解析图片
? ? ? ? options.inJustDecodeBounds = false;
? ? ? ? return BitmapFactory.decodeResource(context.getResources(), resId, options);
? ? }


?


?


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇jQuery 动态标签生成插件 下一篇Android内存优化之Bitmap内存占用..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: