检测动画何时完成

时间:2020-05-21 00:36:16

标签: java android android-studio

这是我旋转的瓶子动画:

public static final Random RANDOM = new Random();
private int lastAngle = -1;
private ImageView bottle;
private void spinTheBottle() {

        int angle = RANDOM.nextInt(3600 - 360) + 360;
        float pivotX = bottle.getWidth() / 2;
        float pivotY = bottle.getHeight() / 2;

        final Animation animRotate = new RotateAnimation(lastAngle == -1 ? 0 : lastAngle, angle, pivotX, pivotY);
        lastAngle = angle;
        animRotate.setDuration(1500);
        animRotate.setFillAfter(true);

        bottle.startAnimation(animRotate);
    }

如何检测动画何时完成?我尝试添加Animator.AnimatorListener,但这根本不执行:

Animator.AnimatorListener listener = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            Toast.makeText(FlasketutenActivity.this, "onAnimationStart", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            Toast.makeText(FlasketutenActivity.this, "onAnimationEnd", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            Toast.makeText(FlasketutenActivity.this, "onAnimationCancel", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            Toast.makeText(FlasketutenActivity.this, "onAnimationRepeat", Toast.LENGTH_SHORT).show();
        }
    };

有什么提示吗?

3 个答案:

答案 0 :(得分:0)

您似乎正在创建侦听器,但未将其分配给动画。尝试像这样在动画对象上调用setAnimationListener:

animRotate.setAnimationListener(new Animation.AnimationListener() {
    //Listener methods
});

答案 1 :(得分:0)

第一个。使ImageView巫婆的高度保持在零以上的高度

    rotateAnimation.setDuration(1500);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setFillEnabled(true);
        rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.d(TAG, "onAnimationStart: ");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d(TAG, "onAnimationEnd: ");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.d(TAG, "onAnimationRepeat: ");
            }
        });
        view.startAnimation(rotateAnimation);

它正在我的项目中工作。

但是我总是在xmls中使用动画,它易于操作且相对于代码而言。

Animation和Animator是不一样的东西。

仅此而已。

答案 2 :(得分:0)

将您已经创建的听众分配给动画

animRotate.setAnimationListener(listener);