Android自定义控件开发入门与实战(7)SVG动画,2021最新Android笔试题及答案

group标签的几个常用属性:

  1. android:name:组名

  2. android:rotation:指定该组图像的旋转度数

  3. android:pivotX:定义缩放和旋转时该组的X参考点。该值是相对于vector的viewport值来指定的

  4. android:pivotY:定义缩放和旋转该组时的Y参考点。该值是相对于vector的viewport值来指定的

  5. android:scaleX:x轴缩放大小

  6. android:scaleY:y轴缩放大小

  7. android:translateX:指定该组沿X轴平移的距离

  8. android:translateY:指定该组沿y轴平移的距离

如何制作SVG图像

  1. 如果有绘图基础就是用Illustrator或在线SVG工具制作SVG图像,或者通过SVG源文件下载网站下载后进行编辑。

  2. Iconfont

Iconfont是一种比较成熟的SVG解决方案,原理是把想要的矢量图打包成一个.ttf文件,在Android中应用这个ttf文件来加载和指定图标。

Android中引入SVG图像

在Android中是不支持SVG图像解析的,我们必须将SVG图像转化为vector标签描述。

这里有两个方法进行转化:

  • 在线转换

拿到SVG图像后,将其拖入在线转换网站http://inloop.github.io/svg2android/,就可以得到对应的vector标签。

  • 通过Android Studio引入

Android Studio2.0以上后就支持创建SVG图像。在 右键单击drawable - Vector Asset 之后,就可以生成Vector图像了。

动态Vector

前面简述了vector的标签的一些应用,而动态Vector实现的SVG效果才是SVG图像在Android应用中的精髓。

要实现Vector动画,首先需要vector图像和它所对应的的动画。

这里用之前做的那个SVG的一条线。

然后定义一个Animator文件:

<?xml version="1.0" encoding="utf-8"?>

<objectAnimator xmlns:android=“http://schemas.android.com/apk/res/android”

android:propertyName=“trimPathStart”

android:valueFrom=“0”

android:valueTo=“1”

android:duration=“2000”>

这里指定的propertyName为path标签的“trimPathStart”属性,动画效果为从开始到结尾渐渐消失

做好一个vector图像和animator动画后,我们需要将这两个标签关联,Android 提供了关联的标签animated-vector,通过它就能关联动画和SVG了。

<?xml version="1.0" encoding="utf-8"?>

<animated-vector xmlns:android=“http://schemas.android.com/apk/res/android”

android:drawable="@drawable/svg_img1">

<target

android:name=“bar”

android:animation="@animator/anim_trim_start">

animated-vector中的drawable属性指的是svg文件, target标签有两个属性,name为svg中的某个path的name,是对应的,animation为使用的动画。

其中animated-vector下可以有多个target,因为如果vector标签中有多个path、group,这里就可以使用多个target了。

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

接下来在代码中声明动画:

AnimatedVectorDrawableCompat avdCompat = AnimatedVectorDrawableCompat.create(MainActivity.this, R.drawable.line_animated_vector);

iv = findViewById(R.id.iv);

iv.setImageDrawable(avdCompat);

((Animatable) iv.getDrawable()).start();

最后需要在build.gradle脚本中添加对vector兼容性的支持,因为Android APPT是默认是在Lolipop版本以上使用Vector,而在Lolipop版本以前使用Gradle生成相应的PNG图片,所以加了以下属性来关闭这个妥协:

defaultConfig {

vectorDrawables.useSupportLibrary = true

}

最后显示的效果如下:

在这里插入图片描述

接下来跟着书做一个复杂点的SVG动画。该动画是点击输入框后产生一个搜索的图标

首先准备一个SVG图像:

<?xml version="1.0" encoding="utf-8"?>

<vector xmlns:android=“http://schemas.android.com/apk/res/android”

android:width=“150dp”

android:height=“24dp”

android:viewportWidth=“150”

android:viewportHeight=“24”>

<path

android:name=“search”

android:pathData=“M141,17 A9,9 0 1,1 142,16 L149,23”

android:strokeWidth=“2”

android:strokeColor="@android:color/darker_gray"/>

<path

android:name=“bar”

android:trimPathStart=“0”

android:pathData=“M0,23 L149,23”

android:strokeWidth=“1”

android:strokeColor="@android:color/darker_gray"/>

得到如下的图片:

在这里插入图片描述

动画的效果首先让下面横线渐渐消失,然后放大镜图标同时显现,所以这里使用了两个动画。

//下面横线消失的动画

<?xml version="1.0" encoding="utf-8"?>

<objectAnimator xmlns:android=“http://schemas.android.com/apk/res/android”

android:duration=“500”

android:propertyName=“trimPathStart”

android:valueFrom=“0”

android:valueTo=“1”

android:valueType=“floatType”>

//这个是让放大镜从无到有的动画

<?xml version="1.0" encoding="utf-8"?>

<objectAnimator xmlns:android=“http://schemas.android.com/apk/res/android”

android:duration=“500”

android:propertyName=“trimPathEnd”

android:valueFrom=“0”

android:valueTo=“1”

android:valueType=“floatType”>

创建好vector图像和animator后,要做的就是用animated-vector来关联它们,我们这里的animated-vector使用了两个target,分别来对应放大镜和下面的横线:

<?xml version="1.0" encoding="utf-8"?>

<target

android:animation="@animator/anim_search_trim_end"

android:name=“search”/>

<target

android:animation="@animator/anim_bar_trim_start"

android:name=“bar”/>

接下来就是在xml文件中使用这个svg图:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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”

android:background="#f7f8fa"

android:orientation=“vertical”

tools:context=".MainActivity">

<EditText

android:id="@+id/et"

android:layout_width=“150dp”

android:layout_height=“24dp”

android:background="@null"

android:hint=“点击输入” />

<ImageView

android:id="@+id/iv"

android:layout_width=“150dp”

android:layout_height=“24dp” />

这里用的是FrameLayoout,因为要让EditText和ImageView重合。

那么接下来就是在代码中使用这段动画啦(我们在让EditText取得焦点 即我们点击它的时候 实现这段SVG动画):

//一开始imageview获取焦点

iv.setFocusable(true);

iv.setFocusableInTouchMode(true);

iv.requestFocus();

iv.requestFocusFromTouch();

//当EditText获取焦点时开始动画

et.setOnFocusChangeListener(new View.OnFocusChangeListener() {

@Override

public void onFocusChange(View v, boolean hasFocus) {

if (hasFocus) {

AnimatedVectorDrawableCompat animatedVectorDrawableCompat =

AnimatedVectorDrawableCompat.create(MainActivity.this, R.drawable.animated_vector_search);

iv.setImageDrawable(animatedVectorDrawableCompat);

((Animatable) iv.getDrawable()).start();

}

}

});

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值