Some summary of Android custom view

Source: Internet
Author: User

Some summary of Android custom view
    1. Paint and Canvas
      Let's start with the basic setup function of paint:
      Paint.setantialias (TRUE);//anti-aliasing function
      Paint.setcolor (color.red); Set Brush color
      Paint.setstyle (Style.fill);//Set Fill style
      Paint.setstrokewidth (30);//Set Brush width
      Paint.setshadowlayer (ten, Color.green);//Set Shadow
      (1) void SetStyle (Paint.style style) set fill style
      Paint.Style.FILL: Filling the interior
      Paint.Style.FILL_AND_STROKE: Filling interior and stroke
      Paint.Style.STROKE: Stroke only
      Look at the difference between these three types, here is an example of a circle drawn:

      (2) Setshadoelayer (float radius,float dx,float dy,int color) Add Shadow
      Parameters:
      Radius: The tilt of the shadow
      DX: Horizontal Displacement
      DY: Vertical Displacement
      Using code:
paint.setShadowLayer(101515, Color.GREEN);//设置阴影 


Then the basic settings for the canvas are:

Canvas Background settings:

Canvas.drawcolor (Color.Blue);
Canvas.drawrgb (255, 255, 0); These two functions are all used to set the background color.

(3) Basic geometry drawing
1. Draw a straight line

void DrawLine (float StartX, float starty, float stopx, float stopy, paint paint)

Parameters:
StartX: Start point x coordinate
Starty: Start point y coordinate
STOPX: End point x coordinate
Stopy: End point Y coordinate

Paint paint=new Paint();paint.setColor(Color.RED);  //设置画笔颜色    paint.setStyle(Style.FILL);//设置填充样式 paint.setStrokeWidth(5);//设置画笔宽度canvas.drawLine(100100200200, paint);

2. Multiple lines
void DrawLines (float[] pts, paint paint)
void DrawLines (float[] pts, int offset, int count, paint pain T)
Parameters:
pts: A collection of points, as you can see below, instead of forming a connecting line, a straight line is formed every two points, and the PTS are organized in {X1,y1,x2,y2,x3,y3,...... }

Paint paint=new Paint();paint.setColor(Color.RED); //设置画笔颜色 paint.setStyle(Style.FILL);//设置填充样式 paint.setStrokeWidth(5);//设置画笔宽度float []pts={10,10,100,100,200,200,400,400};canvas.drawLines(pts, paint);

3. Point
void Drawpoint (float x, float y, paint paint)

Parameters:
Float x: The x-coordinate of the point
Float y: The y-coordinate of the point

Paint paint=new Paint();paint.setColor(Color.RED);  //设置画笔颜色    paint.setStyle(Style.FILL);//设置填充样式 paint.setStrokeWidth(15);//设置画笔宽度canvas.drawPoint(100100, paint);


4, multiple points
void Drawpoints (float[] pts, paint paint)
void Drawpoints (float[] pts, int offset, int count, paint paint)

Parameters:
Float[] pts: The collection of points, along with the line above, with the style {X1,y1,x2,y2,x3,y3,...... }
int offset: The number of skipped values in the collection, note not the number of points! A point is a value of two;
Count: Number of values drawn, refers to the number of people in pts[], not the number of points, because a point is two values

The following examples illustrate the meaning of the above offset and count: (Skip the first point, draw the back two points, the fourth point does not draw), note that a point is two values!

Paint paint=new Paint ();Paint. SetColor(Color. RED); Set Brush ColorPaint. SetStyle(Style. FILL);//Set Fill stylePaint. Setstrokewidth( the);//Set Brush widthfloat []pts={Ten,Ten, -, -, $, $, -, -};Canvas. Drawpoints(PTS,2,4, paint);


5. Rectangle Tool class RECTF and rect

These two are rectangular auxiliary class, the difference is not big, with any line, according to four points to construct a rectangular structure, in the drawing, the use of this rectangular structure can draw the corresponding rectangle or with other graphics region intersect, add and so on;

RECTF:

The constructor has the following four, but the most commonly used is the second one, based on four points to construct a rectangle;

RECTF ()
RECTF (float left, float top, float right, float bottom)
RECTF (RECTF R)
RECTF (Rect R)

Rect

The constructor is as follows, and the most common is to construct a rectangle from four points

Rect ()
Rect (int left, int top, int. right, int bottom)
Rect (rect R)

6. Rectangle

void DrawRect (float left, float top, float right, float bottom, paint paint)
void DrawRect (RECTF rect, paint paint)
void DrawRect (Rect r, Paint paint)

Parameters:

The first one is to directly pass in the rectangle's four points, draw the rectangle

The second to third constructor specifies the rectangle to be drawn based on the passed-in RECTF or rect rectangle variable.

Paint paint=NewPaint ();p aint.setcolor (color.red);//Set Brush colorPaint.setstyle (Style.fill);//Set Fill stylePaint.setstrokewidth ( the);//Set Brush widthCanvas.drawrect (Ten,Ten, -, -, paint);//Direct ConstructionRECTF rect =NewRECTF ( -,Ten, About, -); Canvas.drawrect (rect, paint);//Using RECTF constructionRect Rect2 =NewRect ( the,Ten, the, -); Canvas.drawrect (rect2, paint);//Using a rect construct

7. Rounded Corners Rectangle

void Drawroundrect (RECTF rect, float rx, float ry, paint paint)

Parameters:
RECTF rect: the rectangle to draw
Float RX: The x-axis radius of the ellipse that generated the fillet
Float ry: The y-axis radius of the ellipse that generated the fillet

Paint paint=new Paint();paint.setColor(Color.RED);  //设置画笔颜色    paint.setStyle(Style.FILL);//设置填充样式 paint.setStrokeWidth(15);//设置画笔宽度RectF rect = new RectF(10010300100);canvas.drawRoundRect2010, paint);

8, round

void Drawcircle (float cx, float CY, float radius, paint paint)

Parameters:
Float CX: Center point x axis coordinates
FLOAT CY: center point y axis coordinates
Float radius: radius of Circle

Paint paint=new Paint();paint.setColor(Color.RED);  //设置画笔颜色    paint.setStyle(Style.FILL);//设置填充样式 paint.setStrokeWidth(15);//设置画笔宽度canvas.drawCircle(150150100, paint);

9. Ellipse

Ellipses are created from rectangles, with the x-axis of the rectangle as the ellipse, the width of the rectangle as the y-axis of the ellipse, and the oval shape

void DrawOval (RECTF oval, paint paint)

Parameters:
RECTF Oval: The rectangle used to create the ellipse

Paint paint=new Paint ();Paint. SetColor(Color. RED); Set Brush ColorPaint. SetStyle(Style. STROKE);//Fill style change to strokePaint. Setstrokewidth(5);//Set Brush widthRECTF rect = new RECTF ( -,Ten, -, -);Canvas. DrawRect(Rect, paint);//Draw a rectanglePaint. SetColor(Color. GREEN);//change brush colorCanvas. DrawOval(Rect, paint);//the same rectangle drawing ellipse


10, Arc

Arcs are part of the ellipse, and the ellipse is generated from the rectangle, so the arc is, of course, based on the rectangle;

void DrawArc (RECTF oval, float startangle, float sweepAngle, Boolean usecenter, Paint paint)

Parameters:
RECTF Oval: Ellipse-generated rectangle
Float startangle: The angle at which the arc starts, in the positive direction of the x-axis is 0 degrees
Float SweepAngle: ARC-Continuous angle
Boolean Usecenter: Whether there are arcs on both sides, True, also on both sides, False, only one arc
(1) Set brush as stroke, effect:

Paint paint=NewPaint ();p aint.setcolor (color.red);//Set Brush colorPaint.setstyle (Style.stroke);//Fill style changes to strokePaint.setstrokewidth (5);//Set Brush widthRECTF Rect1 =NewRECTF ( -,Ten, -, -); Canvas.drawarc (Rect1,0, -,true, paint); RECTF Rect2 =NewRECTF ( -,Ten, -, -); Canvas.drawarc (Rect2,0, -,false, paint);


(b), set the brush to fill

Paint paint=NewPaint ();p aint.setcolor (color.red);//Set Brush colorPaint.setstyle (Style.fill);//Fill style changes to strokePaint.setstrokewidth (5);//Set Brush widthRECTF Rect1 =NewRECTF ( -,Ten, -, -); Canvas.drawarc (Rect1,0, -,true, paint); RECTF Rect2 =NewRECTF ( -,Ten, -, -); Canvas.drawarc (Rect2,0, -,false, paint);

(4) Text
1. Paint Related Settings

//General SettingsPaint.setstrokewidth (5);//Set Brush widthPaint.setantialias (true);//Specifies whether antialiasing is used, which slows down the drawing speed if usedPaint.setstyle (Paint.Style.FILL);//Drawing style, valid for setting text and geometryPaint.settextalign (Align.center);//Set text alignment, value: Align. CENTER, align. Left or Align.rightPaint.settextsize ( A);//Set Text size//Style settingsPaint.setfakeboldtext (true);//Set whether to bold textPaint.setunderlinetext (true);//Set underlinePaint.settextskewx ((float) -0.25);//Set font horizontal tilt, normal italic word is -0.25Paint.setstrikethrutext (true);//Set with strikethrough effect//Other SettingsPaint.settextscalex (2);//will only stretch horizontally, and the height will not change

2.canvas Drawing method
1), normal level drawing
constructor function:

void DrawText (String text, float x, float y, paint paint)
void DrawText (charsequence text, int start, int end, float x, float y, paint paint)
void DrawText (String text, int start, int end, float x, float y, paint paint)
void DrawText (char[] text, int index, int count, float x, float y, paint paint)

Description
The first constructor: the most common simple constructor;
Third to fourth constructor: the implementation of the interception of a portion of the font to the graph;
The second constructor: the most powerful, because the incoming can be charsequence type font, so you can draw with the image of the extended text (to be continued), but also to intercept a portion of the drawing

Some summary of Android custom view

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.