Android自定义控件开发入门与实战(7)SVG动画,BTAJ面试有关散列(哈希)表的面试题详解

要实现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了。

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

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

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

尾声

一转眼时间真的过的飞快。我们各奔东西,也各自踏上了自己的旅途,但是即使多年不见,也因为这份情谊我们依旧如从前那般“亲密”。不忘初心方得始终。加油吧,程序员们,在我看来35岁,40岁从来不是危机,只要永远不要忘记自己为何踏上征程!

最后需要同款资料的,可以 **私信我点击【学习】**我愿意分享给你!

为了让更多在学习中或者最近要准备面试的朋友们看到这篇文章,希望你们能多多评论,点赞+转发!

加下面V无偿领取!(备注Android)**
[外链图片转存中…(img-tgRJhRQS-1711375655082)]

尾声

一转眼时间真的过的飞快。我们各奔东西,也各自踏上了自己的旅途,但是即使多年不见,也因为这份情谊我们依旧如从前那般“亲密”。不忘初心方得始终。加油吧,程序员们,在我看来35岁,40岁从来不是危机,只要永远不要忘记自己为何踏上征程!

最后需要同款资料的,可以 **私信我点击【学习】**我愿意分享给你!

为了让更多在学习中或者最近要准备面试的朋友们看到这篇文章,希望你们能多多评论,点赞+转发!

再次感谢所有给我提供过题目的朋友们,感谢一路有你!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值