Skip to content Skip to sidebar Skip to footer

Drawable Loses Color Filter After Converting Into Bitmap

I am trying to add a color filer in a drawable and then convert it in Bitmap. The problem is when convert the drawable into bitmap it loses it's color filter.I used drawable in ima

Solution 1:

Use a canvas to blit the Drawable back onto a bitmap:

    Canvas canvas;
    Drawable drawable = <yourDrawable created from wherever>;
    Bitmap bmp = <your Bitmap which is the same width/height as the drawable>

    // blit the drawable onto the bitmap using a Canvas
    canvas = new Canvas(bmp);
    drawable.draw(canvas);

http://developer.android.com/guide/topics/graphics/2d-graphics.html

Solution 2:

I had the same problem and I figured it out finally!

We need to do following thing to get the bitmap with color filter applied on it.

image_view.setDrawingCacheEnabled(true);
Bitmapbitmap= Bitmap.createBitmap(image_view.getDrawingCache());

Solution 3:

I've faced the same issue and finally found a solution. Drawable can have multiple states thus you might be drawing wrong state.

You should switch it to proper mode before drawing:

Drawabledrawable= icon.loadDrawable(getContext());

        if (drawable == null) {
            return;
        }
        drawable.setState(newint[] {android.R.attr.state_enabled});

        Bitmapbitmap= Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvascanvas=newCanvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

Post a Comment for "Drawable Loses Color Filter After Converting Into Bitmap"