In order to make the code in the article look concise, easy to understand a little, I do not put the paint of the instantiation code, and prompted: This article all variables are I set dead, you can according to the needs to set the Getset method.
Parameters:
Private Paint mpaint;//A brush that draws text and an XY axis
Private Paint mpaintcolumn;//draw a cylindrical brush
Private final Float start_x = 200;//y Axis upper left corner X coordinate
Private final Float start_y = 200;//y Axis Y-coordinate in upper left corner
Private Boolean canload = true;//whether to start drawing
private float Xspace = 150;//y axis per digit spacing
Private float xwidth = total length of 900;//x axis
Private string[] Xnames = {"Test 1", "Test 2", "Test 3", "Test 4", "Test 5"};
private int ycount = number of 10;//y axis values (including 0)
private float Yspace = 100;//y axis per digit spacing
Private float yheight = total length of 1000;//y axis
private float Columswidth = 50;//rectangular width (cylindrical)
Yheight = (float) (Yheight + gettxtheight (mpaint));//This code means: Add the value of the y-axis and the height of the text, because the text has a height, if
Not counting the height of the text, the last y-axis will be a bit higher than the plan.
Get the height of the text
Public double gettxtheight (Paint mpaint) {
Paint.fontmetrics fm = Mpaint.getfontmetrics ();
Return Math.ceil (fm.descent-fm.ascent);
}
Key code Draw:
@Override
public void Draw (Canvas Canvas) {
Super.draw (canvas);
if (canload) {
Canvas.drawline (start_x, start_y, start_x, Yheight + start_y, mpaint);//y axis
Canvas.drawline (start_x, Yheight + start_y, start_x + xwidth, Yheight + start_y, mpaint);//x axis
float maxtextwidth = Mpaint.measuretext ("1000");//Here I know the longest word is 1000, so write, change according to different circumstances
float txtheight = (float) gettxtheight (mpaint);//mpaint the height of the text being painted
The values on the Y axis
for (int i = 0; I <= ycount; i++) {
String count = + * i + "";
Canvas.drawtext (count, Start_x-maxtextwidth, Yheight + start_y-yspace * I, mpaint);
}
Values on the X axis
for (int i = 0; i < xnames.length; i++) {
String name = Xnames[i];
Canvas.drawtext (name, start_x + xspace * (i + 1), Yheight + start_y + txtheight, mpaint);
}
The concrete values of the cylinders and cylinders
for (int i = 0; i < xnames.length; i++) {
String count = (i + 1) * 100 + "";
Canvas.drawrect (start_x + xspace * (i + 1)-COLUMSWIDTH/2, start_y + yheight-(i + 1) * Yspace, start_x + xspace * (i + 1) + COLUMSWIDTH/2, Yheight + start_y, mpaintcolumn);
Canvas.drawtext (count, columswidth/2+start_x + xspace * (i + 1)-COLUMSWIDTH/2, start_y + yheight-(i + 1) * YSpace-10 , Mpaint);
}
}
}