ImageView:在新闻发布时更改图像 [英] ImageView: change image on press, and on release
问题描述
我将在按下imageView图片时对其进行更改,并在释放压力时再次对其进行更改.该ImageView包含一个按钮的图片,当我按下按钮时,我会通过更改图像给用户一个反馈(就像他按下普通按钮时一样).这是我的代码:
I would change imageView picture when it's pressed, and change it again when pressure is released. That ImageView contains picture of a button, and when is pressed i would give to user a feedback (like when he press a normal button) by change image. This is my code:
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("IMAGE", "motion event: " + event.toString());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
imageView.setImageResource(R.drawable.button_hover);
}
case MotionEvent.ACTION_UP: {
imageView.setImageResource(R.drawable.button);
}
}
return false;
}
});
这是xml
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="std_button"
android:src="@drawable/button" />
但是它不起作用.如果我仅使用ACTION_DOWN事件,则可以根据需要更改图片, 怎么了?
but it doesn't work. If i use only ACTION_DOWN event, image change as i want, What's wrong?
推荐答案
尝试添加中断;在每种情况之后,
Try add break; after each case,
并且正如giozh在onTouch中所说,我始终返回false,因此触摸事件结果始终不被占用.只需在每个case语句后将return设置为真.
And as giozh said inside onTouch i return always false, so touch event result always not consumed. Just put return true after each case statement.
这篇关于ImageView:在新闻发布时更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!