Int primar圜olor = res.getColor(R.color.primary) ītColorFilter(primar圜olor, PorterDuff.Mode. Here's a short sample applying a themed color to the background of a View: Resources res = getResources() ĭrawable background = res.getDrawable(R.drawable.background) The second problem is also easily avoided because all Drawables have a setColorFilter() method! How handy. To theme, what we actually want to use is _IN, which only draws the tint color. ImageView uses _ATOP, which places our tint color on top of the image (but still draws the original image below). To solve the first problem, we just need to use a different Porter/Duff mode. In particular, it uses Porter/Duff compositing to apply a color on top of an image. You want to be able to tint any Drawable in any View.īoth of these problems are easily solved by investigating how ImageView implements its tint: with a ColorFilter. So, for example, if the source asset is black, and you want it to be #77FFFFFF (a translucent shade of white), you'll actually end up getting that shade of white with a black background beneath it.Īndroid:tint is limited to ImageView. What you want is for the tint color to take over entirely instead it applies the tint on top of the existing color. ImageView's tint mixes the tint color with the original asset. There's two problems with ImageView's tint, though, which is why you should avoid it: You can set a tint color in XML and voilà! Your asset is now colored differently. isSelected (New Color) : (Default Color) Here is my sample code. Now go back to the button color and use the ternary operator. Make one boolean value isSelected (False as default) and whenever the button is pressed setState the isSelected to true. The good news is this time-saving method can be used today! We can get a hint of how this might work by looking at ImageView's android:tint attribute. Make sure you are using a stateful widget. You can change all your app's colors at once, or just tweak the colors of a single drawable, without having to go back to your designers for new assets. One of the neat aspects of the material theme in the upcoming Android L release is being able to theme all of your app (icons and all) with just a handful of attributes: theming method can save you tons of time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |