2016-12-05 35 views
0

我有矢量可繪製動畫。它在API 17-24中正常工作,但在API 25中存在問題。動畫運行,但矢量以不同的順序移動。API中的矢量動畫25

這是我的矢量繪製

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
       android:drawable="@drawable/logo_vector_white"> 
    <target 
     android:name="eye" 
     android:animation="@animator/blink_eye" /> 
</animated-vector> 

這裏是動畫

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <objectAnimator 
     android:duration="300" 
     android:propertyName="scaleX" 
     android:repeatCount="1" 
     android:repeatMode="reverse" 
     android:valueTo="0.05" 
     android:valueType="floatType"/> 
    <objectAnimator 
     android:duration="300" 
     android:propertyName="translateX" 
     android:repeatCount="1" 
     android:repeatMode="reverse" 
     android:valueTo="155" 
     android:valueType="floatType"/> 
</set> 

這是我如何運行它

 new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       Drawable animation = logo.getDrawable(); 
       if (animation instanceof Animatable) { 
        ((Animatable) animation).start(); 
       } 
      } 
     }, 300); 

我發現,從API 25日起,AnimatedVectorDrawable runs on RenderThread。如果這導致問題,如何解決它?我也嘗試將所有動畫文件捆綁到單個xml,但得到了相同的結果。

+0

動畫在不同版本上運行的順序是什麼? –

+0

@LewisMcGeary請看我的答案 –

回答

0

我只需要做我的矢量對象的水平縮放動畫(這是閃爍的眼睛,即由X縮放到0,然後返回到以前的狀態)。我不知道樞軸,並使用一個更多的translateX動畫來補償矢量的水平移動。這個解決方法一直運行到API 25,當它開始在RenderThread上執行時。

通過將android:pivotX添加到組中並使用translateX屬性移除objectAnimator來解決此問題。還添加了android:valueFrom =「1」(沒有它在API 25動畫上立即切換到valueTo)。

這裏是完整的XML。

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:aapt="http://schemas.android.com/aapt" 
       xmlns:tools="http://schemas.android.com/tools" 
       tools:ignore="MissingPrefix"> 

    <aapt:attr name="android:drawable"> 
     <vector xmlns:android="http://schemas.android.com/apk/res/android" 
       android:width="292dp" 
       android:height="108dp" 
       android:viewportHeight="108.0" 
       android:viewportWidth="292.0"> 
      <path 
       android:fillColor="#FFFFFF" 
       android:pathData="M7,12L93,12A7,7 0,0 1,100 19L100,34A7,7 0,0 1,93 41L7,41A7,7 0,0 1,0 34L0,19A7,7 0,0 1,7 12z" 
       android:strokeColor="#00000000" 
       android:strokeWidth="1"/> 

      <group 
       android:name="eye" 
       android:pivotX="160"> 
       <path 
        android:name="right_eye" 
        android:fillColor="#FFFFFF" 
        android:pathData="M161.5,26.5m-16.5,0a16.5,16.5 0,1 1,33 0a16.5,16.5 0,1 1,-33 0" 
        android:strokeColor="#00000000" 
        android:strokeWidth="1"/> 
      </group> 

      <path 
       android:fillColor="#FFFFFF" 
       android:pathData="M7,67L93,67A7,7 0,0 1,100 74L100,89A7,7 0,0 1,93 96L7,96A7,7 0,0 1,0 89L0,74A7,7 0,0 1,7 67z" 
       android:strokeColor="#00000000" 
       android:strokeWidth="1"/> 
      <path 
       android:name="left_eye" 
       android:fillColor="#FFFFFF" 
       android:pathData="M161.5,81.5m-16.5,0a16.5,16.5 0,1 1,33 0a16.5,16.5 1,1 1,-33 0" 
       android:strokeColor="#00000000" 
       android:strokeWidth="1"/> 
      <path 
       android:fillColor="#FFFFFF" 
       android:pathData="M241.46,1.54L289.96,50.04A5,5 119.52,0 1,289.96 57.11L278.07,69A5,5 0,0 1,271 69L222.5,20.5A5,5 57.75,0 1,222.5 13.43L234.39,1.54A5,5 86.11,0 1,241.46 1.54z" 
       android:strokeColor="#00000000" 
       android:strokeWidth="1"/> 
      <path 
       android:fillColor="#FFFFFF" 
       android:pathData="M241.5,106L290,57.5A5,5 60.48,0 0,290 50.43L278.11,38.54A5,5 0,0 0,271.04 38.54L222.54,87.04A5,5 112.86,0 0,222.54 94.11L234.43,106A5,5 0,0 0,241.5 106z" 
       android:strokeColor="#00000000" 
       android:strokeWidth="1"/> 
     </vector> 
    </aapt:attr> 

    <target android:name="eye"> 
     <aapt:attr name="android:animation"> 
       <objectAnimator 
        android:duration="300" 
        android:propertyName="scaleX" 
        android:repeatCount="1" 
        android:repeatMode="reverse" 
        android:valueFrom="1" 
        android:valueTo="0" 
        android:valueType="floatType"/> 
     </aapt:attr> 
    </target> 

</animated-vector>