所以我的问题是如何制作动画在触摸时开始,一旦没有触摸它就会播放另一个动画.. (对不起我的英语..)
main.java:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView is = (ImageView) findViewById(R.id.img1);
is.setBackgroundResource(R.animator.animup);
final AnimationDrawable animup = (AnimationDrawable) is.getBackground();
final ImageView iv = (ImageView) findViewById(R.id.img1);
iv.setBackgroundResource(R.animator.anim);
final AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
is.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
anim.setVisible(true, true);
anim.start();
//perform your animation when button is touched and held
} else if (event.getAction() == MotionEvent.ACTION_UP) {
anim.setVisible(true, true);
animup.start();
//perform your animation when button is released
}
return false;
}
});
};
;
}
+另一个问题...... 已编辑
最佳答案
我认为这个link会帮助你的。
编辑
要实现触摸和释放,您必须使用 onTouchListener
而不是 onClickListener
。
下面是代码-
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//perform your animation when button is touched and held
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//perform your animation when button is released
}
}
};
编辑2
在 else if
block 中,您没有将 anim
的可见性设置为 false。我觉得这就是问题所在。将您的代码重写为这样 -
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
anim.setVisible(true,true);
anim.start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
anim.stop(); //perform your animation when button is released
animup.setVisible(true,true);
animup.start();
}
}
};
关于java - 动画在触摸时开始,在未触摸时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18803805/