2

AnimatedVectorDrawable

Je me demande si peut créer deux vecteur dessinables dans Android, et l'animer automatiquement du premier vecteur de la deuxième.est-il un moyen d'animer automatiquement VectorDrawable en mettant début et la fin des vecteurs

Quelque chose comme ViewTransiton et TransitionManager.go

+1

La réponse est en grande partie non; vous devez construire vous-même l'animation spécifique. Je suis seulement conscient d'un outil qui fait de l'automatisation: VectAlign https://github.com/bonnyfone/vectalign mais vous obtiendrez des résultats mitigés. –

Répondre

5

Vous avez un vecteur drawable (@ drawable/vector.xml de):

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:viewportWidth="170" 
    android:viewportHeight="170" 
    android:width="500dp" 
    android:height="500dp"> 
    <path 
     android:name="my_vector_path" 
     android:fillColor="#FF000000" 
     android:pathData="M85,40 
          c10,0 20,0 30,0 
          c0,-5 -10,-20 -30,-20 
          c-20,0 -30,15 -30,20 
          c10,0 20,0 30,0" 
    /> 
</vector> 

Créer un vecteur animé (@ drawable/animated_vector.xml) :

<?xml version="1.0" encoding="utf-8"?> 
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
       android:drawable="@drawable/vector" > 
    <target 
     android:name="my_vector_path" 
     android:animation="@anim/vector_animation" 
    /> 
</animated-vector> 

Créer une anim ation (@ anim/vector_animation.xml):

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <objectAnimator 
     android:duration="2000" 
     android:propertyName="pathData" 
     android:valueType="pathType" 
     android:valueFrom="M85,40 
          c10,0 20,0 30,0 
          c0,-5 -10,-20 -30,-20 
          c-20,0 -30,15 -30,20 
          c10,0 20,0 30,0" 
     android:valueTo="M108,35 
         c5.587379,-6.7633 9.348007,-16.178439 8.322067,-25.546439 
         c-8.053787,0.32369 -17.792625,5.36682 -23.569427,12.126399 
         c-5.177124,5.985922 -9.711121,15.566772 -8.48777,24.749359 
         c8.976891,0.69453 18.147476,-4.561718 23.73513,-11.329308"/> 
</set> 

L'animation contient les valeurs from et to de la trajectoire du vecteur.

Enfin, dans le code, démarrer l'animation:

ImageView imageView = (ImageView) findViewById(R.id.image_view); 
AnimatedVectorDrawable animatedVectorDrawable = 
     (AnimatedVectorDrawable) getDrawable(R.drawable.animated_vector); 
imageView.setImageDrawable(animatedVectorDrawable); 
animatedVectorDrawable.start();