动画可绘制XML中的渐变颜色可以吗?

时间:2018-12-07 06:21:34

标签: android android-studio android-animation animatedvectordrawable

我在向量中使用了渐变色。工作正常我想在动画矢量上使用渐变颜色,但无法正常工作。我在XML代码中看不到问题。我不知道这是否可行,还是应该使用其他方法。如果不可能,我想知道为什么。

动画可绘制文件。

<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt">
<aapt:attr name="android:drawable">
    <vector
        android:name="vector"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24"
        android:viewportHeight="24">
        <path
            android:name="path"
            android:pathData="M 1.218 7.957 L 1.218 15.783 L 22.779 15.783 L 22.779 7.957">
            <aapt:attr name="android:fillColor">
                <gradient
                    android:endX="0.0"
                    android:endY="50.0"
                    android:startX="24.0"
                    android:startY="50.0"
                    android:type="linear">
                    <item android:color="#FF70C8BB" android:offset="0.0"/>
                    <item android:color="#FF0964AE" android:offset="1.0"/>
                </gradient>
            </aapt:attr>
        </path>
    </vector>
</aapt:attr>
<target android:name="path">
    <aapt:attr name="android:animation">
        <set>
            <objectAnimator
                android:propertyName="pathData"
                android:duration="500"
                android:valueFrom="M 1.043 0.783 L 1.043 22.957 L 22.957 22.957 L 22.957 0.783"
                android:valueTo="M 7.957 0.783 L 7.957 22.957 L 15.913 22.957 L 15.913 0.783"
                android:valueType="pathType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
            <objectAnimator
                android:propertyName="pathData"
                android:startOffset="500"
                android:duration="500"
                android:valueFrom="M 7.957 0.783 L 7.957 22.957 L 15.913 22.957 L 15.913 0.783"
                android:valueTo="M 7.957 7.957 L 7.957 15.783 L 15.913 15.783 L 15.913 7.957"
                android:valueType="pathType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
            <objectAnimator
                android:propertyName="pathData"
                android:startOffset="1000"
                android:duration="500"
                android:valueFrom="M 7.957 7.957 L 7.957 15.783 L 15.913 15.783 L 15.913 7.957"
                android:valueTo="M 1.043 7.957 L 1.043 15.783 L 22.957 15.783 L 22.957 7.957"
                android:valueType="pathType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
            <objectAnimator
                android:propertyName="pathData"
                android:startOffset="1500"
                android:duration="500"
                android:valueFrom="M 1.043 7.957 L 1.043 15.783 L 22.957 15.783 L 22.957 7.957"
                android:valueTo="M 1.043 0.783 L 1.043 22.957 L 22.957 22.957 L 22.957 0.783"
                android:valueType="pathType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
        </set>
    </aapt:attr>
</target>

1 个答案:

答案 0 :(得分:1)

您可以伪造它。淡入淡出路径,同时以所需的其他渐变淡入另一路径,如下所示:

  1. 复制路径:path2
  2. 在path2中设置另一个渐变
  3. 将path2 fillAlpha设置为0
  4. 复制目标路径为path2的路径动画
  5. 向两个动画集添加一个fillAlpha objectAnimation,其中fillAlpha从1变为0,而path2 fillAlpha从0变为1。

例如:

<objectAnimator
    android:propertyName="fillAlpha"
    android:duration="2000"
    android:valueFrom="1"
    android:valueTo="0"
    android:valueType="floatType"
    android:interpolator="@android:interpolator/fast_out_slow_in"/>

您的完整代码如下:

<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt">
    <aapt:attr name="android:drawable">
        <vector
            android:name="vector"
            android:width="24dp"
            android:height="24dp"
            android:viewportWidth="24"
            android:viewportHeight="24">
            <path
                android:name="path"
                android:pathData="M 1.218 7.957 L 1.218 15.783 L 22.779 15.783 L 22.779 7.957">
                <aapt:attr name="android:fillColor">
                    <gradient
                        android:endX="0.0"
                        android:endY="50.0"
                        android:startX="24.0"
                        android:startY="50.0"
                        android:type="linear">
                        <item android:color="#FF70C8BB" android:offset="0.0"/>
                        <item android:color="#FF0964AE" android:offset="1.0"/>
                    </gradient>
                </aapt:attr>
            </path>
            <path
                android:name="path2"
                android:fillAlpha="0"
                android:pathData="M 1.218 7.957 L 1.218 15.783 L 22.779 15.783 L 22.779 7.957">
                <aapt:attr name="android:fillColor">
                    <gradient
                        android:endX="0.0"
                        android:endY="50.0"
                        android:startX="24.0"
                        android:startY="50.0"
                        android:type="linear">
                        <item android:color="#FFFFC8BB" android:offset="0.0"/>
                        <item android:color="#FF0964FF" android:offset="1.0"/>
                    </gradient>
                </aapt:attr>
            </path>
        </vector>
    </aapt:attr>
    <target android:name="path">
        <aapt:attr name="android:animation">
            <set>
                <objectAnimator
                    android:propertyName="pathData"
                    android:duration="500"
                    android:valueFrom="M 1.043 0.783 L 1.043 22.957 L 22.957 22.957 L 22.957 0.783"
                    android:valueTo="M 7.957 0.783 L 7.957 22.957 L 15.913 22.957 L 15.913 0.783"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="500"
                    android:duration="500"
                    android:valueFrom="M 7.957 0.783 L 7.957 22.957 L 15.913 22.957 L 15.913 0.783"
                    android:valueTo="M 7.957 7.957 L 7.957 15.783 L 15.913 15.783 L 15.913 7.957"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="1000"
                    android:duration="500"
                    android:valueFrom="M 7.957 7.957 L 7.957 15.783 L 15.913 15.783 L 15.913 7.957"
                    android:valueTo="M 1.043 7.957 L 1.043 15.783 L 22.957 15.783 L 22.957 7.957"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="1500"
                    android:duration="500"
                    android:valueFrom="M 1.043 7.957 L 1.043 15.783 L 22.957 15.783 L 22.957 7.957"
                    android:valueTo="M 1.043 0.783 L 1.043 22.957 L 22.957 22.957 L 22.957 0.783"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="fillAlpha"
                    android:duration="2000"
                    android:valueFrom="1"
                    android:valueTo="0"
                    android:valueType="floatType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
            </set>
        </aapt:attr>
    </target>
    <target android:name="path2">
        <aapt:attr name="android:animation">
            <set>
                <objectAnimator
                    android:propertyName="pathData"
                    android:duration="500"
                    android:valueFrom="M 1.043 0.783 L 1.043 22.957 L 22.957 22.957 L 22.957 0.783"
                    android:valueTo="M 7.957 0.783 L 7.957 22.957 L 15.913 22.957 L 15.913 0.783"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="500"
                    android:duration="500"
                    android:valueFrom="M 7.957 0.783 L 7.957 22.957 L 15.913 22.957 L 15.913 0.783"
                    android:valueTo="M 7.957 7.957 L 7.957 15.783 L 15.913 15.783 L 15.913 7.957"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="1000"
                    android:duration="500"
                    android:valueFrom="M 7.957 7.957 L 7.957 15.783 L 15.913 15.783 L 15.913 7.957"
                    android:valueTo="M 1.043 7.957 L 1.043 15.783 L 22.957 15.783 L 22.957 7.957"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="1500"
                    android:duration="500"
                    android:valueFrom="M 1.043 7.957 L 1.043 15.783 L 22.957 15.783 L 22.957 7.957"
                    android:valueTo="M 1.043 0.783 L 1.043 22.957 L 22.957 22.957 L 22.957 0.783"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="fillAlpha"
                    android:duration="2000"
                    android:valueFrom="0"
                    android:valueTo="1"
                    android:valueType="floatType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
            </set>
        </aapt:attr>
    </target>
</animated-vector>