Vector Drawable 使用

来源:互联网 发布:道路交通组织优化pdf 编辑:程序博客网 时间:2024/05/17 21:07

title: 「刚仔Android学习之路」之 Vector Drawable 使用
date: 2016-03-08
categories: android笔记

tags: [Android]

VectorDrawable 优点

VectorDrawable 是android真对 svg图片的使用提供的类,svg图的优点呢就是可以进行不丢失清晰度的压缩,只需要使用一套drawable即可,再也不用提供xhdip, hdip ?这么多的图了。

VectorDrawable 使用

Android 5.0 +

vector是在棒棒糖版本推出的,使用的时候,我们可以直接在图片的src目录,或者textview的drawabletop? drawablebottom等等使用。同样的,如果你想在运行时改变drawables,你可以跟以前一样使用setImageResource()方法,这里没有任何改变。

<?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="android">        <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="#FFFFFF"            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="#FFFFFF"            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" />        <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="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" />        <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></vector>

在 res/drawable目录下定义vector文件,之后在ImageView 中的android:src引入即可。

Android 5.0 -

在Android Support Library 23.2中,官方加入了对Vector的向下版本兼容

  • VectorDrawable 支持 API 7 +
  • AnimationVerctorDrawable 支持 API 11 +

在低于5.0的版本使用,需要如下注意,需要在你的项目的build.grade中进行配置:

// Gradle Plugin 2.0+   android {     defaultConfig {       vectorDrawables.useSupportLibrary = true      }   }        

You’ll note this new attribute only exists in the version 2.0 of the Gradle Plugin. If you are using Gradle 1.5 you’ll instead use

// Gradle Plugin 1.5   android {     defaultConfig {       generatedDensities = []    }    // This is handled for you by the 2.0+ Gradle Plugin    aaptOptions {      additionalParameters "--no-version-vectors"    }   }  

使用上面,需要使用标签

app:srcCompat="@drawable/vector_xx"

AnimationVerctorDrawable 使用

<?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/vector_temp">    <target        android:name="rotation"        android:animation="@anim/rotationgroup" />    <target        android:name="v"        android:animation="@anim/path_gorph" /></animated-vector>

我们要针对vector加入动画的话,直接定义animated-vector标签,然后每个target可以加入一种动画。

anim/rotationgroup.xml

<?xml version="1.0" encoding="utf-8"?><objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"  android:duration="6000"  android:propertyName="rotation"  android:valueFrom="0"  android:valueTo="360" />

anim/path_gorph.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <objectAnimator        android:duration="3000"        android:propertyName="pathData"        android:valueFrom="M300,70 l 0,-70 70,70 0,0   -70,70z"        android:valueTo="M300,70 l 0,-70 70,0  0,140 -70,0 z"        android:valueType="pathType" /></set>        

然后我们只需要将对应的drawable文件加入到ImageView 的src之下,之后,在代码调用start开启动画。

@Overridepublic void onWindowFocusChanged(boolean hasFocus) {    super.onWindowFocusChanged(hasFocus);    Drawable drawable = mAnimation.getDrawable();    if (drawable instanceof Animatable) {        ((Animatable) drawable).start();    }}
0 0
原创粉丝点击