Draw a view and draw three views based on the axis Map

Source: Internet
Author: User

Draw a view and draw three views based on the axis Map

1. Three plotting methods:

In a custom view, three functions can be rewritten for interface painting. During the view creation process, the execution sequence of the three functions is onLayout, onDraw, and dispatchDraw.
1. onLayout (boolean changed, int left, int top, int right, int bottom ):
OnLayout is used to locate the position of the view in the upper-level view. It can be seen from its parameters. Since this function does not have a canvas, it is only suitable for drawing ready-made View Controls.
2. onDraw (Canvas canvas ):
A custom control is generally used to override the onDraw method and draw various images on the canvas.
3. dispatchDraw (Canvas canvas ):
The difference between dispatchDraw and onDraw is that the onDraw is before the lower-level view is drawn, and the dispatchDraw is after the lower-level view is drawn, so if you do not want to overwrite the lower-level view, you need to draw in dispatchDraw. For convenience of memory, as long as the view derived from ViewGroup uses dispatchDraw, and other widgets use onDraw

 

2. Use of the Canvas

Canvas is a graphical operation class provided by Android. The following lists the common methods of Canvas:

① Define the area to be drawn (crop area)

Although all areas in this view can be drawn, sometimes we only want to draw in a circular area or within a rectangle area, you must specify the size of the area to be drawn before drawing.
ClipPath: Crop an irregular curve area.
ClipRect: crop the rectangular area
ClipRegion: Crop a combined area

 

② Draw images within the region

DrawArc: Draw slices
DrawBitmap: Drawing Images
DrawCircle: Draw a circle
DrawLine: draw a straight line
DrawOval: draw an ellipse
DrawPath: Draw a path, that is, an irregular curve.
DrawPoint: drawing point
DrawRect: Draw a rectangle
DrawRoundRect: Draw a rounded rectangle
DrawText: Draw text

③ Move the entire canvas

Rotate: rotate the canvas
Scale: Zoom the canvas
Translate: Pan the canvas

④ Access canvas status

Different Canvas painting operations affect each other. For example, if we want to rotate the entire Canvas, except for a straight line (that is, the straight line remains unchanged), if there is no status mechanism, this line can only be rotated. With the status mechanism, we can save the canvas status (Save the rotation operation) before drawing the straight line, draw a straight line, and then restore the canvas status. In this way, no image operations other than the save-restore code will be affected for any image drawn between the save-restore code.
Save: save the canvas status.
Restore: restore canvas status

⑤ Usage of Paint brush

In the above graphic function, you must specify the Paint, which defines the Paint color, style, width, shadow, underline, and so on. Common Paint operations include:
SetAntiAlias: Used to set whether to use the anti-aliasing function. It is mainly used to draw curves such as circles.
SetColor: Set the paint brush color.
SetShader: Set the gradient effect of the paint brush.
SetShadowLayer: Set the shadow of the paint brush.
SetStyle: Set the paint brush style (line or fill)
SetStrokeWidth: Set the line width.
SetUnderlineText: Set the underline of Text
SetStrikeThruText: Set the text strikethrough

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.