记一次为vector添加alpha动画

时间:2020-05-29
本文章向大家介绍记一次为vector添加alpha动画,主要包括记一次为vector添加alpha动画使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、app—res—drawable右键—new—Aector Asset

(1)Clip Art:可以直接选择编译器提供的图标

(2)Local file:生成xml文件,需要UI提供.svg或.psd文件(阿里的iconfont可以下载svg格式文件)

生成后的文件格式为:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="80dp"
    android:height="80dp"
    android:viewportWidth="1024"
    android:viewportHeight="1024">
    <path
        android:name="target1"
        android:fillColor="#ff4d4f"
        android:pathData="M912,848h-144L768,528c0,-140.8 -115.2,-256 -256,-256s-256,115.2 -256,256v320L112,848c-17.6,0 -32,14.4 -32,32s14.4,32 32,32h800c17.6,0 32,-14.4 32,-32s-14.4,-32 -32,-32zM320,848L320,528c0,-105.6 86.4,-192 192,-192s192,86.4 192,192v320L320,848z" />
    <path
        android:name="target2"
        android:fillColor="#ff4d4f"
        android:pathData="M416,528c-17.6,0 -32,14.4 -32,32v192c0,17.6 14.4,32 32,32s32,-14.4 32,-32V560c0,-17.6 -14.4,-32 -32,-32zM512,208c17.6,0 32,-14.4 32,-32V112c0,-17.6 -14.4,-32 -32,-32s-32,14.4 -32,32v64c0,17.6 14.4,32 32,32zM755.2,273.6l44.8,-44.8c12.8,-12.8 12.8,-32 0,-44.8 -12.8,-12.8 -32,-12.8 -44.8,0l-44.8,44.8c-12.8,12.8 -12.8,32 0,44.8 12.8,12.8 33.6,12.8 44.8,0zM275.2,273.6c12.8,12.8 32,12.8 44.8,0 12.8,-12.8 12.8,-32 0,-44.8l-44.8,-44.8c-12.8,-12.8 -32,-12.8 -44.8,0 -12.8,12.8 -12.8,32 0,44.8l44.8,44.8zM112,480h64c17.6,0 32,-14.4 32,-32s-14.4,-32 -32,-32H112c-17.6,0 -32,14.4 -32,32s14.4,32 32,32zM848,480h64c17.6,0 32,-14.4 32,-32s-14.4,-32 -32,-32h-64c-17.6,0 -32,14.4 -32,32s14.4,32 32,32z" />
</vector>

* path 的 name 为 animated-vecto r中设置的 target 的 name

 2、添加objectAnimator动画

res文件下添加animator文件夹,新建animator_alarm.xml文件

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1500"
    android:propertyName="fillAlpha"
    android:repeatCount="infinite"
    android:repeatMode="restart"
    android:valueFrom="1"
    android:valueTo="0.5"
    android:valueType="floatType" />

* 因为 path 设置透明度的方法为 fillAlpha,所以 propertyName 设置为 fillAlpha

3、Animated-vector资源,VectorDrawable 和 ObjectAnimator的 粘合剂,将动画资源作用到Vector资源的核心(target_alarm.xml)

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:drawable="@drawable/ic_alarm"
    tools:targetApi="lollipop">
    <target
        android:name="target1"
        android:animation="@animator/animator_alarm" />
    <target
        android:name="target2"
        android:animation="@animator/animator_alarm" />
</animated-vector>

* animated-vector 的 drawable 为 生成的 vector 文件,此例中的 ic_alarm.xml 文件

* target 的 name 与为 path 添加的 nane 对应

* target 的 animation 为 objectAnimator动画,此例中的animator_alarm

4、布局文件中添加imageview控件

<androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/iv_alarm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/target_alarm" />

* srcCompat 应为Animated-vector资源,此例中target_alarm.xml

5、使用vector要求在5.0以上手机系统

ImageView ivAlarm = view.findViewById(R.id.iv_alarm);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) ivAlarm.getDrawable();
            if (drawable.isRunning()) {
                drawable.stop();
            } else {
                drawable.start();
            }
        }

* 使用vector需要在gradle/defaultConfig中添加vectorDrawables.useSupportLibrary = true

 defaultConfig {

        ...
        vectorDrawables.useSupportLibrary = true
        ...

    }

* 过程中遇到的问题:

(1) java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.AnimatedVectorDrawable

原因:布局文件中引入的是vector的xml文件,而不是animated-vector的xml文件。

(2) java.lang.IllegalArgumentException: Property: alpha is not supported for FullPath

原因:objectAnimator 的propertyName设置的是 alpha,而不是 fillAlpha

(3) 现在还不太清楚为什么,一样的动画为不同的path要添加不同的target,添加同一个的话,先设置的path动画会失效

原文地址:https://www.cnblogs.com/opiumpoppy/p/12987503.html