Paint class
* The Paint class is used to describe the color and style of the image, such as the width, color, transparency, and fill effect.
* When using the Paint class, you must first create an object of this class, which can be implemented through the constructor of this class. The general implementation code is:
* Paintpaint = newPaint ();
* After creating a Paint object, you can use the method provided by this object to change the default settings of the Paint brush.
Canvas
* The Canvas class represents the Canvas. You can use the constructor provided by this class to draw various images.
* Generally, to draw images in Android, you must first create a View that inherits the View class and override its onDraw method in this class,
* Add the view to the Activity that displays the drawing.
The xml file implementing this instance is defined as follows:
Copy codeThe Code is 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>
The source code is as follows:Copy codeThe Code is 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 (180,180,180, 3, Color. rgb ));
Canvas. drawRect (40, 40, 200,100, paint );
}
}