L. *_* G. 21
如果这是一个ImageButton并且您不想为每个按下/未按下状态使用多个drawable,则可以使用图像的颜色过滤器.该解决方案类似于Omar使用的解决方案.
创建一个OnTouchListener,在触摸时修改滤色器.
public class ButtonHighlighterOnTouchListener implements OnTouchListener {
final ImageButton imageButton;
public ButtonHighlighterOnTouchListener(final ImageButton imageButton) {
super();
this.imageButton = imageButton;
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//grey color filter, you can change the color as you like
imageButton.setColorFilter(Color.argb(155, 185, 185, 185));
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton.setColorFilter(Color.argb(0, 185, 185, 185));
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
将此侦听器分配给您的按钮:
myButton = (ImageButton) findViewById(R.id.myButton);
myButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(myButton));
Run Code Online (Sandbox Code Playgroud)
更新
改进了通过其复合Drawable将Highlighter应用于ImageView,ImageButton或TextView的类.
public class ButtonHighlighterOnTouchListener implements OnTouchListener {
private static final int TRANSPARENT_GREY = Color.argb(0, 185, 185, 185);
private static final int FILTERED_GREY = Color.argb(155, 185, 185, 185);
ImageView imageView;
TextView textView;
public ButtonHighlighterOnTouchListener(final ImageView imageView) {
super();
this.imageView = imageView;
}
public ButtonHighlighterOnTouchListener(final TextView textView) {
super();
this.textView = textView;
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
if (imageView != null) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
imageView.setColorFilter(FILTERED_GREY);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageView.setColorFilter(TRANSPARENT_GREY); // or null
}
} else {
for (final Drawable compoundDrawable : textView.getCompoundDrawables()) {
if (compoundDrawable != null) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
// we use PorterDuff.Mode. SRC_ATOP as our filter color is already transparent
// we should have use PorterDuff.Mode.LIGHTEN with a non transparent color
compoundDrawable.setColorFilter(FILTERED_GREY, PorterDuff.Mode.SRC_ATOP);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
compoundDrawable.setColorFilter(TRANSPARENT_GREY, PorterDuff.Mode.SRC_ATOP); // or null
}
}
}
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
Lio*_*luz 13
看到这里
罗曼盖伊的回答也在这里:
在res/drawable中,创建一个名为mybutton_background.xml的文件,并在其中放入如下内容:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"
android:drawable="@drawable/button_background_focus" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/button_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/button_background_pressed" />
<item android:drawable="@drawable/button_background_normal" />
</selector>
Run Code Online (Sandbox Code Playgroud)
然后将此drawable设置为按钮的背景
android:background="@drawable/mybutton_background"
| 归档时间: |
|
| 查看次数: |
31928 次 |
| 最近记录: |