Introduction to the drawing class in Android path

Source: Internet
Author: User
Tags drawtext

Paint class-related properties:


/**       * Paint Class Introduction * * Paint is a brush, play a very important role in the drawing process, the brush mainly save the color, * style and other drawing information, specify how to draw text and graphics, brush objects have many settings, *              In general, it can be divided into two categories, one is related to drawing, the other is related to text drawing.       * 1. Graphic drawing * SETARGB (int a,int r,int g,int b);       * Set the color to be drawn, a for transparency, and r,g,b for the color value.       * * SETALPHA (int a);       * Set the transparency of the drawing drawing.       * * SetColor (int color);       * Sets the color of the drawing, which is represented by a color value that includes transparency and RGB colors.       * * Setantialias (boolean AA);       * Set whether anti-aliasing is used, consumes large resources, and the drawing speed slows down.       * * Setdither (Boolean dither);       * Set whether to use Image jitter processing, will make the picture color more smooth and full, the image more clear * * Setfilterbitmap (boolean filter); * If the item is set to True, the image will filter out the optimized operation of the bitmap image during animation, speed up the display speed, this setting depends on the settings of dither and Xfermode * Setmaskfilter (maskfilt       Er maskfilter);       * Set Maskfilter, you can use different maskfilter to achieve the effect of the filter, such as filtering, stereoscopic etc * * Setcolorfilter (colorfilter colorfilter);       * Set the color filter, can be used when drawing colors without color conversion effect * Setpatheffect (patheffect effect); * Set the effect of drawing path, such as point drawing line, etc. *Setshader (Shader Shader);       * Set the image effect, use shader can draw a variety of gradient effect * * Setshadowlayer (float radius, float dx,float dy,int color);       * Set the shadow layer below the graph, create a shadow effect, radius for the angle of the shadow, DX and dy for the shadow on the x-axis and the y-axis distance, color for the shadow of the colors * * SetStyle (Paint.style Style);       * Set the style of the brush for Fill,fill_or_stroke, or STROKE * * SETSTROKECAP (paint.cap Cap); * When the brush style is stroke or fill_or_stroke, set the brush's graphic style, such as circular style * cap.round, or square style Cap.square * * Setsrokejoin (Paint.join       Join);       * Set the drawing when the combination of the graphics, such as smoothing effect, such as * * Setstrokewidth (float width);       * When the brush style is stroke or fill_or_stroke, set the brush's thickness * * Setxfermode (Xfermode xfermode);       * Set the process of drawing overlap, such as merging, taking intersection or union, often used to make eraser effect * * 2. Text Draw * Setfakeboldtext (Boolean fakeboldtext);       * Simulation to implement bold text, set in small font effect will be very poor * * Setsubpixeltext (Boolean subpixeltext);       * Setting this to True will help to display the text on the LCD screen * * settextalign (paint.align Align); * Set the alignment direction of the drawing text * * Settextscalex (float ScaleX);       * Set the scale of the drawing text x axis, can achieve the effect of the text stretching * * Settextsize (float textSize);       * Set the font size of the drawn text * * SETTEXTSKEWX (float skewx);       * Set italic text, skewx for oblique radian * * Settypeface (Typeface Typeface);       * Set typeface object, that is, font style, including bold, italic and liner body, non-liner body, etc. * Setunderlinetext (Boolean underlinetext);       * Set the underlined text effect * * Setstrikethrutext (Boolean strikethrutext);         * Set the effect with strikethrough **/

Andoird customizing View-related representations

Private classMyView2extendsView { PublicMyView2 (Context context) {Super(context); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas);            Canvas.drawcolor (Color.White); Paint Paint=NewPaint (); Paint.setantialias (true);            Paint.setcolor (color.red); Paint.setstyle (Paint.Style.STROKE);//set to HollowPaint.setstrokewidth (3); Canvas.drawcircle (40, 40, 30, paint); Canvas.drawrect (10, 90, 70, 150, paint); Canvas.drawrect (10, 170, 70, 200, paint); Canvas.drawoval (NewRECTF (10, 220, 70, 250), paint); Path Path=NewPath ();//TrianglesPath.moveto (10, 330); Path.lineto (70, 330); Path.lineto (40, 270);            Path.close ();            Canvas.drawpath (path, paint); Path path1=NewPath ();//trapezoidalPath1.moveto (10, 410);//painting Base PointPath1.lineto (70, 410); Path1.lineto (55, 350); Path1.lineto (25, 350); Path1.close ();//connect the starting point and the last point together to form a closed graph            /** The most important is movtto and close, if it is Style.fill, do not set close, there is no difference, but if it is stroke mode, * If not set close, the graphics are not closed.             * * Of course, you can also not set close, and then add a line, the same effect. */Canvas.drawpath (path1, paint); ///////////////////////////////////////second columnPaint.setcolor (Color.Blue); Paint.setstyle (Paint.Style.FILL);//Set SolidCanvas.drawcircle (120, 40, 30, paint); Canvas.drawrect (90, 90, 150, 150, paint); Canvas.drawrect (90, 170, 150, 200, paint); RECTF Re2=NewRECTF (90, 220, 150, 250);            Canvas.drawoval (Re2, paint); Path path2=NewPath (); Path2.moveto (90, 330); Path2.lineto (150, 330); Path2.lineto (120, 270);            Path2.close ();            Canvas.drawpath (path2, paint); Path Path3=NewPath (); Path3.moveto (90, 410); Path3.lineto (150, 410); Path3.lineto (135, 350); Path3.lineto (105, 350);            Path3.close ();                                    Canvas.drawpath (path3, paint); ////////////////////////////////////////////////////third column                        /** lineargradient shader = new LinearGradient (0, 0, EndX, EndY, new * Int[]{startcolor, Midlec             Olor, Endcolor},new float[]{0, 0.5f, * 1.0f}, Tilemode.mirror);              * Parameter one is the initial point coordinate x position of the gradient, parameter two is the y-axis position, parameter three and four resolution corresponds to the gradient end point * where parameter new Int[]{startcolor, Midlecolor,endcolor} is a collection of colors participating in the gradient effect, * Where parameter new float[]{0, 0.5f, 1.0f} is the relative position of the gradient in which each color is defined, this parameter can be null if NULL indicates that all colors are evenly distributed in order*/Shader Mshader=NewLinearGradient (0, 0, 100, 100,            New int[] {color.red, Color.green, Color.Blue, color.yellow},NULL, Shader.TileMode.REPEAT); //three modes of Shader.tilemode//REPEAT: Repeating in gradient direction//CLAMP: If you draw outside a predefined range, repeat the color of the border//MIRROR: Same as repeat, but this is repeated symmetricallyPaint.setshader (mshader);//use the color defined in shader to speakCanvas.drawcircle (200, 40, 30, paint); Canvas.drawrect (170, 90, 230, 150, paint); Canvas.drawrect (170, 170, 230, 200, paint); RECTF Re3=NewRECTF (170, 220, 230, 250);            Canvas.drawoval (Re3, paint); Path Path4=NewPath (); Path4.moveto (170, 330); Path4.lineto (230, 330); Path4.lineto (200, 270);            Path4.close ();            Canvas.drawpath (Path4, paint); Path Path5=NewPath (); Path5.moveto (170, 410); Path5.lineto (230, 410); Path5.lineto (215, 350); Path5.lineto (185, 350);            Path5.close ();                        Canvas.drawpath (Path5, paint); //////////////////////////////////4th ColumnPaint.settextsize (24); Canvas.drawtext ("Round", 240, 50, paint); Canvas.drawtext ("Square", 240, 120, paint); Canvas.drawtext ("Rectangle", 240, 190, paint); Canvas.drawtext ("Oval", 240, 250, paint); Canvas.drawtext ("Triangle", 240, 320, paint); Canvas.drawtext ("Trapezoid", 240, 390, paint); }    }

Summary of common methods:

1, MoveTo

moveTodoes not draw and is used only to move the move brush.
Use it in the following ways.

2, LineTo

lineToUsed for line drawing.

mPath.lineTo(300, 300);canvas.drawPath(mPath, mPaint);

The default is drawn starting at coordinates (0,0).

Did we not say that we moveTo used to move the brushes?

mPath.moveTo(100, 100);mPath.lineTo(300, 300);canvas.drawPath(mPath, mPaint);

Move the Brush (100,100) at the beginning of the drawing, the effect

3, Quadto

quadToUsed to draw a smooth curve, that is, a Bezier curve.

mPath.quadTo(x1, y1, x2, y2)(X1,Y1) is the control point, (X2,y2) is the end point.

In the same way, we need moveTo to help control.

mPath.moveTo(100, 500);mPath.quadTo(300, 100, 600, 500);canvas.drawPath(mPath, mPaint);

Effect

4, Cubicto

cubicToThe same is used to achieve Bezier curves.

mPath.cubicTo(x1, y1, x2, y2, x3, y3)(X1,Y1) is the control point, (X2,y2) is the control point, (X3,Y3) is the end point.

So, cubicTo quadTo What's the difference?

This is what the authorities say:

Same as Cubicto, but the coordinates is considered relative to the current point in this contour.

To be blunt, it is a control point.

Then we want to draw the same curve as the previous one, how should we write it?

mPath.moveTo(100, 500);mPath.cubicTo(100, 500, 300, 100, 600, 500);

Look at the effect:

Same!

What if we don't add moveTo it?

(0,0) is the starting point, (100,500) and (300,100) the Bezier curve is drawn for the control points:

5, ArcTo

arcToUsed to draw an arc (actually a part of a circle or ellipse).

mPath.arcTo(ovalRectF, startAngle, sweepAngle), the ovalRectF rectangle for the ellipse, the startAngle starting angle, and the sweepAngle end angle.

new RectF(10, 10, 600, 600);mPath.arcTo(mRectF, 0, 90);canvas.drawPath(mPath, mPaint);

Because of the new RectF(10, 10, 600, 600) square and intercept 0 ~ 90 度 , the resulting curve is a one-fourth-circle arc.

Effect

Introduction to the drawing class in Android path

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.