【Android】【动画】矢量动画AnimatedVectorDrawable

定义矢量路径

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="70dp"
    android:height="30dp"
    android:viewportWidth="70"
    android:viewportHeight="30">

    <path
        android:name="search"
        android:pathData="M55,15.1
        A5,5 0 1,1 55,15
        L65,25
        L0,25"
        android:strokeWidth="2"
        android:strokeAlpha="0.8"
        android:strokeColor="@color/color_orange"
        android:strokeLineCap="round" />
</vector>

定义路径动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:propertyName="trimPathEnd"
        android:valueFrom="0"
        android:valueTo="1"
        android:valueType="floatType" />
</set>

将动画应用到矢量图

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

    <target
        android:name="search"
        android:animation="@anim/anim_fade_out" />
</animated-vector>

将最终的animated-vector设置为控件背景,在代码中启动

            AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) v.getBackground();
            drawable.start();

猜你喜欢

转载自blog.csdn.net/u013718730/article/details/88875456