Jos*_*emm 81
您需要分配一个状态列表drawable 的src属性ImageView.换句话说,该状态列表将具有用于选择,按下,未选择等的不同图像 - 这就是Twitter应用程序如何做到的.
所以如果你有一个ImageView:
<ImageView style="@style/TitleBarLogo"
android:contentDescription="@string/description_logo"
android:src="@drawable/title_logo" />
Run Code Online (Sandbox Code Playgroud)
src drawable(title_logo.xml)如下所示:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/title_logo_pressed"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/title_logo_pressed"/>
<item android:state_focused="true" android:drawable="@drawable/title_logo_selected"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/title_logo_default"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
在谷歌IO调度应用程序有一个很好的例子.
kli*_*mat 26
如果您没有其他可绘制状态,您可以使用它setColorFilter来实现简单的色调效果.
它的行为就像按下状态选择器一样,因此当按下图像时,它会将背景更改为浅灰色.
final ImageView image = (ImageView) findViewById(R.id.my_image);
image.setOnTouchListener(new View.OnTouchListener() {
private Rect rect;
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
image.setColorFilter(Color.argb(50, 0, 0, 0));
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
}
if(event.getAction() == MotionEvent.ACTION_UP){
image.setColorFilter(Color.argb(0, 0, 0, 0));
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
image.setColorFilter(Color.argb(0, 0, 0, 0));
}
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
它处理在视图边界外移动手指,因此如果它发生,它将恢复默认背景.
当您想要支持时,false从onTouch方法返回也很重要onClickListner.
使用 selectableItemBackground 作为背景:
android:background="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81310 次 |
| 最近记录: |