【发布时间】:2017-03-25 09:44:24
【问题描述】:
我有一个FrameLayout,其中有三个图像,其中第一个(左侧)和第二个(右侧)图像是从一个角四舍五入的,我通过使用 BitmapDrawable 创建它,然后用这个四舍五入图像方法。
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int topLeftX, int topLeftY, int topRightX, int topRightY, int bottomRightX, int bottomRightY, int bottomLeftX, int bottomLeftY) {
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
// the float array passed to this function defines the x/y values of the corners
// it starts top-left and works clockwise
// so top-left-x, top-left-y, top-right-x etc
RoundRectShape rrs = new RoundRectShape(new float[]{topLeftX, topLeftY, topRightX, topRightY, bottomRightX, bottomRightY, bottomLeftX, bottomLeftY}, null, null);
canvas.drawARGB(0, 0, 0, 0);
paint.setAntiAlias(true);
paint.setColor(0xFF000000);
rrs.resize(bitmap.getWidth(), bitmap.getHeight());
rrs.draw(canvas, paint);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
问题: 它运行良好,但我的应用程序有更多的图像,所以有时当应用程序经常使用它时它会崩溃并产生此错误。
java.lang.OutOfMemoryError: Failed to allocate a 406992 byte allocation with 397304 free bytes and 387KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:857)
at android.graphics.Bitmap.createBitmap(Bitmap.java:834)
at android.graphics.Bitmap.createBitmap(Bitmap.java:801)
我也尝试过使用 RoundedBitmapDrawable 类,但它只适用于圆角。如果有其他方法或方法可以解决我的问题,请告诉我。
【问题讨论】:
-
缩小尺寸
new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()) -
我已经在方法中使用了这一行
标签: java android bitmap imageview out-of-memory