Graphic images and animations for Android development (i) Paint and canvas class learning _android

Source: Internet
Author: User
Paint Class
The *paint class represents a brush that describes the color and style of the graphic, such as line width, color, transparency, and fill effects.
* When using the paint class, you need to create an object of that class, which can be implemented by the constructor of the class. The usual implementation code is:
*paintpaint=newpaint ();
* After you create the Paint object, you can change the default settings for the brush with the method provided by the object
Canvas
The *canvas class represents a canvas through which you can draw various shapes.
* Normally, to draw on Android, you need to first create a view that inherits from the view class and rewrite its OnDraw method in that class.
* Then add the view in the activity that displays the drawing

The XML file that implements this instance is defined as follows:
Copy Code code as follows:

<framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Tools:context= ". Draw_view "
>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
<com.example.draw_view. Drawview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</FrameLayout>

Implementation of the source code is as follows:
Copy Code code as follows:

publicclassdrawviewextendsview{
Publicdrawview (Contextcontext, Attributesetattrs) {
Super (context,attrs);
//todoauto-generatedconstructorstub
}
@Override
Protectedvoidondraw (canvascanvas) {
//todoauto- Generatedmethodstub
Super.ondraw (canvas);
Paintpaint=newpaint ();
Paint.setcolor (color.red);
Paint.setshadowlayer (2,3,3,color.rgb (180,180,180));
Canvas.drawrect (40,40,200,100,paint);
}

}

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.