Android5.0版本增加的SVG動畫animated-vector,使用時sdk版本大於等於21
使用:第二個svg動畫
1.在drawable/ic_svg2.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportHeight="100"
android:viewportWidth="100">
<group>
<path
android:name="path1"
android:fillColor="#0000"
android:pathData="M20,80L80,80"
android:strokeColor="@android:color/background_dark"
android:strokeLineCap="round"
android:strokeWidth="5" />
<path
android:name="path2"
android:fillColor="#0000"
android:pathData="M20,20L80,20"
android:strokeColor="@android:color/background_dark"
android:strokeLineCap="round"
android:strokeWidth="5" />
<path
android:name="path3"
android:fillColor="#0000"
android:pathData="M20,50L80,50"
android:strokeColor="@android:color/background_dark"
android:strokeLineCap="round"
android:strokeWidth="5" />
</group>
</vector>
2./animator/anim_path21.xml
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="pathData"
android:valueFrom="M20,80L80,80"
android:valueTo="M50,80L80,50"
android:valueType="pathType" />
3./animator/anim_path22.xml
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="pathData"
android:valueFrom="M20,20L80,20"
android:valueTo="M50,20L80,50"
android:valueType="pathType" />
4./drawable/anim_vector2.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_svg2">
<target
android:name="path1"
android:animation="@animator/anim_path21" />
<target
android:name="path2"
android:animation="@animator/anim_path22" />
</animated-vector>
5.在activity中使用
ImageView imageView2 = (ImageView) findViewById(R.id.image2);
imageView2.setImageDrawable(getDrawable(R.drawable.anim_vector2));
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof Animatable){
((Animatable) drawable).start();
}
}
});
示例代碼下載