您的位置:首页 > 其它

高效加载较大的 Bitmaps

2015-10-30 19:42 447 查看
欢迎大家访问我的博客http://blog.csdn.net/mikejaps专注于android
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: