Android矢量图VectorDrawable及动画

2024-04-23 01:08

本文主要是介绍Android矢量图VectorDrawable及动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

什么是SVG?

  • SVG 指可伸缩矢量图形 (Scalable Vector Graphics)
  • SVG 用来定义用于网络的基于矢量的图形
  • SVG 使用 XML 格式定义图形
  • SVG 图像在放大或改变尺寸的情况下其图形质量不会有所损失
  • SVG 是万维网联盟的标准
  • SVG 与诸如 DOM 和 XSL 之类的 W3C 标准是一个整体
    上面的介绍来自W3C官网,更多关于SVG的介绍及使用可以参考W3C官网:http://www.w3cschool.cn/index-42.html
    我们在Android绘制使用矢量图形,主要是用到SVG的路径path指令,当然其它的诸如SVG形状,渐变等图形及效果当然也可以用到我们的项目之中。
    Path指令介绍:
    path就指图形路径,通过路径绘制我们需要形状,path绘制图形通常要用到发下对应指令:

  • M = move to 用于移动起始点

  • L = line to 用于画线
  • H = horizontal lineto 用于画水平线
  • V = vertical lineto 用于画竖直线
  • C = curve to 三次贝塞尔曲线
  • S = smooth curve to 更平滑的三次贝塞尔曲线
  • Q = quadratic Belzier curve 二次贝塞尔曲线
  • T = smooth quadratic Belzier curveto 更平滑的二次贝塞尔曲线
  • A = elliptical Arc 用于画弧
  • Z = close path 关闭path
    注释:以上所有命令均允许小写字母。大写表示绝对定位,小写表示相对定位。
    通过学习,写了个Demo,包括画了几中矢量图作为ImageView的背景,还做了一个点击有动画效果的三角形。
    —-相关代码——
/*** @param* @author ldm* @description Android矢量图绘制Vectory及动画AnimatedVector* @time 2016/6/23 9:43*/
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
}

—-Activity对应布局xml——

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.ldm.svgdemo.MainActivity"><TextView
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:text="列举Android矢量图"android:textSize="16sp" /><LinearLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/rect" /><ImageView
            android:layout_width="50dp"android:layout_height="wrap_content"android:src="@drawable/search" /><ImageView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/square" /><ImageView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/triangle" /><ImageView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/pipe" /></LinearLayout><TextView
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:text="Android矢量图动画AnimatedVectorDrawable"android:textSize="16sp" /><CheckBox
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@null"android:button="@drawable/trim_rect" />
</LinearLayout>

—-几种图形的xml绘制代码——

//饼状
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:name="pipe"android:width="64dp"android:height="64dp"android:viewportHeight="600"android:viewportWidth="600">
<path
    android:name="big"android:pathData="M300,200 h-150 a150,150,0,1,0,150,-150z"android:fillColor="#ff0000"android:strokeColor="#0000ff"android:strokeWidth="0"/><path
        android:name="small"android:pathData="M275,175 v-150 a150,150,0 0,0 -150 150z"android:fillColor="#00ffff"android:strokeColor="#0000ff"android:strokeWidth="2"/>
</vector>
//三角形
<?xml version="1.0" encoding="utf-8"?>
<!--android:width 定义几何宽度,尺寸标准随意,一般为dp--><!-- android:height 定义几何高度,尺寸标准随意,一般为dp-->
<!-- android:viewportWidth 定义viewport宽度,viewport是将path绘制在上面的一个虚拟画布--><!-- android:viewportHeight 定义viewport高度-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="64dp"android:height="64dp"android:viewportHeight="600"android:viewportWidth="600"android:name="test"><!--android:name 定义这个VectorDrawable的名字--><!--android:pivotX 定义缩放或者旋转中轴点x坐标,是虚拟画布中的坐标--><!--android:pivotY 定义缩放或者旋转中轴点y坐标,是虚拟画布中的坐标--><group
        android:name="rotationGroup"android:pivotX="300.0"android:pivotY="300.0"android:rotation="45"><!-- android:pathData 定义路径采用了SVG文件里d标签中的path值--><path
            android:name="v"android:fillColor="#ff00ff"android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z"/></group>
</vector>
//搜索图标
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="150dp"android:height="24dp"android:viewportHeight="24"android:viewportWidth="150"><!--  android:strokeColor 定义path的外轮廓颜色--><!-- android:strokeAlpha 路径的透明度--><!--  android:strokeLineCap 设置线的顶路径,圆(round)还是方(square)等--><!--  android:strokeLineJoin 设置线连接处路径方式--><!--  android:strokeMiterLimit 设置线的修饰--><!--  android:strokeWidth 路径的宽度--><path
        android:name="search"android:pathData="M141,17 A9,9 0 1,1 142,16 L149,23"android:strokeAlpha="0.8"android:strokeWidth="2"android:strokeColor="#ff00ff"android:strokeLineCap="round"></path>
</vector>
//正方形
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="72dp"android:height="72dp"android:viewportHeight="500"android:viewportWidth="500"><group
        android:name="rotationGroup"android:pivotX="250.0"android:pivotY="250.0"android:rotation="0"><path
            android:name="square"android:fillColor="#ff0000"android:pathData="M100,100 L400,100 L400,400 L100,400z" /></group>
</vector>
//动画效果xml
<?xml version="1.0" encoding="utf-8"?><!--   animated-selector支持Android版本最低是API=21-->
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android"><item
        android:id="@+id/state_on"android:drawable="@drawable/square"android:state_checked="true" /><item
        android:id="@+id/state_off"android:drawable="@drawable/triangle"android:state_checked="false" /><transition
        android:fromId="@id/state_off"android:toId="@id/state_on"><animated-vector android:drawable="@drawable/square"><target
                android:name="rotationGroup"android:animation="@animator/rotation" /><target
                android:name="triangle"android:animation="@animator/to_triangle" /><target
                android:name="square"android:animation="@animator/colour" /></animated-vector></transition>
</animated-selector>

—动画效果xml——-

//colour.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"android:duration="1000"android:propertyName="fillColor"android:valueFrom="#ff0000"android:valueTo="#00ffff"android:valueType="intType"></objectAnimator>
//rotation.xml
<?xml version="1.0" encoding="utf-8"?><!--android:propertyName指定动画类型--><!--android:valueFrom指定动画开始位置--><!--android:valueTo指定动画结束位置-->
<!--android:duration指定动画时间-->
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"android:duration="1000"android:propertyName="rotation"android:valueFrom="0"android:valueTo="360" />
//to_triangle.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"android:duration="1000"android:propertyName="pathData"android:valueFrom="M100,100 L400,100L400,400L100,400z"android:valueTo="M250,100L250,100L400,400L100,400z"android:valueType="pathType"></objectAnimator>

运行在手机上效果:
这里写图片描述
参考博客:
http://blog.csdn.net/qq_17583407/article/details/50781667
http://blog.csdn.net/jjwwmlp456/article/details/40617495
项目Demo下载地址:http://download.csdn.net/detail/true100/9557290

这篇关于Android矢量图VectorDrawable及动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/927377

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I