2

因为动画集不提供重复功能,我正在实现自己的重复逻辑。

但我有一个问题。

首先,让我向您展示我的示例代码,如下所示。

我的 MainActivity ->

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Button button;

    private AnimatorSet animatorSet;
    private Repetition repetition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image_view);
        button = (Button) findViewById(R.id.button);

        animatorSet = new AnimatorSet();
        repetition = new Repetition();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnimation();
            }
        });
    }

    private void startAnimation() {
        imageView.post(new Runnable() {
            @Override
            public void run() {
                final ObjectAnimator moveY = ObjectAnimator.ofFloat(imageView, "y", imageView.getY(), imageView.getY() - 200F);
                moveY.setDuration(1000);

                final ObjectAnimator rotation = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);
                rotation.setDuration(1000);

                repetition.repeat(animatorSet, 10);

                List<Animator> list = new ArrayList<>();

                list.add(moveY);
                list.add(rotation);

                // problem is here, if i run a single animator, repetition class can not get onAnmationEnd callback.
                animatorSet.play(moveY);

                // if i run playSequentially with multiple animators it works properly;
//                animatorSet.playSequentially(list);
                animatorSet.start();
            }
        });
    }

}

我对 REPETITION 的支持课程 ->

它正在监听动画师的生命周期,如果动画结束,它会将所需动画重复计数与 onAnimationEnd 回调中的当前重复计数进行比较。如果我运行一个动画师.. onAnimationEnd 不会被调用...

只是第一次调用 onAnimationEnd ..

public class Repetition implements Animator.AnimatorListener {

    private AnimatorSet animatorSet;
    private int repeatCount;

    private int currentRepeatCount;

    public void repeat(AnimatorSet animatorSet, int repeatCount) {
        this.animatorSet = animatorSet;
        this.repeatCount = repeatCount;

        animatorSet.removeAllListeners();
        animatorSet.addListener(this);
    }

    @Override
    public void onAnimationStart(Animator animation) {
        Log.d("RepeatitionLog", "onAnimationStart");
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if(currentRepeatCount < repeatCount) {
            animatorSet.start();
            currentRepeatCount++;
            Log.d("RepeatitionLog", "onAnimationRepeat by repetition");
            Log.d("RepeatitionLog", "currentRepeatCount : " + currentRepeatCount);
        } else {
            Log.d("RepeatitionLog", "onAnimationEnd");
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        Log.d("RepeatitionLog", "onAnimationCancel");
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
        Log.d("RepeatitionLog", "onAnimationRepeat");
    }
}

我会很感激任何帮助!

谢谢。

4

0 回答 0