Android Drawable knowledge in summary

This article is a summary after learning of the Drawable chapter "Android development of artistic exploration."

A common Drawable introduced species

Drawable class xml tag description
BitmapDrawable Represents a picture, compared with direct reference to the original picture can set some effect
ShapeDrawable Configuration graphical shapes of various colors, corresponding to the label is an actual entity class GradientDrawable
LayerDrawable Drawable represents a hierarchical set of different Drawable can be placed on different layers to achieve the synergistic effect
StateListDrawable It means a collection Drawable, each set corresponding to a state in Drawable View most commonly used for Button
LevelListDrawable Means a collection Drawable, Drawable each set (Item) has a concept level (Level) a. Drawable can be switched according to a corresponding different levels. Each Drawable (Item) corresponding to a level range, the method can be switched by setLevel Drawable the foreground as if ImageView may still be switched by setImageLevel Drawable method of ImageView. level range 0-10000.
TransitionDrawable A fade in effect between the two Drawable fade, fade and implemented through the inverse process of startTransition and reverseTransition Drawable method, a method of receiving two time parameter.
InsetDrawable Drawable can be embedded into other among themselves, and can flow around a certain distance. When a View want the background to be smaller than their actual area, you can use this Drawable. LayerDrawable can achieve the same effect
ScaleDrawable It can be scaled to a certain proportion according to their level specified Drawable.
ClipDrawable According to their current levels to cut another Drawable, trimming direction by android: clipOrientation and android: gravity two properties jointly controlled.



Second, a variety of xml attribute Detailed Drawable

1.

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="图片资源id"
    android:antialias="true|false" 
    android:dither="true|false"
    android:filter="true|false"
    android:gravity="..."
    android:mipMap="true|false"
    android:tileMode="disabled|clamp|repeat|mirror" />

antialias antialiasing, will open the picture becomes smooth, but also reduce the clarity of the picture to some extent, but to reduce the magnitude as low as negligible, so it should be open;
DITHER jitter effect, when the image pixel configuration and cell phone pixel screen are inconsistent, turn this option allows high-quality images can maintain a better display on a low-quality screen, such as picture color mode is ARGB8888, but the device screen supports color mode is RGB555, which when open dithering option allows images to display not too distorted to create a Bitmap in Android ARGB8888 general will choose this mode, a color mode in which pixels share size is 4 bytes, the sum of the digits of a pixel high, the more realistic the image. According to the analysis, the effect of jitter should be opened;
filter filtering effect, when the picture size is stretched or compressed, the filter can be maintained better display, should be opened;
Mipmap an image processing technique is not used, the default can be false;
titleMode tiled pattern.

Point 9 picture corresponds NinePatchDrawable, xml tag is

2.

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle|oval|line|ring">
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="color"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type="linear|radial|sweep"
        android:useLevel="true|false" />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

android: shape pattern shape, four options: rectangle (rectangle), Oval (elliptical), Line (horizontal line), Ring (ring). The default is the rectangle, line and ring must pass Label to specify the line width and color information, or can not achieve the desired display.
This shape for the ring, there are five special properties:
android: innerRadius inner radius, and android: when exist innerRadiusRatio, to android: innerRadius subject
android: Thickness The thickness of the ring, i.e., subtracting the size of the outer radius of the inner radius, and android: when thicknessRatio exist in android: Thickness prevail
android: innerRadiusRatio the radius ratio of the total width of Drawable, default 9. If it is n, then the inner radius = width / n-
Android: thicknessRatio thickness to width ratio of the total Drawable default value 3. If it is n, then the thickness = width / the n-
Android: useLevel generally should use false, or they may not achieve the desired results unless it is used as a LevelListDrawable

Represents the angle of the four corners, is only applicable to Shape, where the angle refers to the degree rounded, represented by px, five attributes:
Android: the four corners of RADIUS simultaneously set the same angle, lower priority, It will be covered by the other four properties.
android: top left corner topLeftRadius
android: topRightRadius the upper right corner
android: bottomLeftRadius the lower left corner
android: the lower right corner bottomRightRadius

He represents gradient, and Exclusive solid labels, attributes are as follows:
Android: angle angle of the gradient, the gradient direction of impact, the default is 0, the value must be a multiple of 45, such as 0 represents left to right from top to bottom represents 90
android: centerX gradation center point abscissa
android: centerY ordinate gradation center point, the center point of the influence of the gradient of the gradient specific effects
android: startColor starting color gradation
android: centerColor intermediate color gradation
android: endColor end of the color gradient
android: gradientRadius radius of the gradient, only valid when type = radial
android: type graded categories, there are three values: linear (linear gradient), radial (radial gradient), Sweep (scan line gradient). The default is a linear gradient.
android: useLevel generally false, as is true when a StateListDrawable

Solid color fill, attributes android: color represented by color fill

Shape of the stroke, the following properties:
Android: width width, shape of the edge of the larger stroke look more coarse
android: color stroke color
android: dashWidth composition dashed line width
android: dashGap line composed of a broken line the spacing between the
android: dashWidth and android: dashGap there is a zero, then the result is not dashed into force.

View contains express its gaps, up and down about four properties.

The final size of the shape size, but generally not shape because shape adaptive View, width and height.

3.

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable=""
        android:id=""
        android:top=""
        android:right=""
        android:bottom=""
        android:left="" />
</layer-list>

May comprise a plurality of layer-list item, each item represents a Drawable.
android: top android: right android: bottom android: left right and left vertical offset.
android: drawable Drawable direct reference to a resource, you can also customize the item in Drawable.
All Drawable will default layer-list is scaled to the size of View

4.

<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize="true|false"
    android:dither="true|false"
    android:variablePadding="true|false"
    >
    <item
        android:drawable=""
        android:state_pressed="true|false"
        android:state_focused="true|false"
        android:state_hovered="true|false"
        android:state_selected="true|false"
        android:state_checkable="true|false"
        android:state_checked="true|false"
        android:state_enabled="true|false"
        android:state_activated="true|false"
        android:state_window_focused="true|false"
        />
</selector>

android: constantSize StateListDrawable intrinsic size attribute indicates whether the state does not change with changes, because the change of state causes a switch to a specific Drawable, Drawable different states may be different sizes. true to the inherent size remains the same. dither represents the dithering effect. android: variablePadding indicate whether StateListDrawable of padding with a change of state change, not recommended to open.

Each item of information expressed in one state Drawable. Common states are as follows:
Android: state_pressed pressed
android: state_focused get focus
android: state_selected user selects the View
Android: state_checked the user selects View, View CheckBox generally used for this type between the selected and non-selected state switching
android: state_enabled View the available state

The system selects a corresponding item from the selector according to the state of the View to find in this order from up to the first to find a matching item. General default item should be placed at the end of a selector and does not come with any state, so that when the current state of the above item View can not match, it will select the default item, the item is not included because the default state, so it can View the status of any match.

5.

<level-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable=""
        android:minLevel=""
        android:maxLevel="" />
</level-list>

Sample Code

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/s1" android:minLevel="0" android:maxLevel="20"/>
    <item android:drawable="@drawable/s2" android:minLevel="21" android:maxLevel="40"/>
    <item android:drawable="@drawable/s3" android:minLevel="41" android:maxLevel="60"/>
    <item android:drawable="@drawable/s4" android:minLevel="61" android:maxLevel="100"/>
</level-list>
//id是level_list_img的ImageView用上面的level-list做背景
ImageView iv = (ImageView) findViewById(R.id.level_list_img); 
LevelListDrawable levelListDrawable = (LevelListDrawable) iv.getDrawable(); 

int level = ???; //业务逻辑得出一个level信息,比如当前电量
levelListDrawable.setLevel(level); //根据得出的电量level信息显示level-list中的相应电量的图片

6.

<transition xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable=""
        android:id=""
        android:left=""
        android:top=""
        android:right=""
        android:bottom="" />
</transition>

Fade-out effects between two Drawable, Drawable properties with the foregoing, the sample code:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/drawable1"/>
    <item android:drawable="@drawable/drawable2"/>
</transition>
//id是text_view的TextView用上面的transition做背景
TextView tv = (TextView) findViewById(R.id.text_view); 
TransitionDrawable drawable = (TransitionDrawable) tv.getDrawable(); 

drawable.startTransition(1000);
//drawable.reverseTransition(1000);//逆过程

7.

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable=""
    android:insetLeft=""
    android:insetTop=""
    android:insetRight=""
    android:insetBottom="" />

The other drawable embedded in the inset, and may leave a certain distance, and properties similar to the foregoing Drawable around. The following example code boundary inset from the shape of View 15dp, layer-list can achieve the same effect:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="15dp"
    android:insetLeft="15dp"
    android:insetRight="15dp"
    android:insetTop="15dp">

    <shape android:shape="rectangle">
        <solid android:color="#ff0000"/>
    </shape>
    
</inset>

8.

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable=""
    android:scaleGravity=""
    android:scaleHeight=""
    android:scaleWidth="" />

android: scaleGravity equivalent to the shape of android: gravity, android: scaleWidth and android: scaleHeight respectively represent scale drawable specified width and height, in percentage expressed (see sample code below). When using the scale to consider level of value ScaleDrawable, levle it's time to 0 indicates ScaleDrawable not visible, 0 is the default, so in order to ScaleDrawable visible, not grade level is 0. Sample code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/test_drawable"
    android:scaleGravity="center"
    android:scaleHeight="70%"
    android:scaleWidth="70%" />
View view = findViewById(R.id.test_view);
ScaleDrawable drawable = (ScaleDrawable)view.getBackground();
drawable.setLevel(1);

Code value must be set level, otherwise the default value of 0 is not visible. level range within the system agreed to 0-10000, of course, set to 20,000 can also work but is not recommended to do so.

9.

<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable=""
    android:clipOrientation=""
    android:gravity="" />

clipOrientation represents trimming direction, two vertical and horizontal directions. gravity and clipOrientation together to play a role, the value of gravity as follows:

value meaning
top Drawable on top of the inner container does not change its size. If the vertical cut, then cut from the bottom
bottom On the inside bottom of the container Drawable, without changing its size. If the vertical cut, then cut from the top
left Drawable on the left inside the container, without changing its size. If the horizontal cut, then cut from the right
which is the default
right The internal Drawable on the right side of the container, without changing its size, if the level of cut, then cut from the left
center_vertical Drawable vertically centered in the internal container size does not change, if a vertical cut, the cut starts at the same time the upper and lower
fill_vertical The inner container is filled vertically Drawable. If the vertical cut, then only when ClipDrawable rating is 0 (0 means ClipDrawable been completely cut, that is not visible), in order to have cropped behavior
center_horizontal Similarly center_vertical, in different directions
fill_horizontal Similarly fill_vertical, in different directions
center Drawable internal centering horizontal and vertical directions, does not change the size. If the vertical cut, then cut at the same time, if the level of cut, then cut from top to bottom from left and right at the same time
fill Drawable internal horizontal and vertical directions simultaneously filled, only when ClipDrawable level is 0, only cutting behavior
clip_vertical Additional options, represents the vertical cut, use less
clip_horizontal Additional options, represents the horizontal cut, less use

Drawable level there is scope, namely 0-10000, the minimum value of 0 is fully cut, that the whole Drawable not visible. Maximum of 10000 means no clipping. If the downward vertical direction from the cut, level value 8000 indicates a 2000 cut, i.e. 20% of the region at the top cut off, the cropping area equivalent to not exist.



Three, Drawable the level summary

The above xml attribute describes some of the drawable level is very important, to sum up here:

  • There are a plurality of item, each item corresponds to a drawable, by setting specific values of level to decide which item that is drawable.
  • For scaling, level default value is 0,0 represents ScaleDrawable is not visible, so in order to ScaleDrawable visible, you must set the level is not 0, specifically a few does not matter, you can not zero .
  • The cropped, level value determines the percentage of the crop, the need for specific values, because the decision to cut the percentage .

System level range 0-10000 predetermined value, the level value is provided a method:

  1. Drawable arranged corresponding background of a View
  2. Drawable obtain the corresponding objects, code view.getDrawable () View from background or view.getBackground (), a strong turn to the corresponding type can Drawable
  3. Call setLevel () method to set the level after obtaining the Drawable object.

Guess you like

Origin www.cnblogs.com/developerzjy/p/11084172.html