如何重启android AnimatedVectorDrawables动画?

前端之家收集整理的这篇文章主要介绍了如何重启android AnimatedVectorDrawables动画?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个复杂的矢量drawable,我想动画.
我使用 @RomanNurik’s web工具从svg创建动画

这给了我一个有效的<动画矢量>根据the documentatios.它是一个“一体化”的XML文件.

xml的drawable分为2组,每组包含2个路径,并且还添加了4个动画,如下所示:

  1. <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:aapt="http://schemas.android.com/aapt">
  3. <aapt:attr name="android:drawable">
  4. <vector xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:width="56dp"
  6. android:height="56dp"
  7. android:viewportHeight="56.0"
  8. android:viewportWidth="56.0">
  9. <group
  10. android:name="group_1"
  11. android:pivotX="25"
  12. android:pivotY="25">
  13. <path
  14. android:name="path_3_1"
  15. ... />
  16. <path
  17. android:name="path"
  18. ... />
  19. </group>
  20. <group
  21. android:name="group"
  22. android:pivotX="25"
  23. android:pivotY="25">
  24. <path
  25. android:name="path_1"
  26. ... />
  27. <path
  28. android:name="path_2"
  29. ... />
  30. </group>
  31. </vector>
  32. </aapt:attr>
  33. <target android:name="path">
  34. <aapt:attr name="android:animation">
  35. <set xmlns:android="http://schemas.android.com/apk/res/android">
  36. <objectAnimator
  37. android:name="path"
  38. ... />
  39. <objectAnimator
  40. android:name="path"
  41. .../>
  42. </set>
  43. </aapt:attr>
  44. </target>
  45. <target android:name="group_1">
  46. <aapt:attr name="android:animation">
  47. <set xmlns:android="http://schemas.android.com/apk/res/android">
  48. <objectAnimator
  49. android:name="group_1"
  50. ... />
  51. <objectAnimator
  52. android:name="group_1"
  53. ... />
  54. <objectAnimator
  55. android:name="group_1"
  56. ... />
  57. <objectAnimator
  58. android:name="group_1"
  59. ... />
  60. </set>
  61. </aapt:attr>
  62. </target>
  63. <target android:name="group">
  64. <aapt:attr name="android:animation">
  65. <set xmlns:android="http://schemas.android.com/apk/res/android">
  66. <objectAnimator
  67. android:name="group"
  68. ... />
  69. <objectAnimator
  70. android:name="group"
  71. ... />
  72. <objectAnimator
  73. android:name="group"
  74. ... />
  75. <objectAnimator
  76. android:name="group"
  77. ... />
  78. </set>
  79. </aapt:attr>
  80. </target>
  81. <target android:name="path_3_1">
  82. <aapt:attr name="android:animation">
  83. <set xmlns:android="http://schemas.android.com/apk/res/android">
  84. <objectAnimator
  85. android:name="path_3_1"
  86. ... />
  87. <objectAnimator
  88. android:name="path_3_1"
  89. ... />
  90. </set>
  91. </aapt:attr>
  92. </target>
  93. </animated-vector>

问题1:

我不能使用android:repeatCount =“infinite”,因为ObjectAnimators有不同的android:duration和android:startOffset值,这会在一些运行后弄乱动画.所以要走的路是以编程方式重复它.很公平.

问题2:

AnimatedVectorDrawableCompat或AnimatedVectorDrawable都没有一个说动画应该循环的方法.

问题3:

AnimatedVectorDrawableCompat没有registerAnimationCallback()所以我可以听onAnimationEnd并自己重启动画.此时,我放弃了逆向兼容性.

问题4:

我使用来自AnimatedVectorDrawable的registerAnimationCallback()的当前实现仅适用于android API 25,即使这些方法是在API 23中添加

  1. AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) context().getDrawable(R.drawable.long_press_anim);
  2. imageView.setImageDrawable(drawable);
  3. drawable.registerAnimationCallback(new Animatable2.AnimationCallback() {
  4. @Override
  5. public void onAnimationEnd(Drawable drawable) {
  6. super.onAnimationEnd(drawable);
  7. ((AnimatedVectorDrawable) drawable).start();
  8. }
  9. });
  10. drawable.start();

在API 23和24中,动画作为一次性运行,不重复.

任何想法如何解决这个问题?我即将放弃并使用一个狗屎png序列.

解决方法

官方和工作答案在这里: https://issuetracker.google.com/issues/64591234

代码适用于> = API 16(可能还有14-15).我正在使用支持库26.1.0和vectorDrawables.useSupportLibrary = true(所以我可以引用xml中的vector drawable而不会崩溃)

  1. animatedVector = AnimatedVectorDrawableCompat.create(getContext(),R.drawable.animated_clock);
  2. ringingAlarmImage.setImageDrawable(animatedVector);
  3. final Handler mainHandler = new Handler(Looper.getMainLooper());
  4. animatedVector.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
  5. @Override
  6. public void onAnimationEnd(final Drawable drawable) {
  7. mainHandler.post(new Runnable() {
  8. @Override
  9. public void run() {
  10. animatedVector.start();
  11. }
  12. });
  13. }
  14. });
  15. animatedVector.start();

猜你在找的Android相关文章