Android uses canvas to draw arrows

Source: Internet
Author: User

It is too troublesome to draw arrows. I began to draw arrows and rotate them at the specified point. However, the effect has been poor. I want to use a mathematical method to draw a picture, but I find that the calculation is very complicated. So Google, found a middleware using Java when AWT implementation of the draw arrow (http://www.bangchui.org/simple? T16755.html), so I borrowed it and changed it. The result is actually usable. The results cannot be exclusive. Here we will share with you:

Public class mycanvas extends view {private canvas mycanvas; private paint mypaint = new paint (); Public mycanvas (context) {super (context ); // todo auto-generated constructor stub} public mycanvas (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle ); // todo auto-generated constructor stub} public mycanvas (context, attributeset attrs) {super (context, attrs ); // todo auto-generated constructor stub} @ overrideprotected void ondraw (canvas) {// todo auto-generated method stubsuper. ondraw (canvas); this. mycanvas = canvas; drawal (0, 0,100,100);}/*** set the paint brush default style */Public void setpaintdefaultstyle () {mypaint. setantialias (true); mypaint. setcolor (color. red); mypaint. setstyle (paint. style. stroke); mypaint. setstrokewidth (3);}/** circle * @ Param x coordinate * @ Param YY coordinate * @ Param radius circle radius */Public void drawcircle (float X, float y, float radius) {mycanvas. drawcircle (X, Y, radius, mypaint); invalidate ();} /*** draw a straight line * @ Param fromx start point X coordinate * @ Param fromy start point y coordinate * @ Param tox end point X coordinate * @ Param toy end point y coordinate */Public void drawline (float fromx, float fromy, float tox, float toy) {path linepath = New Path (); linepath. moveTo (fromx, fromy); linepath. lineto (Tox, toy); linepath. close (); mycanvas. drawpath (linepath, mypaint); invalidate ();} /*** draw an arrow * @ Param SX * @ Param Sy * @ Param ex * @ Param ey */Public void drawal (INT Sx, int Sy, int ex, int ey) {double H = 8; // arrow height double L = 3.5; // half of the bottom edge int X3 = 0; int Y3 = 0; int X4 = 0; int Y4 = 0; double awrad = math. atan (L/h); // arrow angle double arraow_len = math. SQRT (L * L + H * H); // double [] arrxy_1 = rotatevec (ex-Sx, ey-sy, awrad, true, arraow_len ); double [] arrxy_2 = rotatevec (ex-Sx, ey-sy,-awrad, true, arraow_len); double X_3 = ex-arrxy_1 [0]; // (X3, Y3) is the first endpoint of double y_3 = ey-arrxy_1 [1]; double x_4 = ex-arrxy_2 [0]; // (X4, Y4) is the second endpoint double y_4 = ey-arrxy_2 [1]; double X3 = new double (X_3); X3 = x3.intvalue (); double Y3 = new double (y_3 ); y3 = y3.intvalue (); double X4 = new double (x_4); X4 = x4.intvalue (); double Y4 = new double (y_4); Y4 = y4.intvalue (); // draw mycanvas. drawline (sx, Sy, ex, ey, mypaint); Path triangle = New Path (); triangle. moveTo (ex, ey); triangle. lineto (X3, Y3); triangle. lineto (X4, Y4); triangle. close (); mycanvas. drawpath (triangle, mypaint);} // calculate public double [] rotatevec (int px, int py, double Ang, Boolean ischlen, double newlen) {double mathstr [] = new double [2]; // vector rotation function, the parameter meanings are X, Y, rotation angle, whether the length is changed, and the new length is double vx = px * Math. cos (ANG)-py * Math. sin (ANG); double Vy = px * Math. sin (ANG) + py * Math. cos (ANG); If (ischlen) {double D = math. SQRT (VX * VX + Vy * Vy); vx = vx/D * newlen; Vy = Vy/D * newlen; mathstr [0] = VX; mathstr [1] = Vy;} return mathstr ;}}

Shows the display effect:

Hope to help you!

Related Article

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.