vinc3m1 / RoundedImageView

A fast ImageView that supports rounded corners, ovals, and circles.
Other
6.44k stars 1.25k forks source link

When using Glide, RoundedDrawable -> drawableToBitmap leads to OutOfMemoryException #192

Closed bikranttripathi closed 7 years ago

bikranttripathi commented 7 years ago

I started noticing huge spikes in memory when using this library with Glide. I believe the issue lies in drawableToBitmap method.

Since Glide is wrapping bitmap into GlideBitmapDrawable, RoundedImageView doesn't know how to extract the bitmap from that drawable. So, every single time it just creates another bitmap and now we have two instances of the same image.

I did a quick & dirty fix for this as a short term solution by updating the drawableTobitmap method to:

public static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        } else if (drawable instanceof GlideBitmapDrawable) {
            return ((GlideBitmapDrawable) drawable).getBitmap();
        } else if (drawable instanceof SquaringDrawable) {
            Drawable wrappedDrawable = drawable.getCurrent();
            if (wrappedDrawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) wrappedDrawable).getBitmap();
            } else if (wrappedDrawable instanceof GlideBitmapDrawable) {
                return ((GlideBitmapDrawable) wrappedDrawable).getBitmap();
            } else {
                Log.e(TAG, "Wrapped Drawable " + drawable.getClass().getSimpleName() + " is not supported.");
            }
        } else {
            Log.e(TAG, "Drawable " + drawable.getClass().getSimpleName() + " is not supported.");
        }

        Bitmap bitmap;
        int width = Math.max(drawable.getIntrinsicWidth(), 2);
        int height = Math.max(drawable.getIntrinsicHeight(), 2);
        try {
            bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
        } catch (Exception e) {
            e.printStackTrace();
            Log.w(TAG, "Failed to create bitmap from drawable!");
            bitmap = null;
        }

        return bitmap;
    }

and added Glide as provided dependency provided 'com.github.bumptech.glide:glide:3.8.0'

It would be great if there is a better way to do this than my hack :)

cesarsicas commented 7 years ago

Same Problem Here. Even when optimize RecyclerView/Adapter, still getting this problem

vinc3m1 commented 7 years ago

Glide isn't currently supported, use the rounded transforms from https://github.com/wasabeef/glide-transformations and you can just target a regular ImageView