What is new in Drawable Tinting in Android L Developer Preview compared to previous version?
Asked Answered
C

2

20

I am working of new Android L preview and now dealing with tinting concept on drawable.

I want to know if there is anything new regarding drawable tinting in Android L Developer Preview.

I read this this documentation which says:

The Android L Developer Preview enables you to define bitmaps or nine-patches as alpha masks and to tint them using a color resource or a theme attribute that resolves to a color resource (for example, ?android:attr/colorPrimary). You can create these assets only once and color them automatically to match your theme.

But I didn't understand how it is different from previous version. I know how to use ColorFilter or PorterDuffColorFilter to apply tint on image. Any help regarding this will be appreciated.

Cruck answered 25/8, 2014 at 9:11 Comment(0)
M
21

Starting in L, you can specify tints in XML. These can reference theme attributes (as shown), color state lists, color resources, or explicit hex color codes. By default, the tint mode is SRC_IN, but it can be set to something else using the android:tintMode attribute.

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/my_icon"
    android:tint="?android:attr/colorControlNormal" />

The default controls all use ?android:attr/colorControlNormal for their normal state (e.g. an unchecked check box) and ?android:attr/colorControlActivated (which maps to ?android:attr/colorAccent by default) for their activated state (e.g. a checked check box).

You can use these attributes in your own app drawables to inherit the default framework colors, or you can redefine them to change the default or activated control colors. You can also reference attributes specific to your app (as shown).

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/my_icon"
    android:tint="?attr/myThemeAttribute" />
Maribelmaribelle answered 26/8, 2014 at 0:14 Comment(5)
Any idea how to tint a compound drawable in a TextView?Afghanistan
What effect are you trying to achieve? Like only tinting a single layer in the compound drawable or tinting the whole thing? From XML or at run time?Maribelmaribelle
I meant a compound drawable in a TextView set using XML android:drawableLeft. I would have expected something like android:drawableTint. Now I'm using code to get the drawable and set a color filter.Afghanistan
The answer is right in front of you. You create a bitmap drawable as @Maribelmaribelle states. Let's say you name it @drawable/my_icon_tinted. Set the tint. And then use that drawable in your TextView as drawableLeft. Be careful though, the default tint mode in the XML layout is SRC_ATOP, but the default tint mode in the bitmap will be SRC_IN.Strobilaceous
Sir. You are a saint."android:tint="?android:attr/colorControlNormal" this thing saved my life and i want to thank you for that. Have a good day.Insanitary
I
13

with new Android Support Library 22.1 now more support for tinting possible!

The Support V4 library serves as the base of much of the Android Support Library and contains many of the classes focused on making backward compatibility much easier.

DrawableCompat now brings drawable tinting back to API 4: simply wrap your Drawable via DrawableCompat.wrap(Drawable) and setTint(), setTintList(), and setTintMode() will just work: no need to create and maintain separate drawables only to support multiple colors!

Info from Chris Banes blog:

Drawable tinting The Drawable tinting methods added in Lollipop are super useful for letting you dynamically tint assets. 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. It’s important to know how it works though.

Drawable drawable = ...;

// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);

// We can now set a tint
DrawableCompat.setTint(drawable, Color.RED);
// ...or a tint list
DrawableCompat.setTintList(drawable, myColorStateList);
// ...and a different tint mode
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

The thing to remember is that after you call DrawableCompat.wrap(), you can not rely on the result being the same type as what you give it. Instead you should use DrawableCompat.unwrap() to retrieve the original Drawable.

Internally, we now wrap your Drawable in a special ‘tint drawable’ will automatically update your Drawable’s color filter from the specified tint. This allows us to handle ColorStateList instances.

But I wonder how to use it xml !! there is option called

<TintImageView
android:layout_width=""
android:layout_height="" 
android:src=""
android:backgroundTint="@color/green"/>

but if I want to change the drawable left|right icon for EditText TextView there no way to do that I feel!

Irresolvable answered 29/5, 2015 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.