简介
SVG 是一种可缩放的 矢量图形,在软件开发中,使用图片加载如果分辨率不足被拉伸后会导致模糊,尽可能使用矢量图形开发,减少项目大小的同时又保证 UI 的质量。
自己绘制 SVG 推荐使用 Adobe Illustrator、Vectornator、SVG 在线编辑器。
网络资源推荐使用 iconfont、undraw
代码
加载 Path 路径
SVG 又一些基础组件构成,例如 点、线、方形、圆形,而使用最多也是最重要的就是钢笔工具,体现在 SVG 中就是 Path 路径,这个方法就是仅使用钢笔绘制的图形加载,需要把图形左上角对齐后才更方便调整位置。1
2
3<Viewbox Margin="30">
<Path Data="m251,34.2l-164.8,0c-47.5,0 -86.2,38.7 -86.2,86.2l0,164.8c0,47.5 38.7,86.2 86.2,86.2l164.8,0c47.5,0 86.2,-38.7 86.2,-86.2l0,-164.8c0.1,-47.6 -38.6,-86.2 -86.2,-86.2zm-9.9,241.1l-145,0l0,-145l145,0l0,145zm9.9,150.8l-164.8,0c-47.5,0 -86.2,38.7 -86.2,86.2l0,164.8c0,47.5 38.7,86.2 86.2,86.2l164.8,0c47.5,0 86.2,-38.7 86.2,-86.2l0,-164.8c0.1,-47.5 -38.6,-86.2 -86.2,-86.2zm-9.9,241.1l-145,0l0,-145l145,0l0,145zm499.3,-525.4l-116.5,-116.5c-16.3,-16.3 -37.9,-25.3 -61,-25.3c-23,0 -44.7,9 -61,25.3l-116.5,116.5c-33.6,33.6 -33.6,88.3 0,121.9l116.6,116.5c16.3,16.3 37.9,25.3 61,25.3c23,0 44.7,-9 61,-25.3l116.6,-116.6c33.4,-33.4 33.4,-88.1 -0.2,-121.8zm-177.5,163.5l-102.5,-102.5l102.5,-102.5l102.5,102.5l-102.5,102.5zm82.4,120.8l-164.8,0c-47.5,0 -86.2,38.7 -86.2,86.2l0,164.8c0,47.5 38.7,86.2 86.2,86.2l164.8,0c47.5,0 86.2,-38.7 86.2,-86.2l0,-164.8c0,-47.5 -38.7,-86.2 -86.2,-86.2zm-9.9,241.1l-145,0l0,-145l145,0l0,145z" Fill="Black"/>
</Viewbox>
加载 DrawingImage
使用开源库 SvgToXaml 可以更方便的加载 SVG,转换为 DrawingImage 后放在 Imgae 控件中即可。
1 | <Image> |