Android canvas and drawing-canvas and drawables (4)

Source: Internet
Author: User

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

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.