AnimatedVector in Android

AnimatedVectorDrawable ci permette di creare animazioni anche complesse, sia in un unico file o in tre file separati. Basta utilizzare
un VectorDrawable per definire l’animazione
un objectAnimator per definire quale proprietà animare
un animatedVector che collega objectAnimator al VectorDrawable tramite l’elemento<target>
Ecco un esempio:

res/drawable
rotation.xml
<?xml version=”1.0″ encoding=”utf-8″?>

<vector
xmlns:android=”http://schemas.android.com/apk/res/android

android:height=”250dp”
android:width=”250dp”
android:viewportHeight=”50″
android:viewportWidth=”50″>
<group
android:name=”rotation”
android:pivotX=”0.0″
android:pivotY=”20.0″
android:rotation=”45.0″>
<path
android:name=”v”
android:fillColor=”#000000
android:pathData=”M 5 0 h 24 v 24 h-24 z” />
</group>
</vector>

res/animator
path_morph.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<set
xmlns:android=”http://schemas.android.com/apk/res/android“>

<objectAnimator
android:duration=”700″
android:propertyName=”pathData”
android:repeatCount=”infinite”
android:valueFrom=”M 5 0 h 24 v 24 h-24 z”
android:valueTo=”M 5 0 h 24 v 0 h-24 z”
android:valueType=”pathType”/>

</set>

res/drawable
vector_drawable.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<animated-vector
xmlns:android=”http://schemas.android.com/apk/res/android

   android:drawable=”@drawable/rotation”>
   <target
  android:name=”v”
  android:animation=”@animator/path_morph”/>
</animated-vector>

activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android
xmlns:app=”http://schemas.android.com/apk/res-auto
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:orientation=”vertical”>

<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/img”
app:srcCompat=”@drawable/vector_drawable”/>

</LinearLayout>

MainActivity.java
package com.example.animatedVectorDrawable.activities;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.example.animatedVectorDrawable.R;
import android.widget.*;
import android.graphics.drawable.*;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

ImageView img=findViewById(R.id.img);


AnimatedVectorDrawable drawable = (AnimatedVectorDrawable)
getDrawable(R.drawable.vector_drawable);
img.setImageDrawable(drawable);
drawable.start();

   }

}