Transferred from: http://blog.csdn.net/ekeuy/article/details/12349853
Look at a lot of open source code used to shape, I look at the code is generally around the area, did not study carefully, these days just have time to learn in detail, in the course of learning reference to the official documents and online sample code, this article will be attached to the test code.
The steps for using shape in Android apps are like this, (1) Define an XML file under Res/drawable to define shape, and (2) you can see the effect by referencing the file in code or in an XML file. Here's how each step is implemented:
1. Create an XML file named Gradient_box under the Res/drawable folder:
<span style="font-size:18px"><?xml version="1.0"encoding="Utf-8"?> <!--a geometry defined in the shape drawable XML file, defined in the Res/drawable/directory, the file name filename, called the resource ID of the access, is accessed through r.drawable.filename in the code, through @[package:]drawable in the XML file ./filename for access. -<!--Android:shape=["Rectangle"|"Oval"|" Line"|"Ring"] shape, the default is rectangle, can be set to rectangle (rectangle), oval (oval), linear shape (line), Ring (ring) below the properties only in Android:shape="ring is available:Android:innerradius Size, radius of inner ring. Android:innerradiusratio float, which represents the radius of the inner ring at the width ratio of the ring, for example, if android:innerradiusratio, which means that the inner ring radius is equal to the width of the ring divided by 5, the value can be overwritten, and the default is 9. Android:thickness size, the thickness of the ring android:thicknessratio float, the width ratio of the ring to represent the thickness of the ring, for example, if Android:thicknes Sratio="2", then the thickness of the ring is equal to the width of the ring divided by 2. This value can be overridden by android:thickness, and the default value is 3. The Android:uselevel Boolean value, or False if it is used as a levellistdrawable using the value of true. -<Shape Xmlns:android="http://schemas.android.com/apk/res/android"Android:shape="Rectangle"> <!--fillet android:radius integer radius android:topleftradius integer upper-left corner radius Android:toprightradius integer upper right corner radius Android:bottomleftradius integer lower left corner radius android:botto Mrightradius integer Right bottom corner radius-<Corners Android:radius="8DP"Android:topleftradius="5DP"Android:toprightradius="15DP"Android:bottomleftradius="20DP"Android:bottomrightradius="25DP"/> <!--gradient Android:startcolor color value start Color Android:endcolor Color value end color Android:centercolor integral gradient middle color, i.e. start and end color The color between the Android:angle integral gradient angle (PS: When AngleWhen =0, the gradient is left to right. Then turn counterclockwise, when angle=90 o'clock is from bottom to top. Angle must be an integral multiple of 45) Android:type ["Linear"|"Radial"|"Sweep"] Gradient type (value: Linear, radial, sweep) linear linear gradient, which is the default setting RA Dial a radioactive gradient, centered on the starting color. Sweep the gradient of the scanned line type. Android:uselevel ["true"|"false"] Set to True if you want to use the Levellistdrawable object. Set to True no gradient. False has gradient Android:gradientradius integral gradient radius. When Android:type="Radial"is used. Use android:type= alone"Radial"will error. Android:centerx the relative position of the X-point coordinate of the integral Gradient center Android:centery integral type Relative position of the Y-point coordinates of the gradient center-<Gradient Android:startcolor="#FFFF0000"Android:endcolor="#80FF00FF"Android:angle=" $"/> <!--Padding , that is, the distance between content and edge Android:left integral left inner margin android:top integer upper padding and Roid:right Integral right inner margin android:bottom integral type bottom inner margin-<padding Android:left="10DP"Android:top="10DP"Android:right="10DP"Android:bottom="10DP"/> <!--size android:width integer width android:height integer height-<size Android:width="600DP"/> <!--Inner Fill Android:color color value fill Color-<Solid Android:color="#ffff9d77"/> <!--Stroke android:width Integer stroke width android:color color value stroke color The Android:dashwidth integer indicates that the style of the stroke is the width of the dashed line, and the value is 0 o'clock, which is expressed as solid. A value greater than 0 is a dashed line. The Android:dashgap integer indicates that when the stroke is dashed, the interval between dashes is "- - - -"-<Stroke Android:width="2DP"Android:color="#dcdcdc"/> </shape></span>
2. In the window layout file, create the file in 1 as the background of TextView:
<span style="font-size:18px"><?xml version="1.0"encoding="Utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="shapetest"Android:background="@drawable/gradient_box"android:textsize="24.0DP"Android:textcolor="@android: Color/black"/> </LinearLayout></span>
The background of the TextView is set dynamically in the code:
<span style="font-size:18px">@Override publicvoid onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.main); = getresources (); = Res. getdrawable (r.drawable.gradient_box); = (TextView) Findviewbyid (R.id.shapetxtid); Tv.setbackgrounddrawable (shape); } </span>
The specific effect of the test code is this:
SOURCE Download: Android Shape instance
Reference Document: Shape drawable
Use of shape in Android (reproduced)