Android动画矢量Drawable无法正常工作

时间:2017-06-28 10:43:11

标签: android animation android-vectordrawable

我已经使用ShapeShifter来创建一个动画矢量drawable,它可以执行一些修剪路径和一些路径数据更改。

结果如下:

<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="24dp"
        android:height="24dp"
        android:viewportWidth="24"
        android:viewportHeight="24">
        <path
            android:name="omino"
            android:pathData="M 9 10 C 7 10 7 10 7 8 C 7 6 7 6 9 6 C 11 6 11 6 11 8 C 11 10 11 10 9 10 L 9 16"
            android:strokeColor="#000000"
            android:strokeAlpha="0.7"
            android:strokeWidth="0.3"
            android:strokeLineCap="round"
            android:strokeLineJoin="round"/>
        <path
            android:name="bilancia"
            android:pathData="M 3 16 L 20 16 M 12 16 L 12 14 L 22 14"
            android:strokeColor="#000000"
            android:strokeAlpha="0.7"
            android:strokeWidth="0.3"
            android:strokeLineCap="round"
            android:strokeLineJoin="round"/>
        <path
            android:name="abox"
            android:pathData="M 13 14 L 13 12 L 19 12 L 19 14 L 13 14"
            android:strokeColor="#000000"
            android:strokeAlpha="0.7"
            android:strokeWidth="0.3"
            android:strokeLineCap="round"
            android:strokeLineJoin="round"/>
        <path
            android:name="braccia"
            android:pathData="M 14 14 L 9 11 L 13 12.5"
            android:strokeColor="#000000"
            android:strokeAlpha="0.7"
            android:strokeWidth="0.3"
            android:strokeLineCap="round"
            android:strokeLineJoin="round"/>
    </vector>
</aapt:attr>
<target android:name="omino">
    <aapt:attr name="android:animation">
        <objectAnimator
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:propertyName="trimPathEnd"
            android:duration="1000"
            android:valueFrom="0"
            android:valueTo="1"
            android:valueType="floatType"
            android:interpolator="@android:interpolator/fast_out_slow_in"/>
    </aapt:attr>
</target>
<target android:name="bilancia">
    <aapt:attr name="android:animation">
        <objectAnimator
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:propertyName="trimPathStart"
            android:duration="1000"
            android:valueFrom="1"
            android:valueTo="0"
            android:valueType="floatType"
            android:interpolator="@android:interpolator/fast_out_slow_in"/>
    </aapt:attr>
</target>
<target android:name="abox">
    <aapt:attr name="android:animation">
        <set xmlns:android="http://schemas.android.com/apk/res/android">
            <objectAnimator
                android:propertyName="trimPathEnd"
                android:duration="1000"
                android:valueFrom="0"
                android:valueTo="1"
                android:valueType="floatType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
            <objectAnimator
                android:propertyName="pathData"
                android:startOffset="2000"
                android:duration="1000"
                android:valueFrom="M 13 12 L 13 10 L 19 10 L 19 12 L 13 12"
                android:valueTo="M 13 14 L 13 12 L 19 12 L 19 14 L 13 14"
                android:valueType="pathType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
            <objectAnimator
                android:propertyName="pathData"
                android:startOffset="500"
                android:duration="1000"
                android:valueFrom="M 13 14 L 13 12 L 19 12 L 19 14 L 13 14"
                android:valueTo="M 13 12 L 13 10 L 19 10 L 19 12 L 13 12"
                android:valueType="pathType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
        </set>
    </aapt:attr>
</target>
<target android:name="braccia">
    <aapt:attr name="android:animation">
        <set xmlns:android="http://schemas.android.com/apk/res/android">
            <objectAnimator
                android:propertyName="trimPathStart"
                android:duration="1000"
                android:valueFrom="1"
                android:valueTo="0"
                android:valueType="floatType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
            <objectAnimator
                android:propertyName="pathData"
                android:startOffset="500"
                android:duration="1000"
                android:valueFrom="M 14 14 L 9 11 L 13 12.5"
                android:valueTo="M 14 12 L 9 11 L 13 10.5"
                android:valueType="pathType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
            <objectAnimator
                android:propertyName="pathData"
                android:startOffset="2000"
                android:duration="1000"
                android:valueFrom="M 14 12 L 9 11 L 13 10.5"
                android:valueTo="M 14 14 L 9 11 L 13 12.5"
                android:valueType="pathType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
        </set>
    </aapt:attr>
</target>

Android工作室不断将动画矢量目标视为错误。但在汇编中一切正常。

我在项目中将此文件导入为drawable。然后在布局中我添加了一个没有任何src的ImageView。

使用此代码,我将动画矢量设置为imageview,然后启动动画:

AnimazioneVettore = (ImageView)view.findViewById(R.id.ImgVectorAnim);
    animatedVector = AnimatedVectorDrawableCompat.create(getContext(), R.drawable.no_abox_animated);
    AnimazioneVettore.setImageDrawable(animatedVector);
    animatedVector.start();

no_abox_animated 是矢量文件。

当我启动应用程序时,没有触发错误,矢量被加载到imageview中并开始动画。

第一个动画&#34;修剪路径结束/开始&#34;。它的工作正常,除了&#34; bilancia&#34;。在这里动画表演了一个糟糕的跳跃&#34;为最后两行制作动画。

在500毫秒时,路径动画应该开始。但没有任何反应。

在200处,最后一个路径应该开始,但它也不起作用。

我正在使用&#39; com.android.support:appcompat-v7:25.3.1&#39;

1 个答案:

答案 0 :(得分:1)

首先是简单的部分。 AnimatedVectorDrawables的路径变形仅添加到版本25.4.0(release notes)中的支持库中。因此,对于您的pathData动画,您不需要更新正在使用的支持库版本。

至于你的“bilancia”问题,当有多个moveTo命令(M)时,这与AnimatedVectorDrawables处理trimPath的方式有关。你可以找到an issue filed here。基本上你的路径是绘制两条线,当它与trimPath结合使用时,它不能很好地工作。我建议将“bilancia”中的两条线分成两条不同的路径并改为动画。

相关问题