Ins*_*Bun 3 android colorfilter statelistdrawable
似乎StateListDrawable将忽略应用于它们包含的drawable的颜色过滤器.例如:
StateListDrawable sld = new StateListDrawable();
Drawable pressedState = Context.getResources().getDrawable(R.drawable.solid_green);
pressedState.setColorFilter(Color.RED, PorterDuff.Mode.SRC);
sld.addState(new int[] {android.R.attr.state_pressed}, pressedState);
// Other states...
Run Code Online (Sandbox Code Playgroud)
如果应用于sld视图的背景,则可以预期视图的背景在按下时会变为红色.相反,它会变成绿色 - pressedState没有应用过滤器的颜色.
要解决此问题,您必须StateListDrawable根据drawable所处的状态将颜色过滤器应用于自身.以下扩展将StateListDrawable完成此操作.
public class SelectorDrawable extends StateListDrawable {
public SelectorDrawable(Context c) {
super();
addState(new int[] {android.R.attr.state_pressed}, c.getResources().getDrawable(R.drawable.solid_green));
// Other states...
}
@Override
protected boolean onStateChange(int[] states) {
boolean isClicked = false;
for (int state : states) {
if (state == android.R.attr.state_pressed) {
isClicked = true;
}
}
if (isClicked)
setColorFilter(Color.RED, PorterDuff.Mode.SRC);
else
clearColorFilter();
return super.onStateChange(states);
}
}
Run Code Online (Sandbox Code Playgroud)
逻辑输入onStateChange(int[] states)可以进一步扩展以测试不仅仅是按下状态,并且可以相应地应用不同的滤色器.
| 归档时间: |
|
| 查看次数: |
1327 次 |
| 最近记录: |