高效加载较大的 Bitmaps
2015-10-30 19:42
447 查看
欢迎大家访问我的博客http://blog.csdn.net/mikejaps,专注于android
ios app 开发
今天学习bitmap的加载,下面是代码
有个要注意的 地方: 图片经过处理后的实际大小与前面设定的大小有一点点差别,如设定的尺寸是100*100,但实际 得到的图片大小 可能为 103*103
ios app 开发
今天学习bitmap的加载,下面是代码
</pre><pre class="java" name="code">BitmapFactory.Options options = new BitmapFactory.Options(); //拿到设置参数的options options.inJustDecodeBounds = true; //先设置inJustDecodeBuonds为true,此时不会返回bitmap对象,只会得到宽,高,outMimeType BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType;
//就算要压缩的比例
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } } return inSampleSize; } public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); }
有个要注意的 地方: 图片经过处理后的实际大小与前面设定的大小有一点点差别,如设定的尺寸是100*100,但实际 得到的图片大小 可能为 103*103
相关文章推荐
- Java最大堆排序
- dede织梦5.7的安全防护设置
- 夺命雷公狗jquery---55---Ajax的高级实现,模拟发送get请求
- js截取中间字符串
- 机房收费系统——详细设计说明书
- 函数指针
- J - Lines
- 【数论】线性筛素数,线性筛欧拉函数,求前N个数的约数个数
- opencv solvePnP
- 使用MediaRecorder实现录像的功能
- 【Unity3d】3d角色换装实现原理及步骤
- iOS(3)多线程编程技术(Thread Cocoa opreations GCD(Grand Central Dispatch ))
- 2015南阳CCPC A - Secrete Master Plan A.
- XXX 不是当前用户的有效责任,请联系您的系统管理员
- Intent用法实例
- 机房收费系统——概要设计说明书
- 数据结构—栈
- 奇怪的NoClassDefFoundError,一个手机能运行,换另一个不能运行7
- Maven构造版本号的方法解决浏览器缓存问题
- 解决关于移动端zepto的tap点透bug(英文ghost click)