Aru*_*run 6 android memory-leaks memory-management android-imageview
我在Android应用程序中工作,我使用位图将图像绑定到ImageView.我的要求是旋转ImageView并为该ImageView提供边框.我已经成功实现了这一点,但是在应用程序使用此活动两到三次之后,出现"强制关闭"错误,表示Bitmap不在VM内存中.请帮我最小化代码中的位图内存消耗.让我知道如何修改相同的代码?
final int BORDER_WIDTH = 5;
// Set the border color
final int BORDER_COLOR = Color.WHITE;
Bitmap res = Bitmap.createBitmap(CAPTURE_IMAGE.getWidth() + 2
* BORDER_WIDTH, CAPTURE_IMAGE.getHeight() + 2 * BORDER_WIDTH,
CAPTURE_IMAGE.getConfig());
System.gc();
Canvas canvas = new Canvas(res);
Paint paint = new Paint();
paint.setColor(BORDER_COLOR);
canvas.drawRect(0, 0, res.getWidth(), res.getHeight(), paint);
canvas.drawBitmap(CAPTURE_IMAGE, BORDER_WIDTH, BORDER_WIDTH, paint);
Matrix mat = new Matrix();
// Set the Imageview position
mat.postRotate(355);
bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(),
res.getHeight(), mat, true);
System.gc();
res.recycle();
res = null;
paint = null;
canvas = null;
mat = null;
// Set the captured bitmap image in the imageview
mShareImageView.setImageBitmap(bMapRotate);
Run Code Online (Sandbox Code Playgroud)
我认为你应该使用这样的收缩功能
bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(),
res.getHeight(), mat, true);
Bitmap myBitmap = ShrinkBitmap(bMapRotate , 300, 300);
mShareImageView.setImageBitmap(myBitmap );
private Bitmap ShrinkBitmap(String file, int width, int height) {
// TODO Auto-generated method stub
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
它对我有用,我避免了位图超出虚拟机内存异常
归档时间: |
|
查看次数: |
7087 次 |
最近记录: |