Coil: Color attribute tint doesn't show when loading drawable

Created on 23 Jul 2020  路  7Comments  路  Source: coil-kt/coil

Describe the bug
Colour attributes (e.g. ?colorPrimary) applied as a tint to a drawable (see below example) are not shown in resulting BitmapDrawable when using imageView.load(R.drawable.tinted_icon)

e.g. tinted icon:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0"
    android:tint="?colorPrimary"
    android:tintMode="src_in">
    <path
        android:fillColor="#00FF00"
        android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z" />
</vector>

Notice the android:fillColor="#00FF00", which is bright green, but the android:tint="?colorPrimary" with android:tintMode="src_in" which should result in overlaying colorPrimary completely.

What shows when using Coil's load function:
image

Expected behavior
ImageView shows tinted drawable like so:
image

To Reproduce
Given a fresh app with a simple layout that contains two ImageViews, load the same drawable in two different ways like so:

val icon = findViewById<ImageView>(R.id.imageView)
val icon2 = findViewById<ImageView>(R.id.imageView2)
icon.load(R.drawable.tinted_icon)
icon2.setImageResource(R.drawable.tinted_icon)

Version
Using coil version 0.11.0,
Tested on:

  • Pixel2 XL API29
  • Nexus5 API24
bug

Most helpful comment

Fixed in 0.12.0.

All 7 comments

I've done some more digging, and it looks like Coil caches the application context for loading the drawable. This is great for avoiding leaks, but is problematic for resolving context-specific theme attributes.

Is there a way we can configure the load call to use the view's context?

Thanks for the report + taking a look into this. I believe this should be fixed as part of the next release which will be out later this week. You can also use the latest snapshot if you need the fix right away.

Awesome, thanks @colinrtwhite. It seems to have done the trick!

Fixed in 0.12.0.

Hey @colinrtwhite it looks like this is still an issue in 0.12.0 and 1.0.0-rc1.

Using the same example with the green icon that is tinted purple with a colour attribute, and the code:

val icon = findViewById<ImageView>(R.id.imageView)
val icon2 = findViewById<ImageView>(R.id.imageView2)
icon.load(R.drawable.tinted_icon)
icon2.setImageResource(R.drawable.tinted_icon)

I'm still getting the following result where the colour attribute tint isn't applied using coil.load and passing a drawable res.

However, using coil.load and passing a resolved Drawable works as expected and the icon is tinted correctly.
e.g.

// Icon is tinted correctly doing this
val drawable = icon.context.getDrawable(R.drawable.tinted_icon)
icon.load(drawable)

I can go with the above workaround for now, but it'd be nice for loading of the drawable resource to be offloaded to a background thread.

Any ideas what the cause of this might be? Happy to help!

@DylanLange Ah yep sorry. The fix that went out in 0.12.0 only fixed the issue for placeholder/error/fallback drawables. However, I think I know how to fix it - will post a PR later today.

The fix is available in the latest snapshot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wooldridgetm picture wooldridgetm  路  3Comments

digitalheir picture digitalheir  路  4Comments

GeniusRUS picture GeniusRUS  路  4Comments

r4phab picture r4phab  路  6Comments

dafi picture dafi  路  3Comments