android 设备查看内存不足,android – 在设备上创建位图时内存不足

我有高分辨率图像的问题.

我使用nodpi-drawable文件夹为1280×720图像,并使用此代码来缩放它.

public static Drawable scaleDrawable(Drawable d, int width, Activity cxt)

{

BitmapDrawable bd = (BitmapDrawable)d;

double oldWidth = bd.getBitmap().getWidth();

double scaleFactor = width / oldWidth;

int newHeight = (int) (d.getIntrinsicHeight() * scaleFactor);

int newWidth = (int) (oldWidth * scaleFactor);

Drawable drawable = new BitmapDrawable(cxt.getResources(),MainScreen.getResizedBitmap(bd.getBitmap(),newHeight,newWidth));

BitmapDrawable bd2 = (BitmapDrawable)drawable;

return drawable;

}

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();

int height = bm.getHeight();

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

// create a matrix for the manipulation

Matrix matrix = new Matrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

return resizedBitmap;

}

我使用该代码将图像缩放到屏幕,所以如果屏幕是320×480,图像将缩放到320并保持比例,我不在乎图像是否从底部离开屏幕.

所有它的工作正常,但尝试在xhdpi设备特别是三星Galaxy Note 2与屏幕正好720×1280.

它在行中出现Out of Memory Exception崩溃:

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

我不明白为什么,图像应该从720到720,但我的代码必须是非常糟糕的优化或其他东西.

我没有试过1080×1920设备,但它似乎也会崩溃.

在查看代码时,有人会看到一些不好的东西吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值