Android 如何在Drawble动画中使用OnTouchListener
我一直在寻找解决方案,但似乎没有人尝试过,或者可能太简单了,我没有找到 无论如何,我正试图通过Android 如何在Drawble动画中使用OnTouchListener,android,android-studio,frame,drawable,ontouchlistener,Android,Android Studio,Frame,Drawable,Ontouchlistener,我一直在寻找解决方案,但似乎没有人尝试过,或者可能太简单了,我没有找到 无论如何,我正试图通过ImageView来实现按钮按下。我想要的是,ImageView来显示一个默认图像,如果按下(OnTouch)[它有一个MotionEvent.ACTION\u DOWNblock]来运行一个帧动画。释放触摸[具有MotionEvent.ACTION\u UP块]时,它应停止动画并返回默认图像 注意:帧动画是连续重复的,不应包括默认图像作为循环图像之一。(触摸时不应显示按钮上的图像) 现在的问题是,我有
ImageView
来实现按钮按下。我想要的是,ImageView
来显示一个默认图像,如果按下(OnTouch)[它有一个MotionEvent.ACTION\u DOWN
block]来运行一个帧动画。释放触摸[具有MotionEvent.ACTION\u UP
块]时,它应停止动画并返回默认图像
注意:帧动画是连续重复的,不应包括默认图像作为循环图像之一。(触摸时不应显示按钮上的图像)
现在的问题是,我有动画工作,但根据android文档,``标记中的第一项将默认显示。但如果我添加默认图像(按钮未触摸状态)作为第一项,它将显示在循环中。另外,当我释放触摸时,我使用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>
默认图像:-button1\u inactive.png
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;
}
};