On the summary of drawable use knowledge in Android

This article is a summary after studying the drawable chapter of Android development art exploration.

Drawable is basically used in our usual development, and it is very useful to everyone. So what is drawable? A gadget that can be drawn on canvas. Compared with view, it doesn't need to consider measure and layout, just how to draw (canavs). Of course, you are no stranger to the traditional usage of drawable. Today, we mainly bring you the following usage of drawable:

1. Compared with view, custom drawable is lightweight and easy to use. When customizing an effect in the future, you can change the idea of view first and try drawable first.

2. Custom state. I believe everyone is familiar with state drawable, but have you tried to customize a state in the past?

3. Use drawable to improve our UI performance. How to use drawable to improve the performance of our UI.

1、 Introduction to common drawable types

2、 Detailed explanation of various drawable XML attributes

1.<bitmap/>

Antialias anti aliasing will make the picture smooth and reduce the definition of the picture to a certain extent, but the reduction is so low that it can be ignored, so it should be turned on;

Dither dither effect: when the pixel configuration of the picture is inconsistent with that of the mobile phone screen, enabling this option can enable high-quality pictures to maintain a better display effect on the low-quality screen. For example, the color mode of the picture is argb8888, but the color mode supported by the device screen is rgb555, At this time, turning on the dithering option can make the picture display not too distorted. Argb8888 is generally used to create a bitmap in Android. In this color mode, the size of a pixel is 4 bytes. The higher the total number of bits of a pixel, the more realistic the image will be. According to the analysis, the dithering effect should be turned on;

Filter filtering effect: when the picture size is stretched or compressed, filtering can maintain a good display effect and should be turned on;

Mipmap is an image processing technology, which is not commonly used. It can default to false;

Titlemode tile mode.

The point 9 image corresponds to ninepatchdrawable, and the XML tag is < nine patch / >

2.<shape/>

Android: shape graphic shape, four options: rectangle, oval, line and ring. The default is rectangle. Line and ring must specify the width and color of the line through the < stroke > label, otherwise the expected display effect cannot be achieved.

For ring, there are five special properties:

Android: innerradius inner ring radius. When Android: innerradius ratio exists at the same time, Android: innerradius shall prevail. Android: thickness ring thickness, that is, the outer radius minus the inner radius. When Android: thicknessratio exists at the same time, Android: thickness shall prevail. Android: innerradiusratio inner radius accounts for the width proportion of the whole drawable. The default value is 9. If it is n, the inner radius = width / N Android: thicknessratio the proportion of thickness to the width of the whole drawable. The default value is 3. If n, then thickness = width / N Android: uselevel should generally use false, otherwise it may not achieve the desired effect unless it is used as levellistdrawable

<corners>

Represents the angle of four corners, which is only applicable to shape. Here, the angle refers to the degree of fillet, expressed in PX. There are five attributes:

<gradient>

Represents a gradient color, which is mutually exclusive with the < solid > solid color label. The attributes are as follows:

<solid>

Solid color filling, and the attribute Android: color indicates the filling color

<stroke>

The stroke attributes of shape are as follows:

<padding>

Represents the blank space of the view containing it. It has four properties: up, down, left and right.

<size>

The size of the shape, but not the final size of the shape, because the shape generally adapts to the width and height of the view.

3.<layer-list/>

A layer list can contain multiple items, and each item represents a drawable.

Android: Top Android: right Android: bottom Android: left sets the up, down, left and right offsets.

Android: drawable directly refers to a drawable resource, or you can customize drawable in item.

By default, all drawable in the layer list will be scaled to the size of the view

4.<selector/>

Android: constantsize property indicates whether the inherent size of statelistdrawable does not change with the change of state, because the change of state will lead to switching to a specific drawable, and the drawable sizes in different states may be different. True means that the inherent size remains unchanged. Dither indicates dithering effect. Android: variablepadding indicates whether the padding of statelistdrawable changes with the change of state. It is not recommended to enable it.

<item>

Each item represents drawable information in a state. Common states are as follows:

android:state_ Pressed Android: state_ Focused get focus Android: state_ Selected user selected view Android: state_ Checked user selects view, which is generally used for Check@R_ 510_ 2419 @ view Android: state that switches between selected and unselected States_ Enabled view is available

The system will select the corresponding item from the selector according to the view status, and search from top to bottom until the first matching item is found. Generally, the default item should be placed in the last item of the selector without any state. In this way, when none of the above items can match the current state of the view, the default item will be selected. Because the default item does not have a state, it can match any state of the view.

5.<level-list/>

Sample code

6.<transition/>

The fade in and fade out effect between two drawable is the same as the previous drawable. Example code:

7.<inset/>

Embed other drawable into inset, and leave a certain space around it. The attribute is similar to the previous drawable. The following example code realizes that the distance between the shape in inset and the boundary of view is 15dp, and layer list can achieve the same effect:

8.<scale/>

Android: scalegravity is equivalent to Android: gravity in shape. Android: scalewidth and Android: scaleheight represent the scaling of the specified drawable width and height respectively, expressed in percentage (see the following example code). When using scale, you need to consider the level value of scaledrawable. When level is 0, it means scaledrawable is invisible, and 0 is also the default value. Therefore, to make scaledrawable visible, the level level cannot be 0. The example code is as follows:

The level value must be set in the code, otherwise the default value of 0 is invisible. The level range is set to 0-10000 in the system. Of course, setting it to 20000 can work normally, but it is not recommended.

9.<clip/>

Cliporientation indicates the clipping direction, which has two directions: horizontal and vertical. Gravity and cliporientation work together. The value of gravity is as follows:

The drawable level has a range, i.e. 0-10000. The minimum value of 0 means full clipping, i.e. the whole drawable is invisible. A maximum of 10000 means no clipping. If the vertical direction is cropped from top to bottom, the level value is 8000, indicating that 2000 is cropped, that is, 20% of the area is cropped at the top, and the cropped area does not exist.

3、 Drawable level summary

In the above XML attribute introduction, some drawable levels are very important. Here is a summary:

The range of level value is 0-10000, and the method of setting level value is as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>