为了节省内存,不少状况下原图片都要通过缩放处理,根据控件的尺寸来处理成对应尺寸的图片,这时使用BitmapFactory建立Bitmap,不少状况下都会使用下面的代码:java
1
2
3
4
5
6
|
BitmapFactory.Options options =
new
BitmapFactory.Options();
options.inJustDecodeBounds =
true
;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int
imageHeight = options.outHeight;
int
imageWidth = options.outWidth;
String imageType = options.outMimeType;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public
static
int
calculateInSampleSize(
BitmapFactory.Options options,
int
reqWidth,
int
reqHeight){
// Raw height and width of image
finalint height = options.outHeight;
finalint width = options.outWidth;
int
inSampleSize =
1
;
if
(height > reqHeight || width > reqWidth){
finalint halfHeight = height /
2
;
finalint halfWidth = width /
2
;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while
((halfHeight / inSampleSize)> reqHeight
&&(halfWidth / inSampleSize)> reqWidth){
inSampleSize *=
2
;
}
}
return
inSampleSize;
}
|
在decode的时候先设置 options . inJustDecodeBounds = true,获取到图片参数后再设置为false,这就是decode时的技巧,下面就把完整代码贴出来,能够做为工具方法来使用:程序员
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
static
Bitmap decodeSampledBitmapFromResource(Resources res,
int
resId,
int
reqWidth,
int
reqHeight){
// First decode with inJustDecodeBounds=true to check dimensions
finalBitmapFactory.Options options =newBitmapFactory.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
;
returnBitmapFactory.decodeResource(res, resId, options);
}
|
上面的方法来自于google官网,不必进行修改,这就是程序员的拿来主义吧,关键在于要知道为何这么写。下面是我本身写的一个方法能够直接拿来当工具用。数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/**
* 对图片进行压缩,主要是为了解决控件显示过大图片占用内存形成OOM问题,通常压缩后的图片大小应该和用来展现它的控件大小相近.
*
* @param context 上下文
* @param resId 图片资源Id
* @param reqWidth 指望压缩的宽度
* @param reqHeight 指望压缩的高度
* @return 压缩后的图片
*/
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);
}
|