Android SVG动画animated-vector使用

来源:互联网 发布:罗莱蚕丝被 知乎 编辑:程序博客网 时间:2024/05/29 11:56

这里写图片描述
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();                }            }        });

示例代码下载

原创粉丝点击