android-View-svg

svg动画效果

1.建立Vector

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
        <path
            android:name="path1"
            android:strokeColor="@color/colorPrimaryDark"
            android:strokeWidth="0.5"
            android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10z"/>

    <path
        android:name="path2"
        android:strokeColor="@color/colorPrimary"
        android:strokeWidth="0.5"
        android:pathData="M3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8z"/>
    <path
        android:name="path3"
        android:strokeColor="@color/colorPrimary"
        android:strokeWidth="0.5"
        android:pathData="M20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5z"/>
    <path
        android:name="path4"
        android:strokeColor="@color/colorPrimaryDark"
        android:strokeWidth="0.5"
        android:pathData="M15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84z"/>

    <path
        android:name="path5"
        android:strokeColor="@color/colorAccent"
        android:strokeWidth="0.5"
        android:pathData="M10,5L9,5L9,4h1v1z"/>
    <path
        android:name="path6"
        android:strokeColor="@color/colorAccent"
        android:strokeWidth="0.5"
        android:pathData="M15,5h-1L14,4h1v1z"/>
</vector>

path代表了每条线的path.PathData是线具体的路径,还能设置线的大小颜色.

2.设置属性动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="5000"
        android:interpolator="@android:interpolator/linear"
        android:propertyName="trimPathEnd"
        android:valueFrom="0"
        android:valueTo="1"
        android:valueType="floatType"
        />
</set>

3.粘合

<?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/ic_android_black_24dp"
    tools:targetApi="lollipop">
    <target
        android:animation="@animator/anim"
        android:name="path1">
    </target>
    <target
        android:animation="@animator/anim"
        android:name="path2">
    </target>
    <target
        android:animation="@animator/anim"
        android:name="path3">
    </target>
    <target
        android:animation="@animator/anim"
        android:name="path4">
    </target>
    <target
        android:animation="@animator/anim"
        android:name="path5">
    </target>
    <target
        android:animation="@animator/anim"
        android:name="path6">
    </target>
</animated-vector>

drawable是目标图片,target是执行的动画,name与之前path的name相对应.

4.在控件中部署

<ImageView
    android:id="@+id/pathDemo_image"
    android:layout_width="143dp"
    android:layout_height="89dp"
    android:src="@drawable/test" />

5.启动动画

val anim = pathDemo_image.drawable as AnimatedVectorDrawable
pathDemo_image.setOnClickListener { anim.start() }

由此可以做到下面效果:

svg文件下载
参考文章

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值