Reputation: 15668
A couple of days ago the support library version v22.1.0 was released and among the new things I have seen DrawableCompat.
Now my question is what is the difference between DrawableCompat and a normal Drawable, what benefits does it bring and when and how to use it.
I have read Chris Banes' blog, but it is lacking to properly explain what this is and how it could be useful for developers.
Upvotes: 0
Views: 579
Reputation: 1007399
Quoting the blog post in question:
The Drawable tinting methods added in Lollipop are super useful for letting you dynamically tint assets.
Here, tinting refers to the Theme.Material
/Theme.AppCompat
approach of using grayscale drawables and applying tints, like colorPrimary
or colorAccent
, to tie them into a custom theme for an app. This is used in lots of places, in particular many places where the accent color is seen.
Continuing with the blog post:
AppCompat had its own baked in implementation in the v21 support library and we’ve now extracted that into DrawableCompat in support-v4 for everyone to use.
Hence, DrawableCompat
allows you to use the tint-the-grayscale image as appcompat-v7
does, for tinting drawables, going back to API Level 4. This is in contrast to the similar tinting methods available on Drawable
starting with API Level 21. In particular, if you are creating custom widgets and using appcompat-v7
, you might use DrawableCompat
to allow your widgets to adopt the app's color scheme.
what is the difference between DrawableCompat and a normal Drawable
DrawableCompat
departs from the typical ...Compat
approach. Classes like NotificationCompat.Builder
are replacements for their progenitors (e.g., Notification.Builder
). In this case, DrawableCompat
applies certain changes, like the tint, to another Drawable
, where that other drawable is "wrapped" by DrawableCompat
. DrawableCompat
simply provides the API and the wrap/unwrap capabilities; it is the Drawable
that you get from wrap()
that you would use in your app, in lieu of using the original grayscale Drawable
that you started with.
Upvotes: 4