在Android Studio中使用.SVG格式的矢量图

相信很多Android开发的小伙伴都被图片资源的OOM头疼过,所以,我们可以考虑引用svg矢量图来分担一些需要适配各种屏幕分辨率并节省资源的方式来处理某些图片。接下来,我将为大家介绍在studio中使用.SVG图片的方式。希望对有需要的小伙伴有益。

  1.首先拿到设计师给出的.SVG形式的图片控制代码(用来合成适量SVG图片)

例如:

<svg width="270px" height="96px" viewBox="0 0 270 96" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <!-- Generator: Sketch 48.2 (47327) - http://www.bohemiancoding.com/sketch -->
    <desc>Created with Sketch.</desc>
    <defs>
        <path d="M-3.06026386e-16,65 L-1.44012417e-16,56 C4.418278,56 8,52.418278 8,48 C8,43.581722 4.418278,40 0,40 L3.06026386e-16,31 C4.418278,31 8,27.418278 8,23 C8,18.6235898 4.48583754,15.0679563 0.125375803,15.0009625 C0.379926721,11.347424 1.01811157,9.51616456 2.00349528,7.67365722 C3.31079847,5.22921278 5.22921278,3.31079847 7.67365722,2.00349528 C10.1181017,0.696192084 12.5427485,4.51389787e-16 19.229139,-7.76880231e-16 L250.770861,7.76880231e-16 C257.457252,-4.51389787e-16 259.881898,0.696192084 262.326343,2.00349528 C264.770787,3.31079847 266.689202,5.22921278 267.996505,7.67365722 C269.303808,10.1181017 270,12.5427485 270,19.229139 L270,33 C261.715729,33 255,39.7157288 255,48 C255,56.2842712 261.715729,63 270,63 L270,76.770861 C270,83.4572515 269.303808,85.8818983 267.996505,88.3263428 C266.689202,90.7707872 264.770787,92.6892015 262.326343,93.9965047 C259.881898,95.3038079 257.457252,96 250.770861,96 L19.229139,96 C12.5427485,96 10.1181017,95.3038079 7.67365722,93.9965047 C5.22921278,92.6892015 3.31079847,90.7707872 2.00349528,88.3263428 C1.01811157,86.4838354 0.379926721,84.652576 0.125375803,80.9990375 C4.48583754,80.9320437 8,77.3764102 8,73 C8,68.581722 4.418278,65 0,65 Z" id="path-1"></path>
    </defs>
    <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <g id="ic_coupon">
            <g id="Rectangle-5-Copy-2">
                <use fill="#FFFFFF" fill-rule="evenodd" xlink:href="#path-1"></use>
                <path stroke="#FF6742" stroke-width="1" d="M0.5,64.5144618 C4.96168974,64.7732707 8.5,68.473427 8.5,73 C8.5,77.466522 5.05212739,81.1360508 0.662968732,81.4745297 C0.906517379,84.4166712 1.41271637,86.1614592 2.44440188,88.0905433 C3.70510517,90.4478536 5.55214644,92.2948948 7.90945665,93.5555981 C10.6051496,94.9972705 13.2084967,95.5 19.229139,95.5 L250.770861,95.5 C256.791503,95.5 259.39485,94.9972705 262.090543,93.5555981 C264.447854,92.2948948 266.294895,90.4478536 267.555598,88.0905433 C268.99727,85.3948504 269.5,82.7915033 269.5,76.770861 L269.5,63.4920882 C261.170992,63.2281357 254.5,56.3931147 254.5,48 C254.5,39.6068853 261.170992,32.7718643 269.5,32.5079118 L269.5,19.229139 C269.5,13.2084967 268.99727,10.6051496 267.555598,7.90945665 C266.294895,5.55214644 264.447854,3.70510517 262.090543,2.44440188 C259.39485,1.00272951 256.791503,0.5 250.770861,0.5 L19.229139,0.5 C13.2084967,0.5 10.6051496,1.00272951 7.90945665,2.44440188 C5.55214644,3.70510517 3.70510517,5.55214644 2.44440188,7.90945665 C1.41271637,9.83854083 0.906517379,11.5833288 0.662968732,14.5254703 C5.05212739,14.8639492 8.5,18.533478 8.5,23 C8.5,27.526573 4.96168974,31.2267293 0.5,31.4855382 L0.5,39.5144618 C4.96168974,39.7732707 8.5,43.473427 8.5,48 C8.5,52.526573 4.96168974,56.2267293 0.5,56.4855382 L0.5,64.5144618 Z"></path>
            </g>
            <path d="M270,33 C261.715729,33 255,39.7157288 255,48 C255,56.2842712 261.715729,63 270,63 L270,76.770861 C270,83.4572515 269.303808,85.8818983 267.996505,88.3263428 C266.689202,90.7707872 264.770787,92.6892015 262.326343,93.9965047 C259.881898,95.3038079 257.457252,96 250.770861,96 L180,96 L180,0 L250.770861,7.76880231e-16 C257.457252,-4.51389787e-16 259.881898,0.696192084 262.326343,2.00349528 C264.770787,3.31079847 266.689202,5.22921278 267.996505,7.67365722 C269.303808,10.1181017 270,12.5427485 270,19.229139 L270,33 Z" id="Rectangle-5-Copy-3" fill="#FF6742"></path>
            <path d="M214.128,54.928 L208.206,54.928 L208.206,52.66 C206.064,54.298 203.754,55.726 201.318,56.902 L199.638,54.34 C204.09,52.408 207.786,49.804 210.684,46.528 L200.94,46.528 L200.94,43.756 L212.826,43.756 C213.792,42.286 214.632,40.732 215.346,39.136 L203.25,39.136 L203.25,36.448 L216.438,36.448 C217.194,34.18 217.74,31.786 218.16,29.266 L221.142,29.644 C220.722,32.08 220.176,34.348 219.504,36.448 L228.03,36.448 L225.888,35.23 C227.232,33.55 228.324,31.618 229.206,29.392 L231.726,30.316 C230.76,32.668 229.584,34.684 228.156,36.448 L234.75,36.448 L234.75,39.136 L218.496,39.136 C217.824,40.774 217.026,42.328 216.186,43.756 L237.06,43.756 L237.06,46.528 L227.862,46.528 C230.004,49.384 233.49,51.988 238.362,54.256 L236.43,56.734 C233.994,55.348 231.936,53.92 230.172,52.492 C230.046,56.188 229.836,59.212 229.542,61.522 C229.206,63.874 228.618,65.512 227.82,66.436 C227.022,67.318 225.762,67.822 224.04,67.906 L217.782,67.906 L216.984,65.092 C218.958,65.176 220.806,65.218 222.57,65.218 C224.334,65.218 225.51,64.588 226.098,63.328 C226.686,61.984 227.064,59.212 227.232,54.928 L216.942,54.928 C216.102,58.078 214.926,60.598 213.456,62.488 C211.65,64.588 208.962,66.52 205.35,68.242 L203.46,65.848 C206.736,64.42 209.214,62.824 210.894,61.102 C212.322,59.506 213.372,57.448 214.128,54.928 Z M214.296,46.528 C212.7,48.628 210.894,50.518 208.878,52.156 L229.794,52.156 C227.61,50.308 225.972,48.46 224.796,46.528 L214.296,46.528 Z M209.256,29.644 C210.432,31.408 211.482,33.256 212.322,35.272 L209.886,36.406 C208.878,34.18 207.828,32.206 206.652,30.484 L209.256,29.644 Z" id="券" fill="#FFFFFF"></path>
        </g>
    </g>
</svg>
  2.接下来打开合成SVG的网站:[http://oss.chengxingyao.cn/svg2android/index.html](http://oss.chengxingyao.cn/svg2android/index.html)

将刚刚拿到的代码丢入到该网站

生成如下图片样式:

代码生成矢量图片.png
3.点击上图的红框框按钮“Download”,将刚刚生成的图片下载到本地以供使用
8C90E1CE-B2BE-4258-9EB6-134CE6F1F31D.png
  4.将生成的图片放入Android studio的drawable文件夹下
image.png

可以看到如上图所示场景

5.接下来就可以直接使用该生成好的矢量图啦
image.png

如上图所示,将文件以常规drawable的形式引入即可看见如图右方生成的图片样式;

好了,大功告成,这样的SVG形式图片显示在Android手机上既不用占用很大的资源,又可以自适应屏幕像素,使其在各种屏幕上都不会失帧,方便简洁,你get到了吗?

【之前看见自己的文章被盗,在这里告诫一下那些小伙伴,请尊重原创,谢谢】

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容