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);
}
}