如何重启android AnimatedVectorDrawables动画?

Fel*_*nde 9 animation svg android android-vectordrawable

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

<animated-vector>根据文件说明,这给了我一个有效.它是一个"一体化"的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>
Run Code Online (Sandbox Code Playgroud)

问题1:

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

问题2:

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

问题3:

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

问题4:

当前实现我有一个使用registerAnimationCallback()AnimatedVectorDrawable只适用于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();
Run Code Online (Sandbox Code Playgroud)

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

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

Car*_*mer 12

官方和工作答案如下: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();
Run Code Online (Sandbox Code Playgroud)


Moh*_*ini 3

我有同样的问题,所以我这样做了:

// setup and set animation
AnimatedVectorDrawableCompat animatedVector = AnimatedVectorDrawableCompat.create(context, R.drawable.listening_vector_anim);
imageView.setImageDrawable(animatedVector);

// loop animation!
new Thread(() -> {
    while (imageView.isAttachedToWindow()) {
        try {
            imageView.post(() -> animatedVector.start());
            Thread.sleep(2000); //depends on your animation duration
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}).start();
Run Code Online (Sandbox Code Playgroud)

当 ImageView 与窗口分离时,该线程将会终止。