【问题标题】:Out of Memeory when Rounding corner of Image图像圆角时内存不足
【发布时间】: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


【解决方案1】:

在 AndroidManifest.xml 文件中添加 largeHeap,

<application
    android:largeHeap="true"
    ...
    ...
</application>

【讨论】:

    【解决方案2】:

    就像@Jaspreet Kaur 提到的那样,您的代码没有释放旧位图的内存。但是,这可能有点复杂。假设您正在使用某个框架加载图像,所以基本上这些框架将使用 Lru 缓存来缓存您的图像。如果是这样,那就有问题了:

    1. 您的原始图像将保留在库的 LruCache 中,当加载更多图像时会自动回收。
    2. 您的新圆形图像未存储在库的 LruCache 中,这意味着您需要管理自己的 LruCache 并稍后释放图像。

    我怀疑你可以访问图书馆的 LruCache。我建议您尝试其他方式,例如如果您使用 Glide,然后尝试实现自定义转换,因为它会为您处理内存管理。

    如果您没有使用任何库来加载图像,我相信您必须使用可以访问的 LruCache,就像@Jaspreet Kaur 提到的那样,释放或回收传入的旧位图并将新图像保留在 LruCache然后 LruCache 会在图像达到内存限制时释放图像。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      当您的位图完成其工作后,将其设为可回收并为空。

      if(bitmap!=null)
      {
          bitmap.recycle();
          bitmap=null;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-04
        • 2014-09-15
        • 2013-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 2013-11-09
        相关资源
        最近更新 更多