前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android--vector动画

Android--vector动画

作者头像
aruba
发布2020-07-03 11:18:17
1.3K0
发布2020-07-03 11:18:17
举报
文章被收录于专栏:android技术android技术
上次说了SVG在安卓中的应用,在我们安卓系统中SVG就是Vector Drawable,Vector除了显示SVG图片外,还可以做动画效果,效果如下:
首先我们需要一张vector图片
在xml中为如下:
代码语言:javascript
复制
<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="path_check"
        android:fillColor="#FF000000"
        android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
</vector>
Vector拥有很多属性,下面是介绍
代码语言:javascript
复制
path 元素里面的 pathData 就是矢量图的路径数据,除此之外还可以设置其他属性。 path 元素一共包含如下属性:

    android:name 定义该 path 的名字,这样在其他地方可以通过名字来引用这个路径
    android:pathData 和 SVG 中 d 元素一样的路径信息。
    android:fillColor 定义填充路径的颜色,如果没有定义则不填充路径
    android:strokeColor 定义如何绘制路径边框,如果没有定义则不显示边框
    android:strokeWidth 定义路径边框的粗细尺寸
    android:strokeAlpha 定义路径边框的透明度
    android:fillAlpha 定义填充路径颜色的透明度
    android:trimPathStart 从路径起始位置截断路径的比率,取值范围从 0 到1,相对于结束位置
    android:trimPathEnd 从路径结束位置截断路径的比率,取值范围从 0 到1,相对于起始位置
    android:trimPathOffset 设置路径截取的范围 Shift trim region (allows showed region to include the start and end), in the range from 0 to 1.
    android:strokeLineCap 设置路径线帽的形状,取值为 butt, round, square.
    android:strokeLineJoin 设置路径交界处的连接方式,取值为 miter,round,bevel.
    android:strokeMiterLimit 设置斜角的上限,Sets the Miter limit for a stroked path. 注:当strokeLineJoin设置为 “miter” 的时候, 绘制两条线段以锐角相交的时候,所得的斜面可能相当长。当斜面太长,就会变得不协调。strokeMiterLimit 属性为斜面的长度设置一个上限。这个属性表示斜面长度和线条长度的比值。默认是 10,意味着一个斜面的长度不应该超过线条宽度的 10 倍。如果斜面达到这个长度,它就变成斜角了。当 strokeLineJoin 为 “round” 或 “bevel” 的时候,这个属性无效。

根元素 vector 是用来定义这个矢量图的,该元素包含如下属性:

    android:name 定义该drawable的名字
    android:width 定义该 drawable 的内部(intrinsic)宽度,支持所有 Android 系统支持的尺寸,通常使用 dp
    android:height 定义该 drawable 的内部(intrinsic)高度,支持所有 Android 系统支持的尺寸,通常使用 dp
    android:viewportWidth 定义矢量图视图的宽度,视图就是矢量图 path 路径数据所绘制的虚拟画布
    android:viewportHeight 定义矢量图视图的高度,视图就是矢量图 path 路径数据所绘制的虚拟画布
    android:tint 定义该 drawable 的 tint 颜色。默认是没有 tint 颜色的
    android:tintMode 定义 tint 颜色的 Porter-Duff blending 模式,默认值为 src_in
    android:autoMirrored 设置当系统为 RTL (right-to-left) 布局的时候,是否自动镜像该图片。比如 阿拉伯语。
    android:alpha 该图片的透明度属性

有时候我们需要对几个路径一起处理,这样就可以使用 group 元素来把多个 path 放到一起。 group 支持的属性如下:

    android:name 定义 group 的名字
    android:rotation 定义该 group 的路径旋转多少度
    android:pivotX 定义缩放和旋转该 group 时候的 X 参考点。该值相对于 vector 的 viewport 值来指定的。
    android:pivotY 定义缩放和旋转该 group 时候的 Y 参考点。该值相对于 vector 的 viewport 值来指定的。
    android:scaleX 定义 X 轴的缩放倍数
    android:scaleY 定义 Y 轴的缩放倍数
    android:translateX 定义移动 X 轴的位移。相对于 vector 的 viewport 值来指定的。
    android:translateY 定义移动 Y 轴的位移。相对于 vector 的 viewport 值来指定的。

通过上面的属性可以看出, group 主要是用来设置路径做动画的关键属性的。

最后, vector 还支持 clip-path 元素。定义当前绘制的剪切路径。注意,clip-path 只对当前的 group 和子 group 有效。

    android:name 定义 clip path 的名字
    android:pathData 和 android:pathData 的取值一样。

从上面 vector 支持的属性可以看出,功能还是比较丰富的。
例如 三角形,通过 group 可以把其旋转 90度。

XHTML
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="400dp"
    android:height="400dp"
    android:viewportHeight="400"
    android:viewportWidth="400">
    <group
        android:name="name"
        android:pivotX="200"
        android:pivotY="200"
        android:rotation="90">
        <path
            android:fillColor="#FF0000"
            android:pathData="M 100 100 L 300 100 L 200 300 z"
            android:strokeColor="#000000"
            android:strokeWidth="5" />
    </group>
</vector>
我们想要vector动起来,就需要新建一个animated-vector

ic_check_animator_vector.xml

代码语言:javascript
复制
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_check_black_24dp">

    <target
        android:name="path_check"
        android:animation="@anim/ic_check_animation" />

</animated-vector>
要注意的是animated-vector需要一个drawable,就是我们静态的vector图片,target标签中需要指定一个执行动画的对象name,对应我们之前在静态图片中定义的name,再创建一个动画

ic_check_animation.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="500"
        android:propertyName="trimPathEnd"
        android:valueFrom="0"
        android:valueTo="1"
        android:valueType="floatType"></objectAnimator>
</set>
在将设置了动画Vector的ImageView放入Activity布局文件中
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="startAnim"
        app:srcCompat="@drawable/ic_check_animator_vector" />

</android.support.constraint.ConstraintLayout>
代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startAnim(View view) {
        ImageView imageView = (ImageView) view;
        Drawable drawable = imageView.getDrawable();
        ((Animatable) drawable).start();
    }
}
点击ImageView就会执行刚刚写的动画
项目地址:https://gitee.com/aruba/VectorApplication.git
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 上次说了SVG在安卓中的应用,在我们安卓系统中SVG就是Vector Drawable,Vector除了显示SVG图片外,还可以做动画效果,效果如下:
  • 首先我们需要一张vector图片
  • 在xml中为如下:
  • Vector拥有很多属性,下面是介绍
  • 我们想要vector动起来,就需要新建一个animated-vector
  • 要注意的是animated-vector需要一个drawable,就是我们静态的vector图片,target标签中需要指定一个执行动画的对象name,对应我们之前在静态图片中定义的name,再创建一个动画
  • 在将设置了动画Vector的ImageView放入Activity布局文件中
  • 点击ImageView就会执行刚刚写的动画
  • 项目地址:https://gitee.com/aruba/VectorApplication.git
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档