1

I have my xml drawable as below. It works great on lollipop and above. However for prelollipop, the android:tint="#888" is not working on the bitmap

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="oval">
                <solid android:color="#1222" />
            </shape>
        </item>
        <item android:bottom="2dp" android:left="1dp" android:right="1dp" android:top="1dp">
            <shape android:shape="oval">
                <solid android:color="#eee" />
            </shape>
        </item>
        <item>
            <bitmap android:src="@drawable/ic_action_name" android:tint="#888" />
        </item>
    </layer-list>

This is same as issue reported in Drawable tinting for api <21, but the answer there are using ImageView, which I can't do it here, since I'm composing my drawable from the bitmap with some shape in the xml. Any soluion out there?

Community
  • 1
  • 1
Elye
  • 53,639
  • 54
  • 212
  • 474

1 Answers1

0

Have you tried DrawableCompat.wrap()?

Example...

static Drawable drawable(Resources resources, Bitmap bitmap, int color) {
    Drawable drawable = new BitmapDrawable(resources, bitmap);
    Drawable.ConstantState state = drawable.getConstantState();
    drawable = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
    DrawableCompat.setTint(drawable, color);
    return drawable
}

That'll turn the Bitmap to a Drawable then tint it.

If you need the Drawable as a Bitmap, just use this or similar...

static Bitmap bitmap(Drawable drawable) {
    if (drawable != null) {
        Bitmap bitmap= Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }
    return null;
}

That should bring it back to a Bitmap, although you may have to set a width/height in the XML.

Jon Merritt
  • 114
  • 2
  • 11
  • Will this only tint my @drawable/ic_action_name, or the entire drawable, including the 2 oval shapes, i.e. a grey circle and it's shade? – Elye Nov 03 '16 at 02:29
  • Sorry for the delay in responding. This will tint the entire bitmap. I'm realizing now that you are trying to tint the bitmap entirely on the layer list, that's not possible as far as I know. I would say you should probably try doing the bitmap tinting separately in code and then add it to your layer list after the fact. I'll need more code/understanding of what you're doing to give any real specific recommendations though. – Jon Merritt Nov 03 '16 at 15:31
  • No problem. Appreciate your effort try to help. I'm just planning to tint my @drawable/ic_action_name, but not the entire drawable. – Elye Nov 03 '16 at 23:08
  • Could try this answer, that may get you going. – Jon Merritt Nov 03 '16 at 23:29
  • Make sure to add 'android:id' to the drawables '' element. – Jon Merritt Nov 03 '16 at 23:32
  • Basically what I'm saying is, first create your layer-list like you have it with the addition of an id on the bitmap item. Then at some point before you set tint, grab the current drawable in the layer-list as shown in that answer I linked earlier, set tinting using DrawableCompat.wrap() minis the drawable creation from my answers first method. Then again from that answer, replace the layer-list bitmap item drawable, Then if it absolutely has to be a bitmap, use the second method. That's with no idea on the end point requirements or what's going on specifically though. – Jon Merritt Nov 04 '16 at 05:08
  • Hi @Jon Merritt, thanks for the further elaboration. I don't know how to `replace the layer-list bitmap item drawable`. I gave an ID to the bitmap, but how do I get to refer to it from the Java code? I can't do `Bitmap bitmap = (Bitmap)findViewById(R.id.bitmap_id);` Hopes you could give the code example of how to add the above code-tinted drawable to the xml file bitmap. Thanks! – Elye Nov 06 '16 at 02:44
  • From this answer - http://stackoverflow.com/questions/8018435/how-to-change-a-layer-list-drawable/11021907#11021907 – Jon Merritt Nov 06 '16 at 03:26