相信很多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)
将刚刚拿到的代码丢入到该网站
生成如下图片样式:
3.点击上图的红框框按钮“Download”,将刚刚生成的图片下载到本地以供使用
4.将生成的图片放入Android studio的drawable文件夹下
可以看到如上图所示场景
5.接下来就可以直接使用该生成好的矢量图啦
如上图所示,将文件以常规drawable的形式引入即可看见如图右方生成的图片样式;
好了,大功告成,这样的SVG形式图片显示在Android手机上既不用占用很大的资源,又可以自适应屏幕像素,使其在各种屏幕上都不会失帧,方便简洁,你get到了吗?
【之前看见自己的文章被盗,在这里告诫一下那些小伙伴,请尊重原创,谢谢】