我有一个九个补丁图像的按钮。我不想为“按下”状态提供另一个图像(这是StateListDrawable
需要的)。我只是希望它在按下时变得“更暗”(就像 iOS 按钮一样)。我怎样才能做到这一点?
问问题
174 次
1 回答
1
使用这样的东西:
public class MyStateDrawable extends LayerDrawable {
public MyStateDrawable(Drawable[] layers) {
super(layers);
}
@Override
protected boolean onStateChange(int[] states) {
for (int state : states) {
if (state == R.attr.state_activated || state == android.R.attr.state_pressed) {
super.setColorFilter(0x7f000000, Mode.SRC_ATOP);
return true;
} else if(state == -android.R.attr.state_activated || state == -android.R.attr.state_pressed){
super.setColorFilter(0x00000000, Mode.SRC_ATOP);
return true;
}else {
super.setColorFilter(null);
}
}
return super.onStateChange(states);
}
@Override
public boolean isStateful() {
return true;
}
}
进而
StateDrawable state = new MyStateDrawable(new Drawable[]{mDefaultDrawable});
view.setImageDrawable(state);
希望对你有帮助 :)
于 2013-01-14T13:41:11.463 回答