如何重启android AnimatedVectorDrawables动画?

我有一个复杂的矢量drawable,我想动画.
我使用 @RomanNurik’s web工具从svg创建动画

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

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

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt">
    <aapt:attr name="android:drawable">
        <vector xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="56dp"
            android:height="56dp"
            android:viewportHeight="56.0"
            android:viewportWidth="56.0">
            <group
                android:name="group_1"
                android:pivotX="25"
                android:pivotY="25">
                <path
                    android:name="path_3_1"
                    ... />
                <path
                    android:name="path"
                    ... />
            </group>
            <group
                android:name="group"
                android:pivotX="25"
                android:pivotY="25">
                <path
                    android:name="path_1"
                    ... />
                <path
                    android:name="path_2"
                    ... />
            </group>
        </vector>
    </aapt:attr>
    <target android:name="path">
        <aapt:attr name="android:animation">
            <set xmlns:android="http://schemas.android.com/apk/res/android">
                <objectAnimator
                    android:name="path"
                    ... />
                <objectAnimator
                    android:name="path"
                  .../>
            </set>
        </aapt:attr>
    </target>
    <target android:name="group_1">
        <aapt:attr name="android:animation">
            <set xmlns:android="http://schemas.android.com/apk/res/android">
                <objectAnimator
                    android:name="group_1"
                    ... />
                <objectAnimator
                    android:name="group_1"
                    ... />
                <objectAnimator
                    android:name="group_1"
                    ... />
                <objectAnimator
                    android:name="group_1"
                    ... />
            </set>
        </aapt:attr>
    </target>
    <target android:name="group">
        <aapt:attr name="android:animation">
            <set xmlns:android="http://schemas.android.com/apk/res/android">
                <objectAnimator
                    android:name="group"
                    ... />
                <objectAnimator
                    android:name="group"
                    ... />
                <objectAnimator
                    android:name="group"
                    ... />
                <objectAnimator
                    android:name="group"
                    ... />
            </set>
        </aapt:attr>
    </target>
    <target android:name="path_3_1">
        <aapt:attr name="android:animation">
            <set xmlns:android="http://schemas.android.com/apk/res/android">
                <objectAnimator
                    android:name="path_3_1"
                    ... />
                <objectAnimator
                    android:name="path_3_1"
                    ... />
            </set>
        </aapt:attr>
    </target>
</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中添加的

AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) context().getDrawable(R.drawable.long_press_anim);
imageView.setImageDrawable(drawable);
drawable.registerAnimationCallback(new Animatable2.AnimationCallback() {
    @Override
    public void onAnimationEnd(Drawable drawable) {
        super.onAnimationEnd(drawable);
        ((AnimatedVectorDrawable) drawable).start();
    }
});
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而不会崩溃)

animatedVector = AnimatedVectorDrawableCompat.create(getContext(),R.drawable.animated_clock);
ringingAlarmImage.setImageDrawable(animatedVector);
final Handler mainHandler = new Handler(Looper.getMainLooper());
animatedVector.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
    @Override
    public void onAnimationEnd(final Drawable drawable) {
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                animatedVector.start();
            }
        });
    }
});
animatedVector.start();

以上是来客网为你收集整理的如何重启android AnimatedVectorDrawables动画?全部内容,希望文章能够帮你解决如何重启android AnimatedVectorDrawables动画?所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。