我有一个九个补丁图像的按钮。我不想为“按下”状态提供另一个图像(这是StateListDrawable
所要求的)。我只是希望它在按下时变得“更暗”(就像iOS按钮一样)。我怎样才能做到这一点?
答案 0 :(得分: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);
希望这会对你有所帮助:)。