The Android Drawable
concept
-
Drawable in Andoird refers to the concept of an image, which is not equal to a concrete picture, but can also be color, shape…
-
Drawable can be defined in XML or in code;
-
Drawable is an abstract class, each specific Drawable is its subclasses, such as ShapeDrawable BitmapDrawable…
Drawable width/height
Drawable can use getIntrinsicHeight() or getIntrinsicWidth() to get its internal length and width; Not all color drawables have a height and width. The internal height and width of an image Drawable are the same as the internal length and width of the image.
Drawable itself has no concept of width and height. When used as the background of a View, it will be stretched to the same size as the View.
Common Drawable
BitmapDrawable
BitmapDrawable represents an image, which can be defined by XML to set a number of effects, as follows:
<bitmap
android:mipMap="true|fase"// Texture mapping is not commonly usedandroid:src=""// Image resourcesId
android:antialias=""// Whether to enable image anti-aliasingandroid:dither="true|fase"// Adaptive Reduces distortion when the original color mode of the image is incompatible with the color mode compatible with the current modelandroid:filter="true|fase"// Filter is used to optimize the display effect of stretched or compressed imagesandroid:gravity=""// The location of the imageandroid:tileMode=""// Fill mode and fill mode when cannot fillgravityThe mutexxmlns:android="http://schemas.android.com/apk/res/android">
</bitmap>
Copy the code
NinePathDrawable is similar to BitmapDrawable
<? The XML version = "1.0" encoding = "utf-8"? > <nine-patch android:mipMap="true" android:src="" android:alpha="" android:dither="" android:filter="" android:gravity="" android:antialias="" android:tileMode="" xmlns:android="http://schemas.android.com/apk/res/android"> </nine-patch>Copy the code
But it’s a.9 graph, and the BitmapDrawable can also use a.9 graph, and it represents a.9 graph;
ShapeDrawable
ShapeDrawable represents a graph that contains colors. The entire graph can be solid or gradient.
<shape
android:shape="oval|line|rectangle|ring"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius=""
android:bottomLeftRadius=""
android:topLeftRadius=""
android:topRightRadius=""
android:bottomRightRadius=""
/>
<gradient
android:angle=""
android:centerX=""
android:centerY=""
android:startColor=""
android:centerColor=""
android:endColor=""
android:gradientRadius=""
android:type=""
/>
<solid android:color=""/>
<padding
android:top=""
android:left=""
android:right=""
android:bottom=""/>
<size
android:width=""
android:height=""/>
</shape>
Copy the code
- android:shape
Rectangle (oval), Rectangle (line),rectangle(ring),rectangle(oval),rectangle(line),rectangle(ring) Line and ring must match
to specify width and color, otherwise the desired effect will not be achieved;
For ring, there are five special properties
Android :innerRadius="" innerRadiusRatio Android :innerRadiusRatio="x" innerRadius= Width /x Android :thicknessRatio="" thicknessRatio android:thickness="" thicknessRatio android:useLevel="" Generally false Unless used as LevelDrawableCopy the code
- <corners>
Used to indicate the Angle of a corner
Android :radius="" Global Angle with the lowest priority Android :bottomLeftRadius="" Android :topLeftRadius="" // Upper left Angle Android: topRightRadius = "/ /" android: upper right Angle bottomRightRadius = "" / / the Angle of the lower rightCopy the code
- <gradient>
It is mutually exclusive with the <solid> tag, indicating gradient fill, and has the following properties
<gradient Android: Angle ="" The default gradient Angle is 0 and must be a multiple of 45 android:centerX="" the x coordinate of the gradient center Android :centerY="" the y coordinate of the gradient center Android :startColor="" Android :centerColor="" Android :endColor="" Android :gradientRadius=" Android: type = "" / / gradient type linear (linear, default) | radial (radial gradient) | sweep (scan line gradient) / >Copy the code
- <solid>
The color can be specified through Android :color
- <stroke>
Stroke, properties described as follows
< span style =" box-sizing: border-box; color: RGB (74, 74, 74); line-height: 20px; font-size: 14px! Important; word-break: break-word! Important;"Copy the code
- <padding>
Said white space, through the android: left, android: right, android: top, android: bottom said four directions of padding
- <size>
GetIntrinsicHeight () or getIntrinsicWidth(), but shape itself has no concept of width and height. Setting it to the View background will still stretch to the size of the View GetIntrinsicHeight () or getIntrinsicWidth() is -1;
LayerDrawable
<? The XML version = "1.0" encoding = "utf-8"? > <layer-list android:shape="ring" android:innerRadius="" android:innerRadiusRatio="" android:thicknessRatio="" android:thickness="" android:useLevel="" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:height="" android:width="" android:right="" android:left="" android:top="" android:bottom="" android:gravity="" android:id="" android:drawable="" android:end="" android:start="" > ... Drawable </item> <item>... Drawable </item> </layer-list>Copy the code
You can place different drawables in the inner item tag, so as to achieve a kind of superposition effect. The inner drawables can be located through the sub-attributes of the item. The internal drawables will be scaled to the actual size of the view, according to the actual size of the item Position, the item declared below, the higher the display priority;
StateListDrawable
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:dither="true">
<item android:drawable="@drawable/ic_ti_reset_enabled"
android:state_enabled="true" />
<item android:drawable="@drawable/ic_ti_reset_disenabled" android:state_enabled="false" />
</selector>
Copy the code
StateListDrawable corresponds to <selector>, and each internal Drawable corresponds to a View in a different state that shows the Drawable background; The set of states is as follows:
Attribute values | meaning |
---|---|
android:state_active | Whether to activate |
android:state_checkable | Whether it can be selected |
android:state_checked | Checked or not |
android:state_enabled | Whether the available |
android:state_active | Whether to activate |
android:state_first | Is it in the starting state |
android:state_focused | Getting focus |
android:state_last | Whether the status is in the end state |
android:state_middle | Whether it is in an intermediate state |
android:state_pressed | Whether to press |
android:state_selected | If the selected |
android:state_window_focused | Whether the window gets focus |
LevelListDrawable
LevelListDrawable corresponding labeled > < level – list are inside each item can be said a drawable, and there is a corresponding level range, use the drawable as a background, you can use the setLevel method set different level to switch the draw Able, but the value of the level is limited to 10000 Max and 0 min
<level-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:maxLevel="100"
android:minLevel="1"
android:drawable=""
/>
</level-list>
Copy the code
TransitionDrawable
The corresponding labels for < the transition >, internal item can be specified drawable and positioning, can through the drawable. StartTransition () and drawable. ReverseTransition () or reverse animation animation sequence