android绘制矢量图_Android矢量可绘制

本文介绍了如何在Android应用中使用Vector Drawable替代PNG图片,以实现无损缩放和减小APK体积。详细讲解了如何创建和使用Vector Drawable,包括在XML中定义路径、绘制直线、曲线等,并通过示例代码展示了如何创建各种形状,如三角形、W字母、A字母、圆形和JD图标。
摘要由CSDN通过智能技术生成

android绘制矢量图

In this tutorial, we’ll be discussing Android Vector Drawable. Furthermore, we’ll be implementing them in our Android Application.

在本教程中,我们将讨论Android Vector Drawable。 此外,我们将在我们的Android应用程序中实现它们。

Android矢量可绘制 (Android Vector Drawable)

Often we use PNG as our drawable images. In order for the PNG images to work for different screen sizes, we create multiple PNG assets with different sizes and densities. Subsequently, PNG images take up extra space and lead to large APK sizes of the Android Apps.

通常,我们使用PNG作为可绘制图像。 为了使PNG图像适合不同的屏幕尺寸,我们创建了多个具有不同尺寸和密度的PNG素材资源。 随后,PNG图像会占用额外的空间,从而导致Android Apps的APK尺寸变大。

This is where Vector Drawable comes to our rescue! They are your replacement for PNG images.

这就是Vector Drawable拯救我们的地方! 它们是PNG图像的替代品。

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information.

VectorDrawable是在XML文件中定义为一组点,线和曲线及其关联的颜色信息的矢量图形。

They can be scaled according to the screen size without loss in quality. They are rendered quickly onto the screen too. VectorDrawable are an XML file.

可以根据屏幕尺寸缩放它们,而不会降低质量。 它们也可以快速呈现到屏幕上。 VectorDrawable是一个XML文件。

You can add a new Vector Asset in your drawable folder using New | Vector Asset.

您可以使用New | New在您的可绘制文件夹中添加一个新的Vector Asset。 向量资产

Thus we can create Vector drawables of Material Design icons. The code for the VectorDrawable looks like this:

因此,我们可以创建“材质设计”图标的Vector drawables。 VectorDrawable的代码如下所示:

They are set in the vector tag. android:viewportWidth and android:viewportHeight are used in setting the width and height of the drawable bounds. Within these dimensions, the vector drawable is drawn on the canvas.

它们在vector标签中设置。 android:viewportWidthandroid:viewportHeight用于设置可绘制范围的宽度和高度。 在这些尺寸内,可绘制矢量在画布上绘制。

path is the tag that creates the drawable. Inside the path we create lines, curves, arcs and set the border, background color. We do so path commands in the pathData.

path是创建可绘制对象的标签。 在路径内部,我们创建直线,曲线,弧线并设置边框,背景色。 我们在pathData执行path命令。

Vector Drawables were introduced since Android Lollipop and higher but thanks to backward compatibility, they are compatible with earlier versions too.
Vector Drawables自Android Lollipop及更高版本开始引入,但由于向后兼容,它们也与早期版本兼容。

为矢量资产创建路径 (Creating Path for Vector Assets)

The path commands consist of an alphabet followed by coordinates. Imagine creating paths as doing a painting. Uppercase alphabets represent absolute position. Lowe

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值