This article translated from: http://developer.android.com/guide/topics/graphics/2d-graphics.html
Shape Rendering
When you want to dynamically draw two-dimensional images, the shapedrawable object will meet your needs. The shapedrawable object can be programmed to draw any original shape and theme style that can be imagined.
The shapedrawable class is a subclass of the drawable class. Therefore, you can use the shapedrawable object wherever you want to use the drawable object. For example, you can use the setbackgrounddrawable () method to set the background of the view object. Of course, you can also use the drawn shape as a custom view object, and then add it to your layout. Because the shapedrawable class has its own draw () method, you can create a view subclass that draws a shapedrawable image during the execution of the view. ondraw () method. The following code is just a basic extension for processing. It uses the shapedrawable object to draw a View window:
Publicclasscustomdrawableviewextendsview {
Privateshapedrawable
Mdrawable;
Publiccustomdrawableview (Context
Context ){
Super (context );
Int x
= 10;
Int y
= 10;
Int width
= 300;
Int height
= 50;
Mdrawable
= Newshapedrawable (newovalshape ());
Mdrawable. getpaint (). setcolor (0xff74ac23 );
Mdrawable. setbounds (X,
Y, X
+ Width, y
+ Height );
}
Protectedvoid
Ondraw (canvas ){
Mdrawable. Draw (canvas );
}
}
In the constructor in the above example, shapedrawable is defined as an ovalshape object, and then a color and border are set for it. If no border is set, the shape is not drawn. If no color is set, the default color is black.
Use this custom view object to draw any desired shape. In the preceding example, a shape is drawn by programming in an activity:
Customdrawableview mcustomdrawableview;
Protectedvoid
Oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Mcustomdrawableview
= Newcustomdrawableview (this );
Setcontentview (mcustomdrawableview );
}
If you want to draw the custom image from the XML layout instead of the activity, the customdrawable class must overwrite the view (context, attributeset) constructor, the constructor is called when the view object is filled in XML. Add the customdrawable element to XML, for example:
<Com. example. shapedrawable. customdrawableview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
The shapedrawable class (like some other drawable types in the Android. Graphics. drawable package) allows you to define various attributes in a public way. Some attributes may need to be adjusted, including transparency, color filtering, jitter, opacity, and color.
You can also use XML to define the initial drawing shape. For more information, see the shape drawables section (http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape) in the drawable resources document