添加彩色滤光片为可绘制改变使用相同的可绘制的所有按钮 [英] Adding a color filter to a Drawable changes all Buttons using the same Drawable

查看:284
本文介绍了添加彩色滤光片为可绘制改变使用相同的可绘制的所有按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个屏幕,多个按钮使用相同的背景绘制对象。我有重复使用的code我在不同的项目中使用添加一个OnTouch监听器,增加了一个灰色的彩色滤光片,同时被触摸一个按钮。这通常工作正常,但在这种情况下,所有的按钮都设色当任何人是pressed。

I have a screen where multiple Buttons use the same background Drawable. I have reusable code I use in various projects to add an OnTouch listener that adds a gray color filter while a button is being touched. That usually works fine, but in this case ALL the buttons are tinted when any of them is pressed.

我看到<一解释href="http://developer.android.com/guide/topics/graphics/2d-graphics.html">http://developer.android.com/guide/topics/graphics/2d-graphics.html:

请注意:在你的项目中的每个独特的资源可以维持只有一个   状态,不管有多少个不同的对象,你可以实例化   它。例如,如果你从同一个实例2拖拉的对象   图像资源,则改变一个属性(如阿尔法)用于一个   在可绘,那么它也将影响其他。

Note: Each unique resource in your project can maintain only one state, no matter how many different objects you may instantiate for it. For example, if you instantiate two Drawable objects from the same image resource, then change a property (such as the alpha) for one of the Drawables, then it will also affect the other.

所建议的解决方案是使用一个TweenAnimation,这似乎不使用滤色器来工作。

The suggested solution is to use a TweenAnimation, which does not seem to work with color filters.

我也看到了<一个href="http://stackoverflow.com/questions/7979440/android-cloning-a-drawable-in-order-to-make-a-statelistdrawable-with-filters">Android:为了使一个StateListDrawable与滤波器的其中建议使用drawable.getConstantState()克隆可绘。newDrawable()。这似乎并没有有所作为。我猜,只要是相同的物理图像文件时,所有可绘制区域将受到更改任何其他绘制对象使用相同的资源。

I also saw Android: Cloning a drawable in order to make a StateListDrawable with filters which suggests using drawable.getConstantState().newDrawable(). This does not seem to make a difference. I'm guessing 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.

有什么解决办法是存在的,比创建第二个背景图像显示pssed状态$ P $其他?这将是很好,有一个简单的编程解决方案,我可以添加到我的code和在每一个项目中使用。

What solution is there, other than creating a second background image to show the pressed state? It would be nice to have a simple programmatic solution I can add to my code and use in every project.

推荐答案

例子应该为你工作:

Drawable buttonBackground = context.getResources().getDrawable(R.drawable.bg);
buttonBackground = buttonBackground.mutate();

//Set your filter here

这篇关于添加彩色滤光片为可绘制改变使用相同的可绘制的所有按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆