0

Because I need my drawable as bitmap descriptor, I convert my resource to a Drawable then to a Bitmap.

But I want to change its color, So I used the following code found somewhere here:

private static Bitmap drawableToBitmap (Drawable drawable) {
    Bitmap bitmap = null;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if(bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());

    int iColor = Color.parseColor("#006f00");

    drawable.setColorFilter(iColor, PorterDuff.Mode.SRC_ATOP);

    drawable.draw(canvas);
    return bitmap;
}

But the drawable's color remains black. Can you please tell me why and how to get it working?

fralbo
  • 2,534
  • 4
  • 41
  • 73

4 Answers4

1

This converts a BitmapDrawable to a Bitmap.

Drawable d = ImagesArrayList.get(0);  
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

And for changing bitmap's color see below link

How to change Bitmap image color in android?

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
  • class cast exception in BitmapDrawable. – RaRa Nov 19 '15 at 13:32
  • @Ramani : Please give more details regarding it. What have you tried ? – KishuDroid Nov 19 '15 at 13:46
  • i will use FontIconDrawable.inflate(context, R.xml.ic_red_marker) and create drawable but when i run app it will crash in BitmapDrawable line say classcastexception – RaRa Nov 19 '15 at 13:49
  • The code only works if **d** already is a BitmapDrawable, in which case it's trivial to retrieve it as a bitmap... Will crash with ClassCastException in all other cases. – KishuDroid Nov 19 '15 at 13:58
  • you have any idea about my code? how to get bitmap. actually i have to set map marker bitmap or resource or etc. i want to set thi"FontIconDrawable.inflate(context, R.xml.ic_red_marker) " in marker – RaRa Nov 19 '15 at 14:04
  • Please give some more code so that i can understand what you want to do ? – KishuDroid Nov 20 '15 at 04:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95639/discussion-between-ramani-and-kishudroid). – RaRa Nov 20 '15 at 04:52
0

I think the problem could be PorterDuff.Mode.SRC_ATOP, the method you use for apply the filter. I don't know specifically SRC_ATOP but this link explains all of them.

I don't know how your image is composed, but I'd use DST_ATOP instead of SRC_ATOP.

EDIT: I presumed that this code won't return in the case you are analizing, 'cause if it returns the filter obviously is never reached.

if (drawable instanceof BitmapDrawable) 
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    if(bitmapDrawable.getBitmap() != null) {
        return bitmapDrawable.getBitmap();
    }
}
Simone Chelo
  • 706
  • 8
  • 25
0
Paint p = new Paint();
ColorFilter filter = new LightingColorFilter(Color.RED, 0);
p.setColorFilter(filter);
canvas.drawBitmap(bitmap, 0, 0, p);
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

To convert drawable into bitmap use this -

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.icon_resource);

You can change bitmap pixels color by this-

int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
for (int i=0; i<myBitmap.getWidth()*5; i++)
    pixels[i] = Color.BLUE;
myBitmap.setPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32