如何混合ValueAnimator和RotateAnimation

时间:2019-04-29 08:46:58

标签: android animation android-animation

我正在尝试混合ValueAnimator和RotateAnimation。我希望视图围绕其中心旋转时将直线从一个位置移动到另一位置。 当我单独测试它们时,两个动画都可以完美地工作。但是,当我尝试混合它们时,它会变得疯狂。

这是我的密码

rotateAnimation = new RotateAnimation(0.0f, 360.0f,
                Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF,
                 Animation.RELATIVE_TO_SELF);
        rotateAnimation.setRepeatCount(-1);
        rotateAnimation.setDuration(1000);

public void makeAnimationFinal(final View view){

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(animationMovementList.get(animationCounter).offSet, animationMovementList.get(animationCounter).moveTo);
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); // increase the speed first and then decrease
        valueAnimator.setDuration(animationMovementList.get(animationCounter).duration);
        view.setAnimation(rotateAnimation);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float progress = (float) animation.getAnimatedValue();
                if(isXDirection)
                view.setTranslationX(progress);

                else
                    view.setTranslationY(progress);

                if(progress == animationMovementList.get(animationCounter).moveTo){
                    animationCounter++;
                    if(animationCounter >= 4) {
                        animationCounter = 0;
//                        isXDirection = !isXDirection;
                    }

                    isXDirection = !isXDirection;
                    makeAnimationFinal(view);

                }

            }
        });

        valueAnimator.start();
    }

0 个答案:

没有答案