Android Paint paint and canvas

Source: Internet
Author: User
Tags drawtext

In Android, you need to display 2D graphics through the graphics class.

The graphics include commonly used classes such as canvas, paint (brush), color (colors), Bitmap (image), and so on. Graphics have features such as drawing points, lines, colors, 2D geometries, and image processing.

Canvas:

void DrawRect (RECTF rect, paint paint)//plot area, parameter one for RECTF one area

void DrawPath (path path, paint paint)//Draw a path, parameter

void Drawbitmap (Bitmap Bitmap, rect src, rect dst, paint paint)//map, parameter one is our regular Bitmap object, parameter two is the source area (here is Bitmap), the parameter three is the target area (should be in The position and size of the canvas), parameter four is the paint brush object, because it is possible to scale and stretch, and when the original rect is not equal to the target rect, there is a significant loss of performance.

void DrawLine (float StartX, float starty, float stopx, float stopy, paint paint)//Draw line, the x-axis position of a starting point of the parameter, the y-axis position of the two starting point of the parameter, the x-axis level of the three end of the parameter Position, parameter four the vertical position of the y-axis, and the last parameter is the paint brush object.

void Drawpoint (float x, float y, paint paint)//Draw point, parameter one horizontal x axis, parameter two vertical y axis, third parameter is paint object.

void DrawText (string text, float x, float y, paint paint)//Render text, canvas class In addition to the above can also depict text, parameter one is a String type of text, parameter two x axis, parameter three Y axis, Parameter four is the Paint object.

void Drawtextonpath (String text, path path, float hoffset, float vOffset, paint paint)//Draw text on path, relative to the second parameter above is the path path object

Paint:

void Setargb (int a, int r, int g, int b) Sets the Paint object color, parameter one is Alpha transparent channel

void Setalpha (int a) sets Alpha opacity with a range of 0~255

void Setantialias (Boolean AA)//whether anti-aliasing

void setcolor (int color)//Set color, here the Android internal definition has a color class containing some common color definitions

void Setfakeboldtext (Boolean fakeboldtext)//Set pseudo-bold text

void Setlineartext (Boolean lineartext)//Set Linear text

Patheffect Setpatheffect (Patheffect effect)//Set path effect

Rasterizer Setrasterizer (Rasterizer Rasterizer)//Set Rasterization

Shader Setshader (Shader Shader)//Set Shadow

void SetTextAlign (Paint.align Align)//Set text alignment

void Settextscalex (float ScaleX)//Set text magnification, 1.0f to original

void Settextsize (float textSize)//Set Font size

Typeface settypeface (Typeface Typeface)//Set the font, Typeface contains the type of font, weight, and tilt, color, and so on.

void Setunderlinetext (Boolean underlinetext)//Set Underline

The basic implementation method of Custom view

First, we need to customize a class, such as MyView, to inherit from the view class. Then, the OnDraw () function of the replication view class. Finally, use the paint and canvas objects in the OnDraw () function to draw the graphics we need.

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); }    }

Android Paint paint and canvas

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.