如何重新启动android AnimatedVectorDrawables动画? [英] How to restart android AnimatedVectorDrawables animations?

查看:116
本文介绍了如何重新启动android AnimatedVectorDrawables动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要绘制动画的复杂矢量可绘制对象。
我使用 @RomanNurik的网络工具从svg创建动画

I have a kinda complex vector drawable that I want to animate. I used @RomanNurik's web tool to create the animation from a svg

根据<动画矢量> /reference/android/graphics/drawable/AnimatedVectorDrawable.html rel = noreferrer>文档。这是一个多合一的XML文件。

That gives me a valid <animated-vector> according to the documentatios. It's an "all in one" XML file.

该xml的可绘制对象分为2组,每组包含2个路径,还添加了4个动画,如下所示:

The xml has the drawable divided in 2 groups, each group containing 2 paths and has also added 4 animations, just as follows:

<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 的值,在某些运行后会使动画混乱。因此,方法是以编程方式重复该步骤。足够公平。

Issue number 1:

I cannot use android:repeatCount="infinite" since the ObjectAnimators have different android:duration and android:startOffset values, that would mess up the animation after some runs. So the way to go is repeat it programmatically. Fair enough.

AnimatedVectorDrawableCompat或AnimatedVectorDrawable都没有

Neither AnimatedVectorDrawableCompat or AnimatedVectorDrawable have a method that says animation should loop.

AnimatedVectorDrawableCompat 没有 registerAnimationCallback( ),这样我就可以收听 onAnimationEnd 并重新启动动画了。在这一点上,我放弃了追溯兼容性。

AnimatedVectorDrawableCompat does not have a registerAnimationCallback() so I can listen to onAnimationEnd and restart the animation myself. At this point, I gave up on retrocompatibility.

我目前使用的实现是<$来自 AnimatedVectorDrawable 的c $ c> registerAnimationCallback()仅在android API 25上有效,即使该方法已在API 23中添加

The current implementation I have that uses registerAnimationCallback() from AnimatedVectorDrawable only works at android API 25, even though the methods were added in 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中,动画是单发运行,不会重复。

In API 23 and 24 the animation runs as a one-shot, it does not repeat.

任何想法如何解决这个问题?我要放弃并改用狗屎png序列。

Any ideas how to solve this? I;m about to give up and use a shit png sequence instead.

推荐答案

官方的有效答案在这里: https://issuetracker.google.com/issues/64591234

The official and working answer is here: https://issuetracker.google.com/issues/64591234

此代码可用于> = API 16(可能也是14-15)。我正在使用支持库26.1.0和 vectorDrawables.useSupportLibrary = true (所以我可以在xml中引用矢量可绘制而不会崩溃)

This code works with >= API 16 (probably also 14-15). I'm using support library 26.1.0 and vectorDrawables.useSupportLibrary = true (so I can refer to a vector drawable in xml without crashing)

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动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆