Illustration of Android UI effects (ii) _android

Source: Internet
Author: User
Tags sin cos

First, Canvas
There are a lot of methods in canvas, here we only pick the usual explanations
The objects that canvas can draw are:

    • ARC (arcs) canvas.
    • Fill Color (ARGB and color)
    • Bitmap
    • Round (circle and Oval)
    • Dot (point)
    • Lines (line)
    • Rectangle (Rect)
    • Pictures (picture)
    • Rounded Rectangle (roundrect)
    • Texts (text)
    • Vertex (vertices)
    • Paths (PATH)

Draw Arcs

/**
  * Draw arcs *
  @param oval Area
  * @param the angle that the startangle starts to draw
  * @param the angle of the sweepangle end drawing
  * @param usecent Does ER use center
  * @param paint brush
 /public void DrawArc (RECTF oval, float startangle, float Sweepangle,boolean UseC Enter, Paint Paint)

The first three parameters are better understood, the key is the third parameter usecenter, look at the picture you will understand

It can be found that when the usecenter=true, the area of the arc is the point of the starting angle and the point of the end angle and the center point of the connection, and Usecenter=false, the Arc area is the point of the starting angle and the point of the end angle directly connected to the area.

Draw Path

Mpaint.setstyle (Paint.Style.STROKE);
  Mpaint.setstrokewidth (3);
  Path PATH = new Path ();
  Path.moveto (5);
  Path.lineto (from);
  Path.lineto (a);
  Path.lineto (231);
  Canvas.drawpath (path, mpaint);

The drawing path is simpler, add the corresponding coordinate points in turn, and note that the anti-aliasing and style of the brush are set.
-Draw a text path

 Mpaint.setstyle (Paint.Style.STROKE);
  Mpaint.setstrokewidth (3);
  Path PATH = new Path ();
  Path.moveto (m);
  Path.lineto (in);
  Path.lineto (450);
  Path.lineto (650);
  Mpaint.settextsize (+);
  Canvas.drawtextonpath ("hi_xiaoyu_bolg_in_android", path, 0, 0, mpaint);

This is the same as the drawing path, note that the size is set and the brush thickness is available.

/**
  * Draw round
  * @param CX  Center x coordinate
  * @param Cy  Center y coordinate
  * @param radius of radius
  * @param paint
 * * public void Drawcircle (float cx, float CY, float radius, Paint Paint) {
 } 

  //Draw round
  canvas.drawcircle (50, 50, 5 0, Mpaint);
  RECTF r = new RECTF (0, 270,);
  Draw Rectangular
  Canvas.drawrect (R, Mpaint);
  RECTF Oval = new RECTF (0, EUR, 270);
  Draw Elliptical
  Canvas.drawoval (oval, mpaint);
  RECTF rect = new RECTF (MB, 170,);
  Draw rounded corners of rectangular
  canvas.drawroundrect (rect, mpaint);
  Draw line
  Canvas.drawline (in), Mpaint,

Other drawxxx methods here are no longer to repeat, you can try one by one.
Here's a look at some of the more important methods in canvas:

Canvas.save ();
Canvas.restore ();
Canvas.translate (dx, dy);
Canvas.rotate (degrees);
Canvas.savelayer (bounds, paint, saveflags);

Save (): Saves the current drawn image so that subsequent operations are equivalent to operations on a new layer.
Restore (): Merge the layer before Sava () and the layer after Save
Translate (): Canvas translation
Rotate (): Canvas rotation
You may have questions, Since we can use the coordinate system to locate all the coordinate points in the canvas, why do we want the canvas translation and the canvas rotation API? Now suppose there is such a demand, you need to draw a dashboard, and draw the scale of the dashboard we can use trigonometric sin cos to calculate the corresponding offset angle, of course, this is easy for the mathematical Couba, the General people need to turn the trigonometric function formula, so Google engineers, For us to implement the translation and rotation of the API, encountered similar to the dashboard effect is, only need to rotate the current canvas to achieve the corresponding effect, saving us the calculation and work.

Paint.setcolor (0XFF00CCFF);
  Paint.setantialias (true);
  Paint.setstyle (Style.stroke);
  Canvas.translate (Canvas.getwidth ()/2, 200); Move the location of the paper coordinates point, not every time from the coordinate Origin calculation
  canvas.drawcircle (0, 0,, paint); 
  Paint tmppaint = new Paint (Paint); 
  Tmppaint.setstrokewidth (1);
  Float y = m;
  int count = 60; Total number of ticks for
  (int i = 0; i < count; i++) {
   if (i% 5 = 0) {
    canvas.drawline (0f, y, 0, y + 12f, paint); 
   canvas.drawtext (string.valueof (I/5 + 1), -4f, y + 25f,
      tmppaint);
   } else {
    canvas.drawline (0f, y, 0f, Y + 5f, tmppaint);
   }
   Canvas.rotate (360/count, 0f, 0f); Rotate paper
  //Draw pointer
  paint.setstrokewidth (2);
  Canvas.drawline (0, 0, -65, paint);
  Paint.setstrokewidth (4);
  Canvas.drawline (0, -35, paint);

These are the second part of the UI effect design module in Android development, and will continue to be updated to help you learn.

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.