Recently in the study of custom controls, encountered the drawing of geometry, here is a common example:
① first in the main activity code:
Package com.example.mycustomwidget;
Import android.app.Activity;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.RectF;
Import Android.os.Bundle;
Import Android.util.AttributeSet;
Import Android.view.View;
public class Mainactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Mycustomview MyView = new Mycustomview (mainactivity.this);
Setcontentview (MyView);
}
}
Code in ②mycustomview:
Package com.example.mycustomwidget;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Rect;
Import Android.graphics.RectF;
Import Android.util.AttributeSet;
Import Android.view.View;
public class Mycustomview extends view{
Public Mycustomview (context context, AttributeSet Attrs) {
Super (context, attrs);
TODO auto-generated Constructor stub
}
Public Mycustomview (context context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr);
TODO auto-generated Constructor stub
}
Public Mycustomview (Context context) {
Super (context);
TODO auto-generated Constructor stub
}
@Override
protected void OnDraw (canvas canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);
Set the color of the canvas
Canvas.drawcolor (Color.Blue);
-----------------------------① draw a hollow square--------------------------------
Create a brush
Paint Mpaint = new paint ();
Set Brush to Hollow
Mpaint.setstyle (Paint.Style.STROKE);
Set Picture color
Mpaint.setcolor (color.red);
Set Brush width
Mpaint.setstrokewidth (10);
/**
* Create a rectangle
* First parameter: The x-axis coordinate of the left vertex of a rectangle
* Second parameter: y-coordinate of the left vertex of a rectangle
* Third parameter: The x-coordinate of the vertex in the lower-right corner of a rectangle
* Fourth parameter: The y-coordinate of the vertex in the lower right corner of the rectangle
*/
Rect mrect = new Rect (10, 10, 50, 50);
Start drawing the rectangle.
Canvas.drawrect (Mrect, mpaint);
-----------------------------② to draw a solid square--------------------------------
Set Brush to Solid
Mpaint.setstyle (Paint.Style.FILL);
Resize a rectangle
Mrect.set (60, 60, 100, 100);
Canvas.drawrect (Mrect, mpaint);
-----------------------------③ draw a hollow rectangle--------------------------------
Set the width of a brush
Mpaint.setstrokewidth (2);
Mpaint.setstyle (Paint.Style.STROKE);
Mrect.set (70, 110, 140, 140);
Canvas.drawrect (Mrect, mpaint);
-----------------------------④ draw a hollow circle--------------------------------
Anti-aliasing
Mpaint.setantialias (TRUE);
/**
* Draw a hollow circle
* First parameter: X-axis coordinate of center point
* Second parameter: Y-axis coordinate of center
* Third parameter: radius of a circle
* Fourth parameter: Brush
*/
Canvas.drawcircle (Mpaint);
-----------------------------⑤ to draw a solid circle--------------------------------
Mpaint.setstyle (Paint.Style.FILL);
Canvas.drawcircle (, Mpaint);
-----------------------------⑥ to draw a solid sector--------------------------------
Draw a rectangle first
RECTF Oval = new RECTF (240, 240, 300, 300);
/**
* Draw a solid fan
* First parameter: A rectangle, the size of the user-defined Pie
* Second parameter: The starting angle of the drawing fan
* The third parameter: the angle to be drawn
* Whether the fourth parameter includes a circle (true is included, usually used to draw a fan; False is not included, generally used for arcs)
* Fourth parameter: Brush
*/
Canvas.drawarc (oval, 0, Max, true, Mpaint);
-----------------------------⑦ to draw a hollow fan--------------------------------
Mpaint.setstyle (Paint.Style.STROKE);
Oval.set (290, 290, 350, 350);
Canvas.drawarc (oval, 0, true,mpaint);
-----------------------------⑧ to draw an arc--------------------------------
Oval.set (360, 360, 400, 400);
Canvas.drawarc (oval, 0, +, false, mpaint);
-----------------------------⑨ to draw a inscribed circle--------------------------------
Oval.set (360, 360, 400,400);
Canvas.drawrect (oval, mpaint);
Canvas.drawarc (oval, 0, N, False, Mpaint);
}
}
Android Paint Canvar Drawing geometry