向量中断和非向量中断_向量在android中断言

向量中断和非向量中断

为什么要在Android中使用Vector资源? (Why you should use Vector assets in Android?)

In present Android development, we use bitmap format resource files like png or jpg. This type of resource files are suitable for Android development at the beginning of the era. As time passes, Android phones started to come out in different shapes and sizes. Moreover, with different densities.

在目前的Android开发中,我们使用位图格式的资源文件,例如png或jpg。 这类资源文件适用于时代初期的Android开发。 随着时间的流逝,Android手机开始以不同的形状和尺寸问世。 而且,具有不同的密度。

To make resources perfectly work on all the devices, we started to include different versions of the same file.

为了使资源在所有设备上都能正常工作,我们开始包含同一文件的不同版本。

Image for post

It turns out all these resources have intrinsic sizes, and their quality is dependent on pixels, which is not ideal for today’s Android devices.

事实证明,所有这些资源都有内在的大小,其质量取决于像素,这对于当今的Android设备而言并不理想。

An ideal solution to this problem is to have a single resource file that shouldn’t have multiple copies, and it should be able to scale its quality based on the device. This is what vector drawables are created to do.

解决此问题的理想方法是拥有一个不应具有多个副本的资源文件,并且该资源文件应能够根据设备扩展其质量。 这就是创建矢量可绘制对象的步骤。

Image for post

矢量资产的优势 (Advantages of Vector Assets)

The first advantage will be no multiple files of the same resource. Along with that, they’re sharp, small, and animatable.

第一个优点是不会有相同资源的多个文件。 除此之外,它们还清晰,小巧且具有动画效果。

尖锐 (Sharp)

They’re sharp because they can be resized to any screen size, and their quality is not dependent on pixels of the resources file. Vector assets are textual so that they can be compressed very well.

它们很敏锐,因为可以将它们调整为任何屏幕尺寸,并且它们的质量不取决于资源文件的像素。 矢量资产是文本的,因此可以很好地进行压缩。

(Small)

As we no longer including multiple files of the same resource, the size will be small.

由于我们不再包含相同资源的多个文件,因此大小会很小。

可动画的 (Animatable)

This is the new thing you can do with vector drawables that you can’t achieve with bitmap resource files. We can define the paths in code and animate them dynamically. Have a look

这是可以使用位图资源文件无法实现的矢量可绘制对象进行的新操作。 我们可以在代码中定义路径并动态设置它们的动画。 看一看

Image for post

矢量资产如何工作 (How Vector Assets Work)

At the compile-time, your vector asset will be scaled down to byte code. So your app needs to perform some operations that generates a model object from byte code, which can be inflated on the screen. Then your application uses some canvas operations to draw individual parts of the vector asset on screen. If the vector asset is not animated vector, then it’ll be stored in the cache, to speed up the process.

在编译时,向量资产将按比例缩小为字节码。 因此,您的应用程序需要执行一些操作,这些操作从字节码生成模型对象,然后可以在屏幕上放大该对象。 然后,您的应用程序使用一些画布操作在屏幕上绘制矢量资产的各个部分。 如果矢量资产不是动画矢量,则将其存储在缓存中以加快处理速度。

Image for post

On the other hand, bitmap resource files don’t have any of these steps to load the file on the screen. Your app just decodes the bitmap and draw on the screen.

另一方面,位图资源文件没有任何这些步骤可将文件加载到屏幕上。 您的应用仅解码位图并在屏幕上绘制。

Image for post

In the beginning, the Android phones aren’t that powerful, so using bitmaps seems correct at that time. But in today’s world, Android phones got upgraded, and now is the time for us(AndroidDev) to upgrade assets in the projects as the advantages of the vector assets out weight those of bitmap assets.

刚开始,Android手机的功能并不强大,因此当时使用位图似乎是正确的。 但是在当今世界,Android手机得到了升级,现在是时候让我们(AndroidDev)升级项目中的资产了,因为矢量资产的优势超过了位图资产的优势。

矢量可绘制格式 (Vector drawable Format)

Vector drawable is an XML file with root node as vector and has inner nodes like path, stroke, solid, and more to draw parts of the assert and assign properties like colors to them. Vector assets can have one or more path nodes to draw different parts of the assert. Have a look at simple vector assert file

Vector drawable是一个XML文件,其根节点为矢量,并具有路径,笔触,实心等内部节点,以绘制断言的各个部分并为其分配诸如颜色的属性。 向量资产可以具有一个或多个路径节点以绘制断言的不同部分。 看看简单的向量断言文件

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:tint="#FFFFFF"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">


    <path
        android:strokeWidth="2"
        android:strokeLineCap="square"
        android:strokeColor="@color/white_black"
        android:pathData="M6.4,6.4 L17.6,17.6 M6.4,17.6 L17.6,6.4" />


</vector>

The vector node has width and height attributes in the above code. This will be the size of the vector when you put it in the imageview with height and width as wrap_content. viewportWidth and viewportHeight are the measurements to draw paths by the canvas. Where as the view-port and intrinsic dimensions can be different, but they should be of the same aspect ratio.

在上面的代码中,矢量节点具有width和height属性。 这是向量的大小,当您将其放在imageview ,其高度和宽度为wrap_contentviewportWidthviewportHeight是画布绘制路径的度量。 视口和固有尺寸可以不同,但​​是它们的纵横比应该相同。

如何创建矢量资产? (How to Create a Vector Asset?)

Now let’s see how to draw a path in vector drawable, it’s pretty much straightforward, at tops, I found there are five things you should learn before you start drawing vectors.

现在让我们看看如何在矢量可绘制对象中绘制路径,这非常简单,最重要的是,我发现在开始绘制矢量之前应该学习五件事。

M — means move the pen to the specified point.

M-表示笔移到指定点。

L — means draw a line from the present point of the pen to the specified point.

L —表示从笔的当前点到指定点画一条线

C — means draw a curve from the present point of the pen to the specified point.

C-表示从笔的当前点到指定点绘制曲线

A — means draw an Arc from the present point of the pen to the specified point.

A-表示从笔的当前点到指定点画一条

Z — means close, ending of the present path.

Z —表示当前路径的close ,结束。

Now let’s see a simple example: let’s draw a cross mark using vector drawables. First, have a look at our end goal and the code I’ve used to achieve, then I’ll explain how to achieve it step by step.

现在让我们看一个简单的例子:让我们使用矢量可绘制对象绘制一个十字标记。 首先,看一下我们的最终目标和我用来实现的代码,然后我将逐步解释如何实现它。

Image for post
Vector assert code to draw cross mark
向量断言代码以绘制十字标记

Here we use the path node to draw the path and the attributes like strokewidth, strokeColor to assign different properties to the path like width, color, and lineCap.

在这里,我们使用路径节点绘制路径,并使用strokewidthstrokeColor属性为路径分配不同的属性,例如width,color和lineCap。

strokeWidth: This attribute is used to mention the size of the stroke(here size of the lines).strokeLineCap: This attribute is used to design how the endpoint of the stroke should look like in a square or rounded. Have a look at available types.

strokeWidth:此属性用于提及笔划的大小(此处为行的大小)。 strokeLineCap:此属性用于设计笔触的端点应为正方形或圆形的外观。 看一下可用的类型。

Image for post
Image for post
Image for post
strokeLineCap attribute strokeLineCap属性的圆形,正方形和对接样式

strokeColor: This attribute is used to mention the color of the actual path mentioned in the path attribute.

strokeColor:此属性用于提及path属性中提及的实际路径的颜色。

路径 (Path)

Now it’s time to see the path attribute, First, have a look at the total path.

现在是时候查看path属性了,首先,查看总路径。

M6.4,6.4 L17.6,17.6 M6.4,17.6 L17.6,6.4

Let’s see whats happens when we draw point by point.

让我们看看当逐点绘制时会发生什么。

M6.4, 6.4 -This will move the pen from 0,0 co-ordinates to 6.4,6.4. As it wasn’t drawing anything, the preview will be empty. Have a look:

M6.4、6.4-这会将笔从0,0坐标移动到6.4,6.4。 由于未绘制任何内容,因此预览将为空。 看一看:

Image for post

L17.6,17.6 -This will draw a line from the present point, which is 6.4,6.4 to 17.6,17.6. Have a look:

L17.6,17.6-这将从当前点画一条线,从6.4,6.4到17.6,17.6。 看一看:

Image for post

M6.4,17.6 -This will again move the pointer from 17.6,17.6 to 6.4,17.6.

M6.4,17.6-这将再次将指针从17.6,17.6移动到6.4,17.6。

Image for post

L17.6,6.4 -This will draw a line from the present point, which is 6.4,17.6 to 17.6,6.4. Have a look:

L17.6,6.4-这将从当前点画一条线,从6.4,17.6到17.6,6.4。 看一看:

Image for post

That’s all you can now create your own vector assets.

这就是您现在可以创建自己的矢量资产的全部。

Before we wrap up, I would like to thank Nick Butcher. I’ve learned many of the concepts that I mentioned here from the talk that he gave at KotlinConfig 2019. If you’re interested, have a look

在结束之前,我要感谢尼克·布彻(Nick Butcher)。 我从他在KotlinConfig 2019上的演讲中学到了很多我在这里提到的概念。如果您有兴趣,请看看

You can find me on Medium, Twitter and LinkedIn.

您可以在MediumTwitterLinkedIn上找到我。

See you next time, thank you for reading.

下次见,谢谢您的阅读。

翻译自: https://levelup.gitconnected.com/vector-asserts-in-android-f0774ba8bf92

向量中断和非向量中断

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值