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

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

How to repeat Android animation in java?

问题

  1. private void flipCoin() {
  2. final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
  3. final ImageView iv = ((ImageView) findViewById(R.id.imageView));
  4. iv.setRotationY(0f);
  5. //iv.animate().setDuration(10);
  6. iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {
  7. @Override
  8. public void onAnimationStart(Animator animation) {
  9. }
  10. @Override
  11. public void onAnimationRepeat(Animator animation) {
  12. }
  13. @Override
  14. public void onAnimationEnd(Animator animation) {
  15. iv.setImageDrawable(drawable);
  16. iv.setRotationY(270f);
  17. iv.animate().rotationY(360f).setListener(null);
  18. }
  19. @Override
  20. public void onAnimationCancel(Animator animation) {
  21. }
  22. });
  23. }
英文:

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?

  1. private void flipCoin() {
  2. final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
  3. final ImageView iv = ((ImageView) findViewById(R.id.imageView));
  4. iv.setRotationY(0f);
  5. //iv.animate().setDuration(10);
  6. iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {
  7. @Override
  8. public void onAnimationStart(Animator animation) {
  9. }
  10. @Override
  11. public void onAnimationRepeat(Animator animation) {
  12. }
  13. @Override
  14. public void onAnimationEnd(Animator animation) {
  15. iv.setImageDrawable(drawable);
  16. iv.setRotationY(270f);
  17. iv.animate().rotationY(360f).setListener(null);
  18. }
  19. @Override
  20. public void onAnimationCancel(Animator animation) {
  21. }
  22. });

答案1

得分: 3

以下是您要翻译的内容:

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

尝试这样做:

  1. int times = 5;
  2. private void flipCoin() {
  3. final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
  4. final ImageView iv = findViewById(R.id.imageView);
  5. iv.setRotationY(0f);
  6. //iv.animate().setDuration(10);
  7. final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
  8. viewPropertyAnimator.rotationY(90f);
  9. viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
  10. @Override
  11. public void onAnimationStart(Animator animation) {
  12. }
  13. @Override
  14. public void onAnimationRepeat(Animator animation) {
  15. }
  16. @Override
  17. public void onAnimationEnd(Animator animation) {
  18. times--;
  19. if (times > 0) {
  20. iv.setImageDrawable(drawable);
  21. iv.setRotationY(270f);
  22. viewPropertyAnimator.rotationY(360f);
  23. viewPropertyAnimator.start(); //重新开始
  24. }
  25. }
  26. @Override
  27. public void onAnimationCancel(Animator animation) {
  28. }
  29. });
  30. viewPropertyAnimator.start(); //初始化
  31. }
  32. [1]: https://developer.android.com/reference/android/view/animation/Animation
  33. [2]: https://developer.android.com/reference/android/animation/Animator
英文:

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

Try this:

  1. int times = 5;
  2. private void flipCoin() {
  3. final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
  4. final ImageView iv = findViewById(R.id.imageView);
  5. iv.setRotationY(0f);
  6. //iv.animate().setDuration(10);
  7. final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
  8. viewPropertyAnimator.rotationY(90f);
  9. viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
  10. @Override
  11. public void onAnimationStart(Animator animation) {
  12. }
  13. @Override
  14. public void onAnimationRepeat(Animator animation) {
  15. }
  16. @Override
  17. public void onAnimationEnd(Animator animation) {
  18. times--;
  19. if (times > 0) {
  20. iv.setImageDrawable(drawable);
  21. iv.setRotationY(270f);
  22. viewPropertyAnimator.rotationY(360f);
  23. viewPropertyAnimator.start(); //Restart
  24. }
  25. }
  26. @Override
  27. public void onAnimationCancel(Animator animation) {
  28. }
  29. });
  30. viewPropertyAnimator.start(); //Init
  31. }

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:

确定