Android uses canvas to draw TextView, bitmap, rectangles (clipping), ellipses, lines, dots, arcs

Source: Internet
Author: User
Tags drawtext

Initializing objects

Private Paint mpaint;//Brush private int count;//Click the number of private Rect rect;//Rectangle public Counstomview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);//initialization Brush mpaint = new Paint (); rect = new Rect (); setonclic Klistener (this);}

1. Draw TextView. and set clickable. //Rendered text, canvas class In addition to the above can also depict text, parameter one is a string type of text, parameter two x axis, parameter three Y axis, parameter four 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 Rectangle Mpaint.setcolor ( color.red); mpaint.settextsize (25.0f); Text = string.valueof (count); mpaint.gettextbounds (text, 0, text.length (), rect);//The length and width of the content.  Add to the rect rectangle float width = rect.width ();//get wide float height = rect.height (); Canvas.drawtext (text, getwidth ()/2  -width/ 2, GetHeight ()/2 + HEIGHT/2, mpaint);//Set text position//} @Overridepublic void OnClick (View arg0) {count++;invalidate ();//Redraw}

2. Draw a simple bitmap. parameter One is our regular bitmap object, parameter two is the source area (here is bitmap), the parameter three is the target area (should be in the canvas location and size), parameter four is the paint brush object, because the use of scaling and stretching possible, There is a significant loss of performance when the original Rect is not equal to the target rect.

protected void OnDraw (canvas canvas) {super.ondraw (canvas);//left and upper positions bitmap bitmap = Bitmapfactory.decoderesource ( Getresources (), R.drawable.login_backgroud); Canvas.drawbitmap (bitmap,0,0, mpaint);}


3. Using Canvas.drawbitmap (bitmap, SRC, DST, paint) to draw the trimmed bitmap//Parameter one is our regular bitmap object, parameter two is the source area (here is bitmap), the parameter three is the target area (should be in the canvas location and size), parameter four is the paint brush object, because it is possible to use scaling and stretching, when the original rect is not equal to the target rect, there will be a significant loss in performance.

protected void OnDraw (canvas canvas) {super.ondraw (canvas);//left and upper positions bitmap bitmap = Bitmapfactory.decoderesource ( Getresources (), R.drawable.login_backgroud); Paint paint = new paint (), Canvas.save (),//left----distance from screen ... Top------distance from ... Right-----the width of the rectangle//buttom-----The height of the rectangle rect rect = new Rect (ten, Ten, and a); Canvas.cliprect (rect); Sets the cropping area canvas.drawcolor (color.blue);//The Rect of the clipping region changes to a blue rect rec = new Rect (A, a, a, a), Canvas.drawbitmap (bitmap, Rect, REC, paint); Canvas.restore ();}




4: Draw ellipse with Canvas.drawoval. draw Ellipse, parameter one is scan area, parameter two is Paint object;

protected void OnDraw (canvas canvas) {super.ondraw (canvas);//left and offset position paint paint = new paint (); Canvas.save ();// Left----distance from screen ... a//top------distance from top of ... b//Right-----The width of the ellipse ..... c//buttom-----The height of the ellipse ... DRECTF DST = new RECTF (10,10,300, n);//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. //Draw point, parameter one horizontal x axis, parameter two vertical y axis, third parameter is paint object.

@Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); Paint paint = new paint (); Canvas.save (); Canvas.drawcolor (color.red);//Background painting background canvas.restore (); Canvas.save (); Paint.setcolor (Color.Black);p aint.settextsize (20.0f), Canvas.drawtext ("Draw point:", ten, all, paint);//Text Canvas.restore (); Canvas.save ();p aint.setcolor (color.green);p aint.setstrokewidth (20.0f);//Set the size of the point Canvas.drawpoint ((), n, paint); /parameter one horizontal x axis, parameter two vertical y-axis, and the third parameter is the Paint object. Canvas.drawpoints (New float[]{60,400,65,400,70,40}, paint);//Draw multiple points canvas.restore (); Canvas.save ();p Aint.setcolor (Color.Black);p Aint.setstyle (Style.fill), Canvas.drawtext ("This can be used as a dot:", "Ten,", "paint);p Aint.setantialias (true); /removal of anti-aliasing Paint.setcolor (Color.yellow), Canvas.drawcircle (n, x, c, paint);//parameter one horizontal × axis, parameter two vertical y-axis, parameter three circle radius, Parameter four Paint object Canvas.restore ();}



6: Draw the line. drawLine (float StartX, float starty, float stopx, float stopy, paintpaint)//parameter The x-axis position of the starting point, the y-axis position of the parameter two starting point, The x-axis horizontal position of the three ends of the parameter, parameter four vertical position of the y-axis, and the last parameter is the paint brush object.

protected void OnDraw (canvas canvas) {super.ondraw (canvas); Paint paint = new paint (); Canvas.drawcolor (Color.yellow);p Aint.setantialias (TRUE);//anti-aliasing Paint.setdither (true);//Jitter, Make the graphic look without the raw edge Paint.setcolor (color.red);p aint.setstrokewidth (3);//Set the thickness of the line canvas.drawline (A, a, a, a, and a paint);// The x-axis position of the starting point of the parameter, the y-axis position of the parameter's two starting points, the x-axis horizontal position of the parameter's three endpoints, the vertical position of the parameter four y-axis, and the last parameter is the paint brush object. }



7: Draw an arc:

Parameter one is the RECTF object, the boundary of a rectangular area ellipse is used to define the shape, size, arc, parameter two is the starting angle (degrees) at the beginning of the arc,

Parameter three sweep angle (degrees) begins to measure clockwise, parameter four is if this is true, including the Oval center arc, and close it if it is false it will be an arc, parameter five is the Paint object;


protected void OnDraw (canvas canvas) {super.ondraw (canvas); Paint paint = new paint (); Canvas.drawcolor (color.blue);//Arc Paint.setstyle (Style.stroke);//Set Hollow Paint.setcolor ( color.red);//Set the color Paint.setstrokewidth (3);//Set the thickness RECTF oval = new RECTF (); Oval.set ((),//oval.contains ( //canvas.drawarc (Oval, 205, 205, true, paint); Canvas.drawarc (oval, N-,-N, false, paint);}
When True
Canvas.drawarc (oval, N, +, True, paint);

When False

Canvas.drawarc (Oval, N., +, FALSE, paint);


Android uses canvas to draw TextView, bitmap, rectangles (clipping), ellipses, lines, dots, arcs

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.