AnimatedVectorDrawableCompat适配5.0以下安卓

*首先 
VectorDrawableCompat依赖与AAPT的一些功能,它能保持最近矢量图使用的添加的属性ID,以便他们可以被v21之前的引用。

如果你没有启用这个标志,当你在KitKat上运行你的APP你会看到以下(或者类似)错误:



解决办法:启用标志位 

如果你正在用Gradle插件v2.0或者更高,我有一个简洁方法去启用它:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

如果你还没有更新,在用v1.5后者更低的版本,你需要在你的build.gradle文件里添加以下内容:

android {
  defaultConfig {
    
    generatedDensities = []
  }
  
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}


ImageView androidImageView = (ImageView) findViewById(R.id.animated_vector_imgv);
AnimatedVectorDrawableCompat animatedVectorDrawableCompat = AnimatedVectorDrawableCompat.create(this, R.drawable.animated_vector);
androidImageView.setImageDrawable(animatedVectorDrawableCompat);
((Animatable) androidImageView.getDrawable()).start();
 
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_android">

    <target
        android:animation="@animator/up_down"
        android:name="head_eyes" />

    <target
        android:animation="@animator/up_down"
        android:name="arms" />

    <target
        android:animation="@animator/left_right"
        android:name="body_group" />

</animated-vector>
vector_android.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="500px"
    android:height="500px"
    android:viewportHeight="500"
    android:viewportWidth="500">
    <group android:name="vector_drawable">
        <group android:name="head_eyes">
            <path
                android:name="head"
                android:fillColor="#9FBF3B"
                android:pathData="M301.314,83.298l20.159-29.272c1.197-1.74,0.899-4.024-0.666-5.104c-1.563-1.074-3.805-0.543-4.993,1.199L294.863,80.53c-13.807-5.439-29.139-8.47-45.299-8.47c-16.16,0-31.496,3.028-45.302,8.47l-20.948-30.41c-1.201-1.74-3.439-2.273-5.003-1.199c-1.564,1.077-1.861,3.362-0.664,5.104l20.166,29.272c-32.063,14.916-54.548,43.26-57.413,76.34h218.316C355.861,126.557,333.375,98.214,301.314,83.298" />
            <path
                android:name="left_eye"
                android:fillColor="#FF0000"
                android:pathData="M203.956,129.438c-6.673,0-12.08-5.407-12.08-12.079c0-6.671,5.404-12.08,12.08-12.08c6.668,0,12.073,5.407,12.073,12.08C216.03,124.03,210.624,129.438,203.956,129.438" />
            <path
                android:name="right_eye"
                android:fillColor="#0000FF"
                android:pathData="M295.161,129.438c-6.668,0-12.074-5.407-12.074-12.079c0-6.673,5.406-12.08,12.074-12.08c6.675,0,12.079,5.409,12.079,12.08C307.24,124.03,301.834,129.438,295.161,129.438" />
        </group>
        <group android:name="arms">
            <path
                android:name="left_arm"
                android:fillColor="#9FBF3B"
                android:pathData="M126.383,297.598c0,13.45-10.904,24.354-24.355,24.354l0,0c-13.45,0-24.354-10.904-24.354-24.354V199.09c0-13.45,10.904-24.354,24.354-24.354l0,0c13.451,0,24.355,10.904,24.355,24.354V297.598z" />
            <path
                android:name="right_arm"
                android:fillColor="#9FBF3B"
                android:pathData="M372.734,297.598c0,13.45,10.903,24.354,24.354,24.354l0,0c13.45,0,24.354-10.904,24.354-24.354V199.09c0-13.45-10.904-24.354-24.354-24.354l0,0c-13.451,0-24.354,10.904-24.354,24.354V297.598z" />
        </group>
        <group android:name="body_group">
            <path
                android:name="body"
                android:fillColor="#9FBF3B"
                android:pathData="M140.396,175.489v177.915c0,10.566,8.566,19.133,19.135,19.133h22.633v54.744c0,13.451,10.903,24.354,24.354,24.354c13.451,0,24.355-10.903,24.355-24.354v-54.744h37.371v54.744c0,13.451,10.902,24.354,24.354,24.354s24.354-10.903,24.354-24.354v-54.744h22.633c10.569,0,19.137-8.562,19.137-19.133V175.489H140.396z" />
        </group>
    </group>
</vector>


animator/up_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="250"
        android:propertyName="translateY"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="-20"
        android:valueType="floatType" />
</set>

animator/left_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="250"
        android:propertyName="translateX"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="10"
        android:valueTo="-10"
        android:valueType="floatType" />
</set>

运行效果:



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值