如何停止 ImageView 上的每个动画

How to stop every animation on an ImageView

我正在创建一个铃声动画(图像视图旋转 10 度,然后旋转 -10)。我正在设置两个 RotateAnimations 和 AnimationListeners,在两者的 onAnimationEnd 中我在 ImageView 上启动另一个动画。方法如下:

创建时:

final RotateAnimation rotateAnimationLeft = new RotateAnimation(-10, 10, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
final RotateAnimation rotateAnimationRight = new RotateAnimation(10, -10, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

rotateAnimationLeft.setDuration(150);
rotateAnimationRight.setDuration(150);

rotateAnimationLeft.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        ivBell.startAnimation(rotateAnimationRight);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }
});

rotateAnimationRight.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        ivBell.startAnimation(rotateAnimationLeft);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }
});

ivBell.startAnimation(rotateAnimationRight);

我设置了一个要在其中停止动画的 onTouchListener。

ivBell.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int X = (int) event.getRawX();

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                ivBell.clearAnimation();
                break;
        }
    }
}

没用。它需要停止什么?

(或者,如果这是一种错误的方法,我将不胜感激一些我应该如何按铃的建议 :) 谢谢!)

这里的问题是,当您清除当前 运行 的动画时,动画结束会触发下一个动画(开始新的循环)。你需要移除动画监听器(设置为null)然后清除动画,

if(ivBell.getAnimation() != null) {
    ivBell.getAnimation().setAnimationListener(null);
    ivBell.clearAnimation();
}

但在这种情况下,您需要将侦听器存储在一个单独的变量中(以防您想再次使用它)。也许一个更简单的替代方案是只使用一个布尔标志,该标志可用于确定是否应在动画侦听器中启动 'subsequent' 动画。

public class Main2Activity extends AppCompatActivity {

    boolean shouldAnimateFlag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        final ImageView ivBell = findViewById(R.id.ivBell);

        final RotateAnimation rotateAnimationLeft = new RotateAnimation(-10, 10, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        final RotateAnimation rotateAnimationRight = new RotateAnimation(10, -10, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

        rotateAnimationLeft.setDuration(150);
        rotateAnimationRight.setDuration(150);

        rotateAnimationLeft.setAnimationListener(new Animation.AnimationListener() {
            @Override public void onAnimationStart(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) {
                if(shouldAnimateFlag) {
                    ivBell.startAnimation(rotateAnimationRight);
                }
            }

            @Override public void onAnimationRepeat(Animation animation) { }
        });

        rotateAnimationRight.setAnimationListener(new Animation.AnimationListener() {
            @Override public void onAnimationStart(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (shouldAnimateFlag) {
                    ivBell.startAnimation(rotateAnimationLeft);
                }
            }

            @Override public void onAnimationRepeat(Animation animation) { }
        });

        ivBell.startAnimation(rotateAnimationRight);

        ivBell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //The following line will just stop the animation
//                shouldAnimateFlag = false;
                //You can start/stop using the following lines
                if(shouldAnimateFlag) {
                    shouldAnimateFlag = false;
                } else {
                    shouldAnimateFlag = true;
                    ivBell.startAnimation(rotateAnimationRight);
                }
            }
        });
    }
}