• Android之 SVG绘制


    一 SVG介绍

    1.1 SVG(Scalable Vector Graphics)是可缩放矢量图形的缩写,它是一种图形格式,其中形状在XML中指定, 而XML又由SVG查看器呈现。

    1.2 SVG可以区别于位图,放大可以做到不模糊,可以做一些图标,按钮等绘制,但太复杂的话会导致渲染速度慢,占用内存大。适合简单的图形绘制。

    1.3 SVG坐标系也是笛卡尔坐标系,和android绘制坐标系一样,x=0,y=0点在左上角,与正常的图坐标系相比,y轴被反转。随着SVG中y的增加,点、形状等向下移动,而不是向上。坐标系单位默认是像素px,还可以选择其它单位:

    em    The default font size - usually the height of a character.

    ex     The height of the character x

    px     Pixels

    pt     Points (1 / 72 of an inch)

    pc     Picas (1 / 6 of an inch)

    cm   Centimeters

    mm  Millimeters

    in     Inches

    1.4 SVG元素也非常多,如rect,circle,line,path,text等。但android只支持path,由坐标数据描述点和线的位置

    1. <svg xmlns="http://www.w3.org/2000/svg"
    2. xmlns:xlink="http://www.w3.org/1999/xlink">
    3. <svg x="10">
    4. <rect x="10" y="10" height="100" width="100"
    5. style="stroke:#ff0000; fill: #0000ff"/>
    6. svg>
    7. <svg x="200">
    8. <rect x="10" y="10" height="100" width="100"
    9. style="stroke:#009900; fill: #00cc00"/>
    10. svg>
    11. svg>

    二 android种svg的使用

    2.1 上面说了android支持path元素的绘制,而path里面包含以下元素:

    m|M = moveto 移动到某点。
    l|L = lineto 画一条直线到某点。
    h|H = horizontal lineto 画一条水平线到某点。
    v|V = vertical lineto 画一条垂直线到某点。
    q|Q = quadratic Bézier curveto 二次贝塞尔曲线
    t|T = smooth quadratic Bézier curveto 平滑二次贝塞尔曲线
    c|C = curveto 三次贝塞尔曲线
    s|S = smooth curveto 平滑三次贝塞尔曲线
    a|A = elliptical Arc 弧形
    z|Z = closepath 从结束点到开始点画一条直线,形成一个闭合的区域。

    注意:大写表示绝对位置(窗口x=0,y=0的位置),小写表示相对位置(自身的位置)

    2.2  Android种SVG的使用,绘制矩形示例

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="160dp"
    3. android:height="60dp"
    4. android:viewportWidth="160"
    5. android:viewportHeight="60">
    6. <path
    7. android:pathData="M0,0
    8. L160,0
    9. L160,60
    10. L0,60
    11. L0,0Z"
    12. android:fillColor="#8fafdb"/>
    13. vector>

    2.3 绘制尖头矩形

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="160dp"
    3. android:height="60dp"
    4. android:viewportWidth="160"
    5. android:viewportHeight="60">
    6. <path
    7. android:fillColor="#8fafdb"
    8. android:pathData="M0,30
    9. L20,0
    10. L140,0
    11. L160,30
    12. L140,60
    13. L20,60
    14. L0,30Z" />
    15. vector>

    2.3 绘制圆角矩形

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="160dp"
    3. android:height="60dp"
    4. android:viewportWidth="160"
    5. android:viewportHeight="60">
    6. <path
    7. android:pathData="M10,0
    8. L150,0
    9. A10,10 0 0 1 160,10
    10. L160,10
    11. L160,50
    12. A10,10 0 0 1 150,60
    13. L150,60
    14. L10,60
    15. A10,10 0 0 1 0,50
    16. L0,50
    17. L0,10
    18. A10,10 0 0 1 10,0
    19. L10,0Z"
    20. android:fillColor="#8fafdb"/>
    21. vector>

    2.4 绘制圆形。注意:开始坐标和结束坐标一样会绘制不上,所以结束坐标要错开点坐标

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="160dp"
    3. android:height="160dp"
    4. android:viewportWidth="160"
    5. android:viewportHeight="160">
    6. <path
    7. android:pathData="M0,80
    8. A80,80
    9. 0
    10. 1
    11. 1
    12. 0,80.00001Z"
    13. android:fillColor="#8fafdb"/>
    14. vector>

    2.5 绘制一次贝塞尔曲线

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="160dp"
    3. android:height="160dp"
    4. android:viewportWidth="160"
    5. android:viewportHeight="160">
    6. <path
    7. android:pathData="M0,0
    8. Q 80,80 160,0
    9. "
    10. android:fillColor="#8fafdb"/>
    11. <path
    12. android:pathData="M0,0
    13. L80,80 160,0"
    14. android:strokeColor="#ff0000" android:strokeWidth="1"/>
    15. vector>

    红色是辅助线 

    2.5 绘制二次贝塞尔曲线 

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="160dp"
    3. android:height="160dp"
    4. android:viewportWidth="160"
    5. android:viewportHeight="160">
    6. <path
    7. android:pathData="M0,80
    8. Q 40,0 80,80
    9. T160,80"
    10. android:strokeColor="#8fafdb" android:strokeWidth="2"/>
    11. <path
    12. android:pathData="M0,80
    13. L40,0 80,80"
    14. android:strokeColor="#ff0000" android:strokeWidth="1" android:strokeAlpha="0.5"/>
    15. <path
    16. android:pathData="M80,80
    17. L120,160 160,80"
    18. android:strokeColor="#ffff00" android:strokeWidth="1" android:strokeAlpha="0.5"/>
    19. vector>

  • 相关阅读:
    全渠道客服体验:Rocket.Chat 的无缝互动 | 开源日报 No.41
    【Linux】进程状态(含僵尸进程,孤儿进程)
    你听说过苹果的搜索引擎吗?
    Java项目:SSM动漫影视网站系统
    PHP对接百度语音识别技术
    配置本地Maven仓库——IDEA配置本地Maven源
    小程序优化实践
    Python基础入门例程20-NP20 增加派对名单(一)(列表)
    2023年视频号视频下载提取使用教程
    云原生Java架构实战Docker原理说明及使用+idea使用docker插件一键部署(一篇就够了)
  • 原文地址:https://blog.csdn.net/qq_29848853/article/details/132694793