Android uses Canvas to draw textview, bitmap, rectangle (cropping), ellipse, line, point, and arc

Source: Internet
Author: User

Android uses Canvas to draw textview, bitmap, rectangle (cropping), ellipse, line, point, and arc

Initialization object

Private Paint mPaint; // brush private int count; // Number of clicks private Rect rect; // rectangular public CounstomView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); // initialize the Paint brush mPaint = new Paint (); rect = new Rect (); setOnClickListener (this );}

 

1. Draw textview. And click set.// Render text. In addition to the above description, the Canvas class can also depict text. Parameter 1 is String text, parameter 2 x axis, parameter 3 Y axis, and parameter 4 is the Paint object.

String text = null; @ SuppressLint ("DrawAllocation") @ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); // set paintmPaint. setStyle (Style. FILL); // solid // mPaint. setColor (Color. BLUE); // yellow // mPaint. setAlpha (50); // transparency 15canvas. drawColor (Color. YELLOW); canvas. drawRect (0, 0, getHeight (), getWidth (), mPaint); // draw a rectangle mPaint. setColor (Color. RED); mPaint. setTextSize (25366f); text = String. valueOf (count); mPai Nt. getTextBounds (text, 0, text. length (), rect); // you can specify the length and width of the content. Float width = rect in the rect rectangle. width (); // obtain the width float height = rect. height (); canvas. drawText (text, getWidth ()/2-width/2, getHeight ()/2 + height/2, mPaint ); // set the text position //} @ Overridepublic void onClick (View arg0) {count ++; invalidate (); // redraw}

 

2. Draw a simple bitmap.Parameter 1 is our regular Bitmap object, parameter 2 is the source region (here it is bitmap), parameter 3 is the target region (should be in the position and size of the canvas ), parameter 4 is the Paint brush object. Because of the possibility of scaling and stretching, the performance will be greatly compromised when the original Rect is not equal to the target Rect.

 

Protected void onDraw (Canvas canvas) {super. onDraw (canvas); // Bitmap bitmap = BitmapFactory in the left and top positions. decodeResource (getResources (), R. drawable. login_backgroud); canvas. drawBitmap (bitmap, 0, 0, mPaint );}

3. Use canvas. drawBitmap (bitmap, src, dst, paint) to draw the cropped bitmap.// Parameter 1 is our regular Bitmap object. Parameter 2 is the source region (bitmap here) and parameter 3 is the target region (the location and size of the canvas should be included ), parameter 4 is the Paint brush object. Because of the possibility of scaling and stretching, the performance will be greatly compromised when the original Rect is not equal to the target Rect.

 

Protected void onDraw (Canvas canvas) {super. onDraw (canvas); // Bitmap bitmap = BitmapFactory in the left and top positions. decodeResource (getResources (), R. drawable. login_backgroud); Paint paint = new Paint (); canvas. save (); // left ---- distance from the left side of the screen... // Top ------ distance from top... // Right ----- width of the rectangle // buttom ----- height of the rectangle Rect rect = new Rect (10, 10,500,500); canvas. clipRect (rect); // sets the cropping area canvas. drawColor (Color. BLUE); // The rect of the cropping area changes to BLUE Rect rec = new Rect (20, 20,300,300); canvas. drawBitmap (bitmap, rect, rec, paint); canvas. restore ();}


 


4: draw an ellipse using canvas. drawOval.Draw an ellipse. The first parameter is the scan area, and the second parameter is the paint object;

 

Protected void onDraw (Canvas canvas) {super. onDraw (canvas); // The left and top positions of the Paint painting = new paint (); canvas. save (); // left ---- distance from the left side of the screen... A // top ------ distance from the top... B // right ----- width of the elliptic ..... C // buttom ----- the height of the elliptic ...... DRectF dst = new RectF (10, 10, 300,400); // 2a = 100-30, 2b = 310-260paint.setColor (Color. YELLOW); canvas. drawColor (Color. RED); canvas. drawOval (dst, paint); canvas. restore ();}

5. Use canvas to draw points.// Painting point, parameter 1 Horizontal X axis, parameter 2 vertical Y axis, and third parameter is the Paint object.

 

@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); Paint paint = new Paint (); canvas. save (); canvas. drawColor (Color. RED); // draw a canvas with a background. restore (); canvas. save (); paint. setColor (Color. BLACK); paint. setTextSize (20366f); canvas. drawText ("painting point:", 10, 90, paint); // text canvas. restore (); canvas. save (); paint. setColor (Color. GREEN); paint. setStrokeWidth (20366f); // set the size of the vertex canvas. drawPoint (120, 90, paint );// Parameter 1 Horizontal X axis, parameter 2 vertical Y axis, and third parameter is the Paint object. // Canvas. drawPoints (new float [] {60,400, 65,400,}, paint); // draw multiple vertices canvas. restore (); canvas. save (); paint. setColor (Color. BLACK); paint. setStyle (Style. FILL); canvas. drawText ("Can this be used as a dot?", 10,130, paint); paint. setAntiAlias (true); // remove anti-sawtooth paint. setColor (Color. YELLOW); canvas. drawCircle (120,130, 10, paint); // you can specify the horizontal X axis, the Y axis, the radius of the three circles, and the canvas of the four painting objects. restore ();}



 

 

6: draw a line.DrawLine (float startX, float startY, float stopX, float stopY, Paintpaint) // the x-axis position of the starting point of the parameter, the y-axis position of the two starting points of the parameter, and the X-axis horizontal position of the three ending points, the four Y axis vertical position of the parameter. The last parameter is the Paint brush object.

 

Protected void onDraw (Canvas canvas) {super. onDraw (canvas); Paint paint = new Paint (); canvas. drawColor (Color. YELLOW); paint. setAntiAlias (true); // anti-sawtooth paint. setDither (true); // Jitter to make the image look like there is no burr paint. setColor (Color. RED); paint. setStrokeWidth (3); // you can specify the width of the canvas. drawLine (20, 50,200,100, paint); // the x-axis position of the starting point of the parameter, the y-axis position of the two starting points of the parameter, the x-axis horizontal position of the three ending points of the parameter, and the four Y-axis vertical position of the parameter, the last parameter is the Paint brush object .}


 

 

7. Draw an arc:

The first parameter is the RectF object. the boundary of a rectangular area is defined in shape, size, and arc. The second parameter is the starting angle (degree) at the beginning of the arc,

Parameter 3 scanned angle (degree) starts clockwise measurement. Parameter 4 is an arc that includes the center of the elliptic and closes it if it is false, parameter 5 is the Paint object;


 

Protected void onDraw (Canvas canvas) {super. onDraw (canvas); Paint paint = new Paint (); canvas. drawColor (Color. BLUE); // draw arc paint. setStyle (Style. STROKE); // set the hollow paint. setColor (Color. RED); // set the color to paint. setStrokeWidth (3); // set the RectF oval = new RectF (); oval. set (50, 50,200,200); // oval. contains (100, 55,205,205); // canvas. drawArc (oval, 180,180, true, paint); canvas. drawArc (oval, 180,180, false, paint );}
If it is true
canvas.drawArc(oval, 180, 180, true, paint);

 

If it is false

canvas.drawArc(oval, 180, 180, false, paint);

 

8: Draw a polygon.Path1.quadTo (x1, y1, x2, y2) // X axis of the first circle of x1, and Y axis of the first circle of y1. X2 end X axis position, end y position

 

Protected void onDraw (Canvas canvas) {super. onDraw (canvas); Paint paint = new Paint (); canvas. drawColor (Color. YELLOW); paint. setColor (Color. BLACK); paint. setTextSize (20); paint. setTextScaleX (0.5f); canvas. drawText ("Draw polygon:", 50, 50, paint); paint. setColor (Color. RED); paint. setStrokeWidth (3); Path path = new Path (); paint. setStyle (Style. STROKE); // set the hollow path. moveTo (120, 50); // start point path. lineTo (190,100); // The first line path. l IneTo (190, 50); // second line path. lineTo (120, 50); // path of the third line. close (); // close canvas. drawPath (path, paint); Path path1 = new Path (); paint. setColor (Color. BLUE); // path1.addPath (path); path1.moveTo (100,150); // start point path1.quadTo (10,280, 10,350); // path1.quadTo (x1, y1, x2, y2) // X axis of the first circle of x1 and Y axis of the first circle of y1. X2 end X axis position, end y position path1.setLastPoint (90, 60); canvas. drawPath (path1, 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.