I have a screen where several buttons use the same Drawable background. I have reusable code that I use in different projects to add an OnTouch listener that adds a gray color filter at the click of a button. This usually works fine, but in this case ALL the buttons are tinted when any of them is pressed.
I see an explanation at http://developer.android.com/guide/topics/graphics/2d-graphics.html :
Note. Each unique resource in your project can only support one, no matter how many different objects you can create for It. For example, if you create two Drawable objects from the same one and then change the property (for example, alpha) for one of Drawables, then this will also affect the other.
The proposed solution is to use TweenAnimation, which does not seem to work with color filters.
I also saw Android: cloning a picture to make a StateListDrawable with filters , which suggests using drawable.getConstantState (). newDrawable (). It does not seem to matter. I assume that as long as the same physical image file is used, all Drawables will be affected by a change to any other Drawable using the same resource.
What solution exists besides creating a second background image to display the pressed state? It would be nice to have a simple software solution that I can add to my code and use in every project.
android
Chad Schultz Jun 04 2018-12-12T00: 00Z
source share