如何高效加载bitmap
来源:互联网 发布:蔚来汽车发展前景知乎 编辑:程序博客网 时间:2024/06/06 03:13
对于如何高效加载bitmap,就是采用BitmapFactory.Options来加载所需的图片大小。
通过BitmapFactory.Options来对图片进行缩放,主要利用其inSampleSize参数,即采样率。
获取采样率的步骤一般如下:
(1)将BitmapFactory.Options的inJustDecodeBounds参数设置为true并加载图片
(2)获取原始图片的宽高信息,即options的outwidth和outheight
(3)计算出所需采样率的大小,即inSampleSize的值
(4)将BitmapFactory.Options的inJustDecodeBounds参数设置为false并加载图片
相关代码如下
//reqWidth 期望的图片的宽,单位像素
//reqHeight 期望的图片的高,单位像素
public static Bitmap getBitmapFromRes(Resources res,int resId,int reqWidth,int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res,resId,options);
options.inSampleSize = getSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;
BitmapFactory.decodeResource(res,resId,options);
}
public static int getSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight) {
final int width = options.outWidth;
final int height = options.outHeight;
int inSampleSize = 1;
if (width > reqWidth || height > reqHeight) {
final int halfWidth = width / 2;
final int halfHeight = height / 2;
while ((halfWidth / inSampleSize) >= reqWidth && (halfHeight / inSampleSize) >= reqHeight) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
- 如何高效加载bitmap
- 如何高效加载Bitmap
- 如何高效的加载Bitmap
- Bitmap如何高效加载图片
- Android实践:如何高效加载Bitmap
- Bitmap的高效加载
- Bitmap的高效加载
- 高效加载Bitmap
- 高效加载Bitmap
- Bitmap的高效加载
- Bitmap的高效加载
- Bitmap高效加载图片
- Bitmap的高效加载
- 高效的加载Bitmap
- Bitmap 高效加载图片
- Bitmap的高效加载
- Android高效加载Bitmap
- BitMap的高效加载
- linux init进程
- 熊猫烧香之逆向分析下
- 396. Rotate Function
- HDU 5869 Different GCD Subarray Query (数学gcd+树状数组离线查询)
- java 数组的学习
- 如何高效加载bitmap
- poj 2104 K-th number
- php基础之字符串简述
- ASP.NET SignalR with MVC
- 华为OJ——求解立方根
- Android轻松搞定Dialog提示动画效果
- ORACLE用SYS登录报ORA-28009:connection as SYS should be as SYSDBA OR SYSOPER解决方法
- nginx初探
- Activity 进入和进出的动画效果