热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android矢量图(VectorDrawable)及动画(AnimatedVectorDrawable)

VectorDrawable矢量图形的优点在这里就不再多介绍了,并且矢量图形在安卓的Lollipop中已经实现了,相关的类就是VectorDrawable。下面先通过一个小例子来看一下Ve

VectorDrawable

矢量图形的优点在这里就不再多介绍了,并且矢量图形在安卓的Lollipop中已经实现了,相关的类就是VectorDrawable。
下面先通过一个小例子来看一下VectorDrawable的运用
layout: main.xml

<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/heart"/>

drawable: heart.xml

<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="250dp"
android:width="250dp"
android:viewportHeight="32"
android:viewportWidth="32" >

<path
android:name="heart"
android:fillColor="#e91e63"
android:pathData="M20.5,9.5
c-1.955,0,-3.83,1.268,-4.5,3
c-0.67,-1.732,-2.547,-3,-4.5,-3
C8.957,9.5,7,11.432,7,14
c0,3.53,3.793,6.257,9,11.5
c5.207,-5.242,9,-7.97,9,-11.5
C25,11.432,23.043,9.5,20.5,9.5z"
/>

vector>

效果:
heart
是不是感到十分神奇,一个心型图案就这样表现出来了,在这个过程中没有使用任何png图片。
其中这里最让人不解的是pathData里面的那些数字,正是这些数字让这个drawable呈现出心形。pathData指的是绘制一个图形所需要的路径信息,那么问题来了,我怎么知道该如何绘制呢?
w3c的文档中详细讲解了绘制的规则:http://www.w3.org/TR/SVG11/paths.html#PathData 。其实在svg格式的图像中也是使用这种规则,而且在安卓中android.graphics.Path Api对路径的定义也差不多是这种规则。
虽然有对path 规则的绘制教程,但是要创造出现有安卓中各种图标的效果是很难的,要让VectorDrawable有实际价值,肯定不能让开发者去想办法实现这些图形的绘制,而是原本就有很多现成的图像可用,8000个已分类好的扁平化图标(PNG/SVG/WEBFONT)http://www.shejidaren.com/8000-flat-icons.html, 从网上的搜索结果来看svg的图标是大有人在。

SVG Path Data

下面对一些常用的命令来做一下介绍:
1. M: move to 移动绘制点
2. L:line to 直线
3. Z:close 闭合
4. C:cubic bezier 三次贝塞尔曲线
5. Q:quatratic bezier 二次贝塞尔曲线
6. A:ellipse 圆弧
参数:
7. M (x y) 移动到x,y
8. L (x y) 直线连到x,y,还有简化命令H(x) 水平连接、V(y)垂直连接
9. Z,没有参数,连接起点和终点
10. C(x1 y1 x2 y2 x y),控制点x1,y1 x2,y2,终点x,y
11. Q(x1 y1 x y),控制点x1,y1,终点x,y
12. A(rx ry x-axis-rotation large-arc-flag sweep-flag x y)
rx ry 椭圆半径
x-axis-rotation x轴旋转角度
large-arc-flag 为0时表示取小弧度,1时取大弧度
sweep-flag 0取逆时针方向,1取顺时针方向

AnimatedVectorDrawable

VectorDrawable也是可以做各种各样的动画的,AnimatedVectorDrawable类可以去创建一个矢量资源的动画。
你通常在三个XML文件中定义矢量资源的动画载体:
1. 元素的矢量资源动画,在res/drawable/(文件夹)
2. 元素的矢量资源,在res/drawable/(文件夹)
3. 元素的一个或多个对象动画器,在res/anim/(文件夹)
矢量资源动画能创建和元素属性的动画。元素定义了一组路径或子组,并且元素定义了要被绘制的路径。
当你想要创建动画时去定义矢量资源,使用android:name属性分配一个唯一的名字给组和路径,这样你可以从你的动画定义中查询到它们。
下面来看两个例子,这两个例子可以从https://github.com/chiuki/animated-vector-drawable中得到。

Clock

MainActivity.java

public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock);

imageView = (ImageView) findViewById(R.id.image);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animate();
}
});
}

private void animate() {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
}
}

布局文件:就一个ImageView,使用矢量动画图像作为source。
layout: activity_clock.xml

<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/nine_to_five"/>

定义一组矢量图形的动画
drawable:nine_to_five.xml

<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/clock" >

<target
android:name="hours"
android:animation="@anim/hours_rotation" />

<target
android:name="minutes"
android:animation="@anim/minutes_rotation" />

animated-vector>

矢量图形
drawable:clock.xml

<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="200dp"
android:width="200dp"
android:viewportHeight="100"
android:viewportWidth="100" >

<group
android:name="minutes"
android:pivotX="50"
android:pivotY="50"
android:rotation="0">

<path
android:strokeColor="@android:color/holo_green_dark"
android:strokeWidth="4"
android:strokeLineCap="round"
android:pathData="M 50,50 L 50,12"/>

group>
<group
android:name="hours"
android:pivotX="50"
android:pivotY="50"
android:rotation="0">

<path
android:strokeColor="@android:color/holo_blue_dark"
android:strokeWidth="4"
android:strokeLineCap="round"
android:pathData="M 50,50 L 24,50"/>

group>
<path
android:strokeColor="@android:color/holo_red_dark"
android:strokeWidth="4"
android:pathData="M 50,50 m -48,0 a 48,48 0 1,0 96,0 a 48,48 0 1,0 -96,0"/>

vector>

动画
anim:hours_rotation.xml

<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="8000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="240"
android:interpolator="@android:anim/linear_interpolator"/>

anim:minutes_rotation.xml

<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360"
android:repeatCount="7"
android:interpolator="@android:anim/linear_interpolator"/>

注意这里的android:propertyName使用rotation,当然在下个例子中我们用的是android:propertyName=”pathData”,可以对比一下。

Face:

layout: activity_face.xml

<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/smiling_face"/>

drawable:smiling_face.xml


<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/face" >

<target
android:name="mouth"
android:animation="@anim/smile" />

animated-vector>

drawable:face.xml

<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="200dp"
android:width="200dp"
android:viewportHeight="100"
android:viewportWidth="100" >

<path
android:fillColor="#ff0"
android:pathData="M 50,50 m -48,0 a 48,48 0 1,0 96,0 a 48,48 0 1,0 -96,0"/>

<path
android:fillColor="@android:color/black"
android:pathData="M 35,40 m -7,0 a 7,7 0 1,0 14,0 a 7,7 0 1,0 -14,0"/>

<path
android:fillColor="@android:color/black"
android:pathData="M 65,40 m -7,0 a 7,7 0 1,0 14,0 a 7,7 0 1,0 -14,0"/>

<path
android:name="mouth"
android:strokeColor="@android:color/black"
android:strokeWidth="4"
android:strokeLineCap="round"
android:pathData="M 30,75 Q 50,55 70,75"/>

vector>

anim:smile.xml

<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:propertyName="pathData"
android:valueFrom="M 30,75 Q 50,55 70,75"
android:valueTo="M 30,65 Q 50,85 70,65"
android:valueType="pathType"
android:interpolator="@android:anim/accelerate_interpolator"/>

注意这里用的是属性是android:propertyName=”pathData”

Reference

http://mobile.51cto.com/news-478709.htm
http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0123/2346.html
http://blog.csdn.net/xu_fu/article/details/44004841

http://www.w3.org/TR/SVG11/paths.html#PathData
https://github.com/chiuki/animated-vector-drawable
https://gist.github.com/nickbutcher/b3962f0d14913e9746f2


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 如何去除Win7快捷方式的箭头
    本文介绍了如何去除Win7快捷方式的箭头的方法,通过生成一个透明的ico图标并将其命名为Empty.ico,将图标复制到windows目录下,并导入注册表,即可去除箭头。这样做可以改善默认快捷方式的外观,提升桌面整洁度。 ... [详细]
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 生成对抗式网络GAN及其衍生CGAN、DCGAN、WGAN、LSGAN、BEGAN介绍
    一、GAN原理介绍学习GAN的第一篇论文当然由是IanGoodfellow于2014年发表的GenerativeAdversarialNetworks(论文下载链接arxiv:[h ... [详细]
author-avatar
ld无痕的心迹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有