我正在尝试为 ImageButton 制作动画,以便每次单击它时它都会移动一点。但是由于某些原因,动画只工作一次。
代码:
public void ballClicked(View v) {
imgBtn.clearAnimation();
imgBtn.animate().translationX(50).setDuration(500).start();
imgBtn.animate().translationY(50).setDuration(500).start();
oldX = oldX+50;
oldY = oldY+50;
imgBtn.setClickable(false);
// Using this as Animation Listener doesn't trigger when the animation finish
new Handler().postDelayed(new Runnable() {
public void run() {
imgBtn.setX((float)oldX+50);
imgBtn.setY((float)oldY+50);
imgBtn.setClickable(true);
}
}, 500);
}`
最佳答案
这会起作用!
public void ballClicked(View v) {
imgBtn.clearAnimation();
oldX = oldX + 50;
oldY = oldY + 50;
imgBtn.animate().translationX(oldX).setDuration(500).start();
imgBtn.animate().translationY(oldY).setDuration(500).start();
new Handler().postDelayed(new Runnable() {
public void run() {
imgBtn.setX((float) oldX + 50);
imgBtn.setY((float) oldY + 50);
}
}, 500);
}
关于android - 动画只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42031223/