【发布时间】:2014-04-27 19:40:04
【问题描述】:
我一直在寻找解决方案,但似乎没有人尝试过,或者可能太简单了我没有找到它。
无论如何,我正在尝试使用ImageView 实现按钮按下。我想要的是,ImageView 显示默认图像,如果按下 (OnTouch) [有一个 MotionEvent.ACTION_DOWN 块] 运行帧动画。当您释放触摸[具有MotionEvent.ACTION_UP 块] 时,它应该停止动画并返回到默认图像。
注意:帧动画是连续重复的,不应包含默认图像作为循环图像之一。 (触摸时不应显示按钮向上图像)
现在的问题是,我有动画工作,但根据 android 文档,<animation-list> 标签中的第一项将默认显示。但是如果我将默认图像(按钮未触摸状态)添加为第一项,它将显示在循环中。另外,当我释放触摸时,我使用AnimationDrawable 的stop() 方法,它在当前帧(图像)处停止动画,我似乎找不到任何方法来停止并进入默认图像状态。
这是我的代码:
button_anim.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="@drawable/button1_press1" android:duration="200" />
<item android:drawable="@drawable/button1_press2" android:duration="200" />
<item android:drawable="@drawable/button1_press3" android:duration="200" />
</animation-list>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
button = (ImageView)findViewById(R.id.imageView1);
button.setBackgroundResource(R.drawable.button_anim);
buttonAnim = (AnimationDrawable)button.getBackground();
button.setOnTouchListener(buttonTest);
}
private OnTouchListener buttonTest = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
buttonAnim.start();
// Log.d("Test", "Touch down");
} else if (action == MotionEvent.ACTION_UP) {
buttonAnim.stop();
// Log.d("Test", "Touch Stop");
}
return true;
}
};
默认图片:-button1_inactive.png
【问题讨论】:
标签: android frame drawable ontouchlistener