Rotating images on android. Is there a better way?

后端 未结 3 1043
-上瘾入骨i
-上瘾入骨i 2020-12-31 13:48

I have an app that displays quite a few images for the user, and we\'ve been seeing a lot of error reports with OutOfMemoryError exception.

What we curr

3条回答
  •  没有蜡笔的小新
    2020-12-31 14:21

    When working with lots of Bitmaps be sure to call recycle() on them as soon as they are not needed. This call will instantly free memory associated with a particular bitmap.

    In your case if you do not need the original bitmap after rotation, then recycle it. Something along the lines of:

    Bitmap result = bmp;
    
    // Check if image is a landscape image
    if (bmp.getWidth() > bmp.getHeight()) {
        // Rotate it to show as a landscape
        Matrix m = image.getImageMatrix();
        m.postRotate(90);
        result = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
        // rotating done, original not needed => recycle()
        bmp.recycle();
    }
    
    image.setImageBitmap(result);
    

提交回复
热议问题