如何在Java中重复播放Android动画?

huangapple go评论69阅读模式
英文:

How to repeat Android animation in java?

问题

private void flipCoin() {
    final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
    final ImageView iv = ((ImageView) findViewById(R.id.imageView));
    iv.setRotationY(0f);
    //iv.animate().setDuration(10);
    iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            iv.setImageDrawable(drawable);
            iv.setRotationY(270f);
            iv.animate().rotationY(360f).setListener(null);

        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }
    });
}
英文:

i have this code here. I want that the Imageview do the animation 5 times. How i need to set the setRepeatCount? How to inilize it?

private void flipCoin() {
    final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
    final ImageView iv = ((ImageView) findViewById(R.id.imageView));
    iv.setRotationY(0f);
    //iv.animate().setDuration(10);
    iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            iv.setImageDrawable(drawable);
            iv.setRotationY(270f);
            iv.animate().rotationY(360f).setListener(null);

        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }
    });

答案1

得分: 3

以下是您要翻译的内容:

方法setRepeatCount属于Animation类,您正在使用Animator

尝试这样做:

int times = 5;

private void flipCoin() {
    final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
    final ImageView iv = findViewById(R.id.imageView);
    iv.setRotationY(0f);
    //iv.animate().setDuration(10);
    final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
    viewPropertyAnimator.rotationY(90f);
    viewPropertyAnimator.setListener(new Animator.AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {

            times--;
            if (times > 0) {
                iv.setImageDrawable(drawable);
                iv.setRotationY(270f);
                viewPropertyAnimator.rotationY(360f);
                viewPropertyAnimator.start(); //重新开始
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }
    });

    viewPropertyAnimator.start(); //初始化
}

[1]: https://developer.android.com/reference/android/view/animation/Animation
[2]: https://developer.android.com/reference/android/animation/Animator
英文:

The method setRepeatCount belongs to Animation class, you are using Animator.

Try this:

int times = 5;

private void flipCoin() {
    final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
    final ImageView iv = findViewById(R.id.imageView);
    iv.setRotationY(0f);
    //iv.animate().setDuration(10);
    final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
    viewPropertyAnimator.rotationY(90f);
    viewPropertyAnimator.setListener(new Animator.AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {

            times--;
            if (times > 0) {
                iv.setImageDrawable(drawable);
                iv.setRotationY(270f);
                viewPropertyAnimator.rotationY(360f);
                viewPropertyAnimator.start(); //Restart
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }
    });

    viewPropertyAnimator.start(); //Init
}

huangapple
  • 本文由 发表于 2020年9月2日 03:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63694574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定