我试图为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);
}`
答案 0 :(得分:1)
这会有效!
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);
}