Android Custom Control Leading Basics Learning (i)--canvas

Source: Internet
Author: User

Overview:

We often encounter features that need to be implemented using paint. For example, some common geometric shapes-points, lines, arcs, circles, ellipses, text, rectangles, polygons, curves, rounded rectangles-can never encounter a geometric figure instead. So our cell phone will certainly be too unbearable. Even if we can now use a few images to solve some of the current problems, these are nothing to say about the "changeable" needs. For this, we can use canvas drawing technology to solve. Let's take the mystery of canvas together.


Example Programming:1. Draw a circle

First look at the show:


Implementing the Key code:

private void Drawcirle (canvas canvas) {canvas.drawcircle (n, a, $,            paint);        }

2. Plot the arc area

Show:


Implementing the Key code:

private void Drawcuttingcirle (canvas canvas) {            RECTF rect = new RECTF (0, 0, +, +);            Canvas.drawarc (Rect,//ARC uses a rectangular area size of                    0,//Start angle                    90,//Sweep angle                    false,//whether to use center                    paint);        }


3. Draw 1/4 Circle

Show:


Implementing the Key code:

private void Drawquartercirle (canvas canvas) {            RECTF rect = new RECTF (0, 0, +);            Canvas.drawarc (Rect,//ARC used for rectangular area size                    0,//Start angle                    90,//Sweep angle true,//                    whether to use center                    paint);        }

4. Draw a line

Show:


Implementing the Key code:

private void DrawLine (canvas canvas) {            canvas.drawline (ten, ten, +, +, paint);        }

5. Draw an ellipse

Show:


Implementing the Key code:

private void DrawOval (canvas canvas) {            //define a rectangular area            RECTF oval = new RECTF (5, +, +);            Rectangular area inner Tangent oval            canvas.drawoval (oval, paint);        }


6. Draw a rectangle

Show:


Implementing the Key code:

private void DrawRect (canvas canvas) {            RECTF rect = new RECTF (.);            Canvas.drawrect (rect, paint);        }

7. Draw rounded rectangles

Show:


Implementing the Key code:

private void Drawsmoothrect (canvas canvas) {            RECTF rect = new RECTF (.);            Canvas.drawroundrect (rect, +,//x-axis radius of (                    )///y-Axis radius                    paint);        }

8. Drawing polygons

Show:


Implementing the Key code:

private void DrawPolygon (canvas canvas) {            Path Path = new path ();//define a path            Path.moveto (10, 10);//move to coordinates 10,10
   path.lineto (1360);            Path.lineto (960, 720);            Path.lineto (a);            Path.lineto (ten);            Canvas.drawpath (path, paint);        }

9. Draw the Compass

Show:


Implementing the Key code:

private void Drawcompass (canvas canvas) {Paint.setantialias (true);            Paint.setstyle (Style.stroke); Canvas.translate (Canvas.getwidth ()/2, 500); Pan Compass canvas.drawcircle (0, 0, $, paint);            Draw a circle//use path to draw the route text Canvas.save ();            Canvas.translate (-155,-155);            Path PATH = new Path ();            Path.addarc (New RECTF (0, 0, 300, 300),-180, 180);            Paint Citepaint = new paint (paint);            Citepaint.settextsize (30);            Citepaint.setstrokewidth (1);            Canvas.drawtextonpath ("Http://blog.csdn.net/lemon_tree", path, 0, citepaint);            Canvas.restore (); Paint Tmppaint = new paint (paint);            Small scale Brush Object Tmppaint.setstrokewidth (2);            Tmppaint.settextsize (30);            Float y = 200; int count = 60; Total number of ticks for (int i = 0; i < count; i++) {if (i% 5 = = 0) {Canvas.drawlin E (0f, y, 0, y+ 20f, paint);                Canvas.drawtext (string.valueof (I/5 + 1), -4f, y + 55f, tmppaint);                } else {canvas.drawline (0f, y, 0f, y + 15f, tmppaint); } canvas.rotate (360/count, 0f, 0f);            Rotate the drawing paper}//Draw the pointer Tmppaint.setcolor (Color.gray);            Tmppaint.setstrokewidth (4);            Canvas.drawcircle (0, 0, tmppaint);            Tmppaint.setstyle (Style.fill);                        Tmppaint.setcolor (Color.yellow);            Canvas.drawcircle (0, 0, 5, tmppaint);        Canvas.drawline (0, 0, -135, paint); }
In the above code, I don't know if everyone seems to feel a bit of a mess, but it doesn't matter, in the next blog I will take this piece alone to say. I will use a custom dynamic clock as an example to detail this part of the content, we have a little to be impatient.


10. Record the path of the finger motion

Show:


Implementing the Key code:

This part of the functional implementation is mainly to consider three parts: Record path (here is a separate variable to save the reason is because there will be a redraw process, it will need to erase and redraw, if not save the previous path, which may cause the previous path is missing), log events, draw.

Get the path to the record:

Private arraylist<pointf> graphics = new arraylist<pointf> ();
Touch events:

public boolean ontouchevent (Motionevent event) {            Graphics.add (new PointF (Event.getx (), Event.gety ()));            Invalidate (); Redraw area            return true;        }
Draw:

private void Drawfingerpath (canvas canvas) {            for (PointF point:graphics) {                canvas.drawpoint (point.x, Point.y, p aint);            }        }

Source Download:

(Note: The source code in addition to the demonstration of the use of the set of the drawing curve, the positive/cosine function, the use of Surfaceview, heartbeat. PS: Heartbeat example needs to tap screen to trigger)

http://download.csdn.net/detail/u013761665/8423823

Android Custom Control Leading Basics Learning (i)--canvas

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.