当前位置 : 主页 > 手机开发 > android >

聊聊Android中Bitmap缩放

来源:互联网 收集:自由互联 发布时间:2021-06-11
最近在学IOS,发现IOS加载图片不像Android那样,动不动就 OOM(Out of memory) ,下面看看一个比较通用的方法。。 先摆个阵 先上个布局文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 LinearLayout

最近在学IOS,发现IOS加载图片不像Android那样,动不动就OOM(Out of memory),下面看看一个比较通用的方法。。

先摆个阵

先上个布局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showThumbImage"
android:text="获取自适应图" />


<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="matrix" />


</LinearLayout>

Activity代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class extends Activity {

private ImageView mImageView;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView);
}

public void showThumbImage(View v) {

}
}

我们使用的图片

是一张3500?×?2625的图片,我们现在就让它缩放后显示到512MB的Android模拟器上面

图片

好啦,上车吧

Options的inSampleSize

BitmapFactory.decodeXXX中我们都可以看到有一个Options的参数。我们可以设置这个参数,从而使图片按照预期加载。

属性说明

下面是Options的两个基本属性:

  • inJustDecodeBounds:是否只加载图片的轮廓
  • inSampleSize:加载图片时加载原来的1/inSampleSize

1、当inJustDecodeBounds为true的时候,Android系统并不会去加载图片的具体数据,只会提取图片的轮廓信息。在这里我们就可以防止加载大图片进来的时候把内存给挤爆。
2、上面获取了宽高有怎样????这样我们就可以根据比例去挑中inSampleSize的值,inSampleSize可以告诉系统加载Bitmap的时候按照怎样的比例去加载(按照1/inSampleSize去加载),这样在加载进来系统之前就已经处理好了,就不会挤爆内存了。

代码

1
2
3
public void showThumbImage(View v) {
mImageView.setImageBitmap(thumbImageWithInSampleSize(mImageView.getWidth(), mImageView.getHeight()));
}
1
2
3
4
5
6
7
8
9
10
大专栏  聊聊Android中Bitmap缩放11
12
13
14
public Bitmap thumbImageWithInSampleSize(float destWidth, float destHeight) {
Options opt = new Options();
opt.inJustDecodeBounds = true;

BitmapFactory.decodeResource(getResources(), R.drawable.img, opt);
double scaleW = Math.max(destWidth, opt.outWidth) / (Math.min(destWidth, opt.outWidth) * 1.0);
double scaleH = Math.max(destHeight, opt.outHeight) / (Math.min(destHeight, opt.outHeight) * 1.0);
opt.inSampleSize = (int) Math.max(scaleW, scaleH);

opt.inJustDecodeBounds = false;

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img, opt);
return bitmap;
}
  • 上面通过测试,图片可以正常显示出来。第一次设置inJustDecodeBounds = true是告诉Android系统不去加载Bitmap 的具体数据。计算完inSampleSize之后真正去加载的时候别忘了设置inJustDecodeBounds = false
  • 但是inSampleSize也有自身的缺点,就是只能缩放整数倍,所以很多时候可能不会自适应,这时候我们可以通过设置图片拉伸或者填充等方法达到预期效果。在强调不失真的情况下,也可以使用四舍五入的方法:
1
2
double scaleW = Math.max(destWidth, opt.outWidth) / (Math.min(destWidth, opt.outWidth) * 1.0) - 0.5;
double scaleH = Math.max(destHeight, opt.outHeight) / (Math.min(destHeight, opt.outHeight) * 1.0) - 0.5;

使用-0.5是因为最后缩放的是1/inSampleSize倍。

下面的方法慎用

因为图片始终会加载到内存中,所以如果是OOM的话就使用上面的方法。
下面的方法只是提供缩放图片的思路

使用矩阵变换

矩阵在图像领域中的应用非常广泛,只要给出对应的矩阵,就能实现缩放、平移等操作。但是在Android中我们并不需要自己构造矩阵,一切早已被封装好了。例如下面的matrix.postScale(scaleW,scaleH),直接得到特定缩放比例后的矩阵。

1
2
3
public void showThumbImage(View v) {
mImageView.setImageBitmap(thumbImageWithMatrix(mImageView.getWidth(), mImageView.getHeight()));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
public Bitmap thumbImageWithMatrix(float destWidth, float destHeight) {
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.img);
float bitmapOrgW = bitmapOrg.getWidth();
float bitmapOrgH = bitmapOrg.getHeight();

float bitmapNewW = (int) destWidth;
float bitmapNewH = (int) destHeight;

Matrix matrix = new Matrix();
matrix.postScale(bitmapNewW / bitmapOrgW, bitmapNewH / bitmapOrgH);
bitmapOrg.recycle();
return Bitmap.createBitmap(bitmapOrg, 0, 0, (int) bitmapOrgW, (int) bitmapOrgH, matrix, true);
}

ThumbnailUtils

其实Android也给我们提供一个ThumbnailUtils工具类,直接就可以生成缩略图,不过跟上面一样,需要去先加载Bitmap,所以也不能解决OOM错误

1
2
3
public void showThumbImage(View v) {
mImageView.setImageBitmap(thumbImageWithThumbnailUtils(mImageView.getWidth(), mImageView.getHeight()));
}
1
2
3
4
5
6
public Bitmap thumbImageWithThumbnailUtils(float destWidth, float destHeight) {
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.img);
Bitmap bitmap = ThumbnailUtils.extractThumbnail(bitmapOrg, (int) destWidth, (int) destHeight);
bitmapOrg.recycle();
return bitmap;
}
网友评论