Android Custom View Basic Functions You Need to Know
First, draw to Canvas requires a Paint. What are the commonly used functions for brushes? Since there is no debugging environment, the functions are basically written in dictation. Please comment and ask if there is any error, Crab!
Paint p = new Paint(); //Set the color of the brush(("#2EA4F2")); //Set the style of the brush: Fill all FILL and draw only the outline STROKE(); //Set the width of the brush(8); //Set whether to anti-aliasing(true);
//Set the text size(30); //Measure the length of the string("Hello World");
When we have a brush, we can draw basic graphics.
Wire:
//Draw a line from 0,0 to 100,100(0,0,100,100,p); triangle&Polygons It's forPathClass implemented。PathThe class provides the function of point drawing lines,See examples [java] view plain copy existCODEView code fragments derived from my code fragments (0,0);//The starting point of a given path(10,10);//Draw a path to 10, 10(5,3);//Continue to draw a path from 10, 10 to 5, 3;//To form a closed space with the drawn lines(path,p);
rectangle:
//Draw a rectangle, the coordinates of the upper left corner are 0,0, the coordinates of the lower right corner are 100, 50(0,0,100,50,p);
Rounded rectangle:
//A rectangleRectF rectF = new RectF(0,0,100,50); //Draw a rounded rectangle with a size rectF, 20, and 20 tables represent the radius of the left round corner and the radius of the right round corner.(RectF,20,20,p);
Circular
//Draw a circle with a center of 50 and 50 with a radius of 100(50,50,100,p);
arcNote that the second parameter here is calculated from the direction of three o'clock to 0°, so if you want to draw from the middle direction of 12 o'clock, then it is 270°. The fourth parameter is to decide whether to pass through the center of the circle (change this parameter yourself and you will know the difference).
//Draw an arc, the rectangle where the arc is located is rectF, start from 270°, draw 90° without passing through the center of the circle(rectF,270,90,false,p);
The above are basically the most basic functions used in custom view, welcome to add them.
Thank you for reading, I hope it can help you. Thank you for your support for this site!