一、需要准备工作来支持:
1.需要android-support-library-23.2以上的库支持。
这里我直接使用了25的。
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:support-vector-drawable:25.1.1'
2.版本< API 21时需要支持的话:
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
这个我是放在自己的Application里面的,文档有说:
Please note: this only takes effect in Activities created after this call
二、代码
1.一个静态的vector:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="120dp"
android:height="120dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<group android:name="left">
<path
android:fillColor="#FF000000"
android:pathData="M9.01,14L2,14v2h7.01v3L13,15l-3.99,-4v3"/>
</group>
<group android:name="right">
<path
android:fillColor="#FF000000"
android:pathData="M14.99,13v-3L22,10L22,8h-7.01L14.99,5L11,9l3.99,4"/>
</group>
</vector>
2.放在animator文件夹里面的两个动画文件:
anim_left:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:interpolator="@android:interpolator/anticipate_overshoot"
android:propertyName="translateX"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="-10"
android:valueType="floatType"/>
anim_right:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:interpolator="@android:interpolator/anticipate_overshoot"
android:propertyName="translateX"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="10"
android:valueType="floatType"
>
</objectAnimator>
3.测试的动画文件testanimatedvector.xml,放在drawable下面:
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/staticvector">
<target
android:name="left"
android:animation="@animator/anim_left"/>
<target
android:name="right"
android:animation="@animator/anim_right"/>
</animated-vector>
注意:animated-vector 会报错,但不影响编译。
3.xml中使用:
<ImageView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/testanimatedvector"
/>
4.代码中启动动画:
mImageView = (ImageView) findViewById(R.id.imageview);
AnimatedVectorDrawableCompat animatedVectorDrawableCompat = AnimatedVectorDrawableCompat.create(
this, R.drawable.testanimatedvector
);
if(animatedVectorDrawableCompat!=null) {
mImageView.setImageDrawable(animatedVectorDrawableCompat);
((Animatable) mImageView.getDrawable()).start();
}
转载注明:http://blog.csdn.net/u014614038/article/details/75014652
参考来源:http://www.jianshu.com/p/e3614e7abc03
另外:
svg在线编辑器:http://editor.method.ac/
svg转化为Android Vector Drawable在线工具:http://inloop.github.io/svg2android/