【问题标题】:How to animate the Particular Part of the Icon in Vector Drawable?如何在 Vector Drawable 中为图标的特定部分设置动画?
【发布时间】:2017-04-08 06:29:42
【问题描述】:

我是 Vector Drawable Animation 的新手,因为我在 Vector Drawable 中有 Notification Active 图标。

通知图标矢量路径代码:-

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="140dp"
android:height="140dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
    android:fillColor="@color/dark_grey"
    android:pathData="M7.58,4.08L6.15,2.65C3.75,4.48 2.17,7.3 2.03,10.5h2c0.15,-2.65 1.51,-4.97 3.55,-6.42zM19.97,10.5h2c-0.15,-3.2 -1.73,-6.02 -4.12,-7.85l-1.42,1.43c2.02,1.45 3.39,3.77 3.54,6.42zM18,11c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2v-5zM12,22c0.14,0 0.27,-0.01 0.4,-0.04 0.65,-0.14 1.18,-0.58 1.44,-1.18 0.1,-0.24 0.15,-0.5 0.15,-0.78h-4c0.01,1.1 0.9,2 2.01,2z" />

图标会是这样的

只想使用矢量可绘制动画来制作外环的动画。谁能指导我学习 Vector Drawable 动画。

【问题讨论】:

    标签: android android-animation android-vectordrawable


    【解决方案1】:

    VectorDrawable 路径使用与SVG paths 相同的语法。按照这些说明,您可以看到路径首先描绘出左右环部分,然后是主体和底部位。

    为了使振铃器与主体分开设置动画,请将其拆分成自己的路径。

    <!-- The ringer -->
    <path
        android:fillColor="@color/dark_grey"
        android:pathData="M7.58,4.08L6.15,2.65C3.75,4.48 2.17,7.3 2.03,10.5h2c0.15,-2.65 1.51,-4.97 3.55,-6.42zM19.97,10.5h2c-0.15,-3.2 -1.73,-6.02 -4.12,-7.85l-1.42,1.43c2.02,1.45 3.39,3.77 3.54,6.42z" />
    <!-- The body -->
    <path
        android:fillColor="@color/dark_grey"
        android:pathData="M18,11c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2v-5zM12,22c0.14,0 0.27,-0.01 0.4,-0.04 0.65,-0.14 1.18,-0.58 1.44,-1.18 0.1,-0.24 0.15,-0.5 0.15,-0.78h-4c0.01,1.1 0.9,2 2.01,2z" />
    

    然后我们需要为该部分命名,以便动画师可以附加到它,如果您想为其位置或旋转设置动画,则将其包裹在 &lt;group&gt; 中。我盯着旋转的枢轴点。

    <group android:name="ringer" android:pivotX="12" android:pivotY="10.5">
        <path
            android:fillColor="@color/dark_grey"
            android:pathData="M7.58,4.08L6.15,2.65C3.75,4.48 2.17,7.3 2.03,10.5h2c0.15,-2.65 1.51,-4.97 3.55,-6.42zM19.97,10.5h2c-0.15,-3.2 -1.73,-6.02 -4.12,-7.85l-1.42,1.43c2.02,1.45 3.39,3.77 3.54,6.42z" />
    </group>
    

    现在您可以将其嵌入AnimatedVectorDrawable 并将动画附加到该组。

    <?xml version="1.0" encoding="utf-8"?>
    <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:width="140dp" android:height="140dp"
                android:viewportWidth="24.0" android:viewportHeight="24.0">
                <group android:name="ringer" android:pivotX="12" android:pivotY="10.5">
                    <path
                        android:fillColor="@color/dark_grey"
                        android:pathData="M7.58,4.08L6.15,2.65C3.75,4.48 2.17,7.3 2.03,10.5h2c0.15,-2.65 1.51,-4.97 3.55,-6.42zM19.97,10.5h2c-0.15,-3.2 -1.73,-6.02 -4.12,-7.85l-1.42,1.43c2.02,1.45 3.39,3.77 3.54,6.42z" />
                </group>
                <path
                    android:fillColor="@color/dark_grey"
                    android:pathData="M18,11c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2v-5zM12,22c0.14,0 0.27,-0.01 0.4,-0.04 0.65,-0.14 1.18,-0.58 1.44,-1.18 0.1,-0.24 0.15,-0.5 0.15,-0.78h-4c0.01,1.1 0.9,2 2.01,2z" />
            </vector>
        </aapt:attr>
        <target android:name="ringer">
            <aapt:attr name="android:animation">
                <objectAnimator
                    android:duration="50"
                    android:propertyName="rotation"
                    android:valueFrom="-15"
                    android:valueTo="15"
                    android:repeatCount="infinite"
                    android:repeatMode="reverse"/>
            </aapt:attr>
        </target>
    </animated-vector>
    

    当你希望它运行时,不要忘记在 drawable 上调用 Animatable2#start()

    【讨论】:

    • 在 XML 中显示错误,如 Element animated-vector doesn't have required attribute android:drawable
    • @Yugesh IDE 错误,它会编译并运行良好。如果您想使用 IDE,请将aapt:attr 位取出到单独的资源文件中并写入引用它们的属性。见developer.android.com/guide/topics/resources/…
    • 谢谢。完成后动画不回到原来的位置。一个 XML 属性可用回到原始位置或我们在动画完成后设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多