Android AnimatedVectorDrawable 動畫

AnimatedVectorDrawable可以將SVG變爲動畫

先在drawable目錄下新建

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="20dp"
    android:height="20dp"
    android:tint="#111111"
    android:viewportWidth="60"
    android:viewportHeight="60">

    <path
        android:fillColor="#FFFFFF"
        android:pathData="M 7 54 H 12 V 9 H 7 V 54 Z"/>

    <path
        android:fillColor="#FFFFFF"
        android:pathData="M 21 54 H 26 V 6 H 21 V 54 Z"/>

    <path
        android:fillColor="#FFFFFF"
        android:pathData="M 35 54 H 40 V 6 H 35 V 54 Z"/>

    <path
        android:fillColor="#FFFFFF"
        android:pathData="M 49 54 H 54 V 24 H 49 V 54 Z"/>

</vector>

效果如下所示
在這裏插入圖片描述
接着,新建objectAnimator

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="380"
    android:propertyName="scaleY"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:valueFrom="0.4F"
    android:valueTo="1F"
    android:valueType="floatType">

</objectAnimator>

再drawable目錄下新建animated-vector

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

    <target
        android:name="bar1"
        android:animation="@animator/list_loading_progress_1" />

    <target
        android:name="bar2"
        android:animation="@animator/list_loading_progress_2" />

    <target
        android:name="bar3"
        android:animation="@animator/list_loading_progress_3" />

    <target
        android:name="bar4"
        android:animation="@animator/list_loading_progress_4" />

</animated-vector>  

然後就可以使用了

 <ImageView
        android:id="@+id/img_01"
        android:src="@drawable/animated_vector"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="60dp"
        android:layout_height="60dp" />

java代碼進行調用

ImageView img01 = findViewById(R.id.img_01);
Drawable drawable = img01.getDrawable();
if(drawable instanceof Animatable){
   ((Animatable)drawable).start();
}

效果如下所示
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章