动画只工作一次

时间:2017-02-03 18:58:18

标签: android animation

我试图为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);

    }`

1 个答案:

答案 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);


}