android - 如何重启 android AnimatedVectorDrawables 动画?

标签 android animation svg android-vectordrawable

我有一个有点复杂的矢量可绘制对象,我想对其进行动画处理。 我用了@RomanNurik's从 svg 创建动画的网络工具

这给了我一个有效的 <animated-vector>根据 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: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();

在 API 23 和 24 中,动画会一次性运行,不会重复。

有什么解决办法吗?我将要放弃并改用一个该死的 png 序列。

最佳答案

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

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

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 - 如何重启 android AnimatedVectorDrawables 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41767676/

相关文章:

android - : java. lang.NoSuchMethodError : okhttp3. internal.Platform.log 导致 Retrofit2 登录出错

iphone - UITableView 通过 JSON 更新,动画行插入

javascript - 严重困扰 WOW.js 并与 animate.css 集成

html - 在 Firefox 中带有基本 href 的 SVG 掩码

c# - 使用 C# 将 SVG 转换为 PNG

android - 等分屏幕宽度和几个布局问题

android - V/FA : Inactivity, 与服务断开连接

Android 应用程序已分配给任何可能的文件 - 应仅分配给我的自定义文件

ios - UIView +animateWithDuration : isn't animating a recently allocated view correctly

javascript - 从水平滚动中卡住一个div,但让它垂直滚动