2017-04-08 55 views
1

上傳到Vector Drawable Animation,我有Notification Active Vector Drawable中的圖標。如何動畫矢量繪圖中圖標的特定部分?

通知圖標矢量路徑代碼: -

<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" /> 

的圖標會是這樣

enter image description here

想動畫外環線僅使用矢量繪製動畫。可以引導我學習矢量繪製動畫。

回答

3

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" /> 

然後,我們需要給那個角色名稱,以便動畫師可以連接到它,如果你想製作動畫的位置或旋轉它包裝在一個<group>。我用眼睛看着旋轉的樞軸點。

<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()

+0

在XML中它的顯示錯誤像元素動畫矢量沒有必需的屬性android:drawable' – Yugesh

+0

@Yugesh IDE是錯誤的,它會編譯和運行良好。如果您想使用IDE,請將'aapt:attr'位分成不同的資源文件並編寫引用它們的屬性。請參閱https://developer.android.com/guide/topics/resources/complex-xml-resources.html – ephemient

+0

謝謝。完成動畫後不能回到原來的位置。可用的XML屬性返回到原始位置或我們在動畫完成後設置。 – Yugesh