android svg第三方,android接入svg

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

为了解决任我花项目中的loading动画加载出现的内存溢出情况,开始考虑接入svg。另外还可以提高用户体验。

首先,需要讲解两个概念——SVG和Vector。SVG,即Scalable Vector Graphics 矢量图,这种图像格式在前端中已经使用的非常广泛了,详见WIKI

Vector,在Android中指的是Vector Drawable,也就是Android中的矢量图,详见Develop

因此,可以说Vector就是Android中的SVG实现,因为Android中的Vector并不是支持全部的SVG语法,也没有必要,因为完整的SVG语法是非常复杂的,但已经支持的SVG语法已经够用了,特别是Path语法,几乎是Android中Vector的标配,详细可以参考Path语法详解

vector语法简介

Android以一种简化的方式对SVG进行了兼容,这种方式就是通过使用它的Path标签,通过Path标签,几乎可以实现SVG中的其它所有标签,虽然可能会复杂一点,但这些东西都是可以通过工具来完成的,所以,不用担心写起来会很复杂。

Path指令解析如下所示:

1.支持的指令:M = moveto(M X,Y) :将画笔移动到指定的坐标位置

L = lineto(L X,Y) :画直线到指定的坐标位置

H = horizontal lineto(H X):画水平线到指定的X坐标位置

V = vertical lineto(V Y):画垂直线到指定的Y坐标位置

C = curveto(C X1,Y1,X2,Y2,ENDX,ENDY):三次贝赛曲线

S = smooth curveto(S X2,Y2,ENDX,ENDY)

Q = quadratic Belzier curve(Q X,Y,ENDX,ENDY):二次贝赛曲线

T = smooth quadratic Belzier curveto(T ENDX,ENDY):映射

A = elliptical Arc(A RX,RY,XROTATION,FLAG1,FLAG2,X,Y):弧线

Z = closepath():关闭路径

2.使用原则:坐标轴为以(0,0)为中心,X轴水平向右,Y轴水平向下

所有指令大小写均可。大写绝对定位,参照全局坐标系;小写相对定位,参照父容器坐标系

指令和数据间的空格可以省略

同一指令出现多次可以只用一个

注意,’M’处理时,只是移动了画笔, 没有画任何东西。 它也可以在后面给出上同时绘制不连续线。

关于这些语法,开发者需要的并不是全部精通,而是能够看懂即可,其它的都可以交给工具来实现。

以下是我在项目中的小例子

制作Vector Loading图标

这是我项目中效果

loading.gif

首先需要布局文件main.xml<?xml version="1.0" encoding="utf-8"?>

android:layout_width="wrap_content"

android:background="@drawable/loading_pic_19"

android:layout_height="wrap_content">

android:id="@+id/loding_img"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:src="@drawable/loading_vector_drawable_anim"/>

这个xml布局文件需要两个关键的资源文件android:[email protected]/loading_pic_19”

android:[email protected]/loading_vector_drawable_anim”

第一个是格式为png的背景图片loading_pic_19.png

第二个就是xml的vector文件loading_vector_drawable_anim.xml

下边是loading_vector_drawable_anim.xml<?xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

android:drawable="@drawable/loading_vector_drawable">

android:name="star"

android:animation="@animator/star_anim"/>

好,在这里我们看到又需要两个资源文件

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

android:width="164dp"

android:height="164dp"

android:viewportHeight="164"

android:viewportWidth="164">

android:name="star"

android:pathData="M 116.8,89.1 c 14.3 -10, 4.4 -35.3 -16.9 -26.7 c 3.2-23.1-31-26.4-31-1.6c-23-11.6-34.5,20.6-13.4,28.1

c-13.6,2-17.1,13.2-13.7,21.5c3.6,9,15,14.8,26,5c0.5-0.4,0.7-1.1,0.6-1.7C62.6,82.9,76.3,66,89.5,73.2c7.9,4.3,7.1,19.2-5.2,20.5

c-1.2,0.1-2,1.4-1.5,2.5c1.1,2.8,3.6,7.6,8.5,12.7 C111.7,129.7,131.9,102.9,116.8,89.1z"

android:strokeColor="#F73E3E"

android:strokeMiterLimit="10"

android:strokeWidth="3"/>

star_anim.xml(属性动画)<?xml version="1.0" encoding="utf-8"?>

android:duration="1000"

android:propertyName="trimPathStart"

android:repeatCount="infinite"

android:repeatMode="restart"

android:valueFrom="1"

android:valueTo="0"/>

按照下边的路径建一个animator的目录,然后再创建star_anim.xml

%E8%B5%84%E6%BA%90%E8%B7%AF%E5%BE%84.png

准备工作到这里结束了。

下边我们在你的activity里边,找到你的Imageview控件,然后设置监听事件。

点击图片,就可以看到效果了。private ImageView ivSvgLoading;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ivSvgLoading = (ImageView) findViewById(R.id.loding_img);

Drawable drawable = ivSvgLoading.getDrawable();

ivSvgLoading.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if (drawable instanceof Animatable) {

if (!((Animatable) drawable).isRunning()) {

((Animatable) drawable).start();

}

}

}

});

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值