Detailed Android drawable method _android

Source: Internet
Author: User
Tags dashed line trim

This article for everyone to share the Android Drawable method of detailed use of methods for your reference, the specific content as follows

1. Bitmapdrawable Related methods:

New under the drawable directory, examples are as follows:

<bitmap xmlns:android= "http://schemas.android.com/apk/res/android"
  android:antialias= "true"
  Android: Dither= "true"
  android:filter= "true"
  android:gravity= "center"
  android:mipmap= "false"
  Android: src= "@drawable/ic_launcher"
  

Each attribute is explained as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <bitmap xmlns:android= "Http://schemas.android.com/apk/res/android" <!--the anti-aliasing effect of the image, open it will make the picture smooth, can also reduce the resolution of the picture (amplitude can be ignored)--> android:antialias= "true" <!--whether to open the jitter effect, Turn on this option to dye high-quality pictures in a low quality screen can also better display--> android:dither= "true" <!--whether to open the filter, open can maintain a good display--> android:filter= "true "<!--picture gravity control, can control picture position, also can pass" | "        To use: Top to top, do not change the picture size bottom the picture to the bottom, do not change the size left picture on the left-hand side, do not change the size right The picture to the right, does not change the size center_vertical the picture vertically center, does not change the size fill_vertical picture vertical direction fills the entire container center_horizontal picture horizontal direction to reside , do not change the picture size fill_horizontal picture Horizontal direction Fills center Picture Center fill picture fills the container, this is the default value clip_vertic The AL add-on option, which indicates a vertical cut, uses less clip_horizontal additional options, is horizontal cropping, and uses less--> android:gravity= "center" <!--texture mapping , once again false--> android:mipmap= "false" <!--the data source of the picture--> android:src= "@drawable/ic_launcher" <!--has the following values
  The approach should be as follows:  Disabled default mode, turn off tile repeat horizontal and vertical tiles mirror a mirror projection effect in horizontal and vertical direction clamp around pixels spread to the surrounding area--> Android 

 : tilemode= "Disabled"/>

The above layout method is only for easy viewing and cannot be copied directly into the code.

2. shapedrawable, solid color, gradient drawable

Basic Sample code:

<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android= "Http://schemas.android.com/apk/res/android" Android:shape= "Rectangle" > <corners android:bottomleftradius= "1px" android:bottomrightradius= "1px" and roid:radius= "2px" android:topleftradius= "1px" android:toprightradius= "1px"/> <gradient android:angle=
    "5" android:centercolor= "#ff789434" android:centerx= "x" android:centery= "y" android:endcolor= "#34234324" android:gradientradius= "55DP" android:startcolor= "#ff783247" android:type= "linear" android:uselevel= "true"
  /> <padding android:bottom= "5DP" android:left= "5DP" android:right= "5DP" android:top= "5DP"/> 
    <size android:width= "70DP" android:height= "70DP"/> <solid android:color= "#fffff333"/> <stroke Android:width= "30DP" android:color= "#33333333" android:dashgap= "4DP" android:dashwidth= "4DP"/> </s

 Hape>

Each label detailed description:

<shape> representing image shapes

Represents a graphic shape with four options rectangle (rectangles), oval (oval), line (horizontal), Ring (circle), and the default is a rectangle. When you use line and ring, you need to specify the width and color of the lines by using the Stroke property, otherwise the display will be abnormal.
When you set up the ring, there are five special properties that correspond to the following:
android:innerradius--the inner radius of the ring, and the presence of innerradiusratio at the same time, Innerradius is the standard.
The thickness of the android:thickness--ring and the outer radius minus the inner radius
android:innerradiusratio--within the radius of the entire drawable width ratio, default is 9
android:thicknessradio--thickness of our entire drawable ratio, the default is 3, calculated by thickness = width/n
android:uselevel--should generally use false unless it is used as a levellistdrawable

<corners> (four angle angle, applicable to rectangle)

Represents the angle of four corners of a shape. It is only tried with the rectangle, the angle value is the degree of rounded angle, expressed in px. Where radius is used to set the angle for four angles, the priority is lower. Topleftradius, Toprightradius, Bottomleftradius, Bottomrightradius respectively represent the upper left, upper right, lower left, lower right angle.

<gradient> (color gradient)

Represents a gradient fill, which is mutually exclusive to the label, and contains several attribute values that are of the following meaning:
android:angle--the gradient angle, the default is zero, and the value must be a multiple of 45.
android:centerx--Gradient Center Point horizontal axis
android:centery--Gradient Center Point ordinate
android:startcolor--Gradient Start Color
android:centercolor--Gradient's middle color
End color of android:endcolor--gradient
android:gradientradius--Gradient Radius
android:uselevel--is generally false, only if android:type= "radial"
The type of android:type--gradient, with a linear (linear gradient), radial (radial gradient), sweep (scan line gradient) Three, where the default is a linear gradient

<solid> (solid color fill)

You can specify the color of the fill in shape by android:color

<stroke> (Stroke attributes)

Contains the following common tags:
android:width--the width of the stroke, the larger the edge of the shape will look thicker
Color of android:color--Strokes
android:dashwidth--the width of the segment that makes up a dashed line
android:dashgap--the spacing between segments that make up a dashed line, the greater the gap

<padding> Inner margin

Can be understood as an inner margin, used and directly in view with a margin similar to include four properties: Android:top, Android:bottom, Android:left, and Android:right, respectively, representing the upper and lower left and right interior margins.

<size>

There are two properties Android:width and Android:height, which indicate that the view is wide, similar to the height of the picture itself, and is stretched in view.

3. Layerdrawable layered drawable, similar layers

A collection of multiple drawable, similar to the layer concept, by placing different drawable on different layers to achieve an overlay effect, its syntax rules are as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <layer-list xmlns:android=
"Http://schemas.android.com/apk/res" /android ">
  <item
    android:id=" @+id/item1 "
    android:bottom=" 1DP "
    android:drawable=" @ Drawable/ic_launcher "
    android:left=" 1DP "
    android:right=" 1DP "
    android:top=" 1DP ">
  </item >

A layer-list can contain multiple item, each item represents a drawable. The top or bottom is an attribute that represents the drawable relative to the top and bottom offset of the view, in pixels. You can refer directly to a drawable resource by drawable. You can also customize the drawable directly under the item label.
By default, all drawable in layer-list are scaled to view size. Layer-list has a hierarchical concept, the following drawable will cover the drawable above. Through reasonable layering, some special superposition effects can be realized.

4. Statelistdrawable (selector)

The

selector tag, the corresponding label is <selector>, and is also a collection of drawable, used to change the color according to the current state.

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android= "http://schemas.android.com/apk/res/" Android <!--will change as the state changes, the default is False, indicating that it will change--> android:constantsize= "true" <!--whether to open the jitter, open on the low resolution of the mobile phone can get better Display, it is recommended to True--> android:dither= "true" <!--whether to change the inner margin with the state, it is recommended to be true--> android:variablepadding= "true" > & 
    Lt;item <!--corresponding drawable--> android:drawable= "@drawable/ic_launcher" <!--pressed state, such as Button pressed not released--> Android:state_pressed= "True" <!--indicates that view has got focus--> android:state_focused= "true" <!--the cursor is hovering, 4.0 to Post-attribute--> android:state_hovered= "true" <!--indicates that the user has selected view--> android:state_selected= "true" <!--table Shows whether the component can be selected, such as RadioButton--> android:state_checkable= "true" <!--indicates that the user has selected view, which is typically used to select buttons--> android:state _checked= "True" <!--indicates that view is currently available--> android:state_enabled= "true" <!--indicates that view is active--> and Roid:state_activated= "true" <!--is the application in front of the--> android:state_window_focused= "true" > </item> </selector> 

 

Note: If you have more than one item, the program will automatically match from top to bottom, and the first match will be applied. (not by Best match) If an item has no status description, it can be matched by any one of the states. So the default item should be put on the last one, or it will cause the bottom option to have no effect.

5. Levellistdrawable (different grades switch different drawable)

Levellistdrawable corresponds to the <level-list> tag, which is used to represent a drawable collection. Each drawable in the collection is a level, depending on the level, levellistdrawable will switch to the corresponding drawable, details are as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <level-list xmlns:android=
"Http://schemas.android.com/apk/res" /android ">
  <item
    <!--corresponding drawable-->
    android:drawable =" @drawable/ic_lunch "
    <!-- Maximum rank-->
    android:maxlevel = "2"
    <!--minimum rank-->
    android:minlevel = "1" >
    <!-- The rank between the maximum and the minimum levels will correspond to the drawable in this item, and when it is the background of the view, the
    drawable Setlevel method can be used to set different levels to toggle the specific drawable. You can also switch through the ImageView
    Setimagelevel method, with a rating range of 0 to 10000,0 is the default value-->
  </item>
</level-list>

6. Transitiondrawable (can be implemented in fade)

corresponding to the <transition> tag, used to achieve the effect of fading between two drawable, the specific use of the following methods

<?xml version= "1.0" encoding= "Utf-8"?> <transition xmlns:android=
"Http://schemas.android.com/apk/res" /android ">
  <item
    <!--corresponding drawable-->
    android:drawable =" @drawable/ic_lunch "
    <!-- The corresponding ID-->
    android:id = "@+id/item1"
    <!--the offsets around drawable-->
    android:top = "1DP"
    android: right = "1DP"
    android:bottom = "1DP"
    android:left = "1DP" >
  </item>
</transition>

How to use:

Defining gradients

<?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/drawable "/>
</transition>

Add a gradient to the background

<textview
android:id = "@+id/button"
andoird:layout_height = "Wrap_content"
android:layout_width = "Wrap_content"
android:background = "@drawable/transition_drawable"
/>

Controlling in code

TextView TextView = (TextView) Findviewbyid (R.id.button);
Transitiondrawable drawable = (transitiondrawable) textview.getbackground ();
Drawable.starttransition (1000);

7. Insetdrawable (embedded a drawable)

You can embed drawable inside yourself and leave a gap around you, generally in a view where you want your background to be smaller than your actual area.

<?xml version= "1.0" encoding= "Utf-8"?> <inset xmlns:android=
"http://schemas.android.com/apk/res/" Android "
  <!--corresponding drawable can also be written directly in the internal-->
  android:drawable=" @drawable/ic_launcher "
  <!-- Drawable distance around the margin-->
  android:insetbottom= "2DP"
  android:insetleft= "2DP"
  android:insetright= "2DP"
  android:insettop= "2DP" >
  <!--internally used to place the corresponding drawable, you can also refer directly to the top-->
</inset> 

8. Scaledrawable (zoom class, can be reduced)

The corresponding code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <scale xmlns:android=
"http://schemas.android.com/apk/res/" Android
  <!--corresponding drawable-->
  android:drawable= "@drawable/ic_launcher"
  <!--display location-->
  android:scalegravity= "center"
  <!--corresponding scaling-->
  android:scaleheight= "50%"
  android: Scalewidth= "50%" >
</scale> 

You must set the drawable level when you use it, or you cannot use it, set the level code as follows:

View Testscale = Findviewbyid (R.id.test_scale);
Scaledrawable testscaledrawable = (scaledrawable) testscale.getbackground ();
Testscaledrawable.setlevel (1);/range is 1~10000, as long as it is not zero

9. Clipdrawable (cut drawable according to different settings)

You can crop another drawable according to the current level, with the following specific code:

<?xml version= "1.0" encoding= "Utf-8"?> <clip xmlns:android=
"http://schemas.android.com/apk/res/" Android "
  <!--cutting direction, horizontal or vertical-->
  android:cliporientation=" Horizontal "
  android:drawable=" @+id/icon "
  <!--and cliporientation in conjunction with the details see below-->
  android:gravity= "Top" >
</clip> 

top--is placed to the top, and if you crop vertically, crop from the bottom
bottom--to the bottom, if it is trimmed vertically, cut from the top
left--to the left, if horizontal cropping, then crop from right (this is the default)
right--to the right, if the horizontal crop, then from the left to start cropping
center_vertical--Vertical Center, cut vertically, cut both up and down
fill_vertical--vertical padding, if the vertical crop, only when the clipdrawable level is 0 o'clock (not visible at this time, for full cropping), only the clipping behavior
(horizontal orientation also has similar properties as vertical, corresponding to Center_horizontal and fill_horizontal, similar to the two above)
Center of middle of center--, cut up and down at the same time when cutting vertically, and reduce at the same time when cutting horizontally
fill--full drawable, when clipdrawable level is zero effective
clip_vertical--additional options, table trim in vertical direction
clip_horizontal--additional options, table trim in vertical direction

You need to set the level code in your code to use the following

Imageviwe Testclip = (imageview) Findviewbyid (r.id.test_clip);
Clipdrawable testclipdrawable = (clipdrawable) testclip.getdrawable ();
The grade is from 0 to 10000, Level 0 means complete cropping, and level 10000 means not cutting
testclipdrawable.setlevel (5000);

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.