首先介紹Paint和Canvas類的用法: canvas
Paint:就是一個畫筆,使用以前首先要調整好畫筆,而後就能夠在畫布上繪圖了,這樣就能夠顯示在手機屏幕上。 ide
主要方法有:setColor() 設置畫筆的顏色 字體
setTextSize() 設置字體大小 this
setStyle() 設置畫筆的風格,空心仍是實心 對象
setStrokWidth() 設置空心的邊框寬度 ci
setTextAlign() 設置文字的對齊方式 get
setTypeface() 設置字體,如粗細、傾斜 it
在設置畫筆顏色的時候,使用到了Color類,這個類定義了一些顏色常量和顏色轉換。如Color.RED、Color.GRENN等,還能夠經過Color類的靜態方法rgb(int,int,int) class
來定一個顏色,這三個參數的的值範圍是0~255。 效率
Canvas:是一個畫布,能夠在上面畫咱們想要的任何東西,咱們也能夠設置畫布的一些的屬性,好比背景顏色,尺寸等。Canvas提供了一下一些方法:
方法:Canvas(),建立一個空的畫布,能夠使用setBitmap()方法來設置繪製的具體畫布;
Canvas(Bitmap bitmap),以bitmap對象建立一個畫布,此時則將內容繪製在bitmap上,bitmap不得爲null.
drawColor(),設置畫布的背景顏色。
drawRect(left,top,right,bottom,paint);畫矩形,前四個是float,後一個是Paint類型。
drawLine(startX,startY,stopX,stopY,paint),畫線,前四個參數是float,後一個是Paint類型。
drawText(text,x,y,paint);在畫布上畫指定的文本,x,y兩個參數是float,後一個是Paint類型。
*********************************看下面的代碼****************************************
/*空矩形*/
Paint paintRect = new Paint();
paintRect.setColor(Color.rgb(79, 129, 189));
paintRect.setStrokeWidth(2);
paintRect.setStyle(Style.STROKE);
canvas.drawRect(20.0,20.0,100.0,100.0, paintRect);
/*實矩形*/
Paint paintRect = new Paint();
paintRect.setColor(Color.rgb(79, 129, 189));
paintRect.setStrokeWidth(2);
paintRect.setStyle(Style.STROKE);
canvas.drawRect(20.0,20.0,100.0,100.0, paintRect);
/*畫直線*/
Paint paintRect = new Paint();
paintRect.setColor(Color.rgb(79, 129, 189));
paintRect.setStrokeWidth(1);
paintRect.setStyle(Style.STROKE);
canvas.drawLine(10.0,80.0,100.0,80.0, paintRect);
/*畫字*/
Paint paint = new Paint();
paint.setColor(Color.rgb(79, 129, 189));
paint.setStyle(Style.STROKE);
paint.setTextAlign(Align.CENTER);//字水平居中
// FontMetrics fontMetrics = paint.getFontMetrics();計算字的高度
canvas.drawText(text ,20.0,40.0, paint);
***********畫表格******************
public class TableView extends View {
// 表格的行數和列數
private int row, col;
// 表格定位的左上角X和右上角Y
private final static int STARTX = 25;
private final static int STARTY = 25;
// 表格的寬度
private static float gridWidth;
private static float gridHeight;
public TableView(Context context, int row, int col) {
super(context);
this.row = row;
this.col = col;
if (this.row == 0 || this.col == 0) {
assert false : "行數和列數爲0,不符合";
}
this.setFocusable(true);
this.setFocusableInTouchMode(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
gridWidth = (w - 50) / (this.col * 1.0f);
if (this.row > this.col) {
gridHeight = (h - 100) / (this.row * 1.0f);
} else {
gridHeight = gridWidth;
}
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 填充表格顏色
Paint paintColor = new Paint();
paintColor.setStyle(Style.FILL);
paintColor.setColor(Color.rgb(235, 241, 221));
canvas.drawRect(STARTX, STARTY, STARTX + gridWidth * this.col, STARTY
+ gridHeight * this.row, paintColor);
paintColor.setColor(Color.rgb(219, 238, 243));
for (int i = 0; i < this.row; i++) {
if ((i + 1) % 2 == 1) {
canvas.drawRect(STARTX, i * gridHeight + STARTY, STARTX
+ this.col * gridWidth, STARTY + (i + 1) * gridHeight,
paintColor);
}
}
// 畫表格最外層邊框
Paint paintRect = new Paint();
paintRect.setColor(Color.rgb(79, 129, 189));
paintRect.setStrokeWidth(2);
paintRect.setStyle(Style.STROKE);
canvas.drawRect(STARTX, STARTY, STARTX + gridWidth * this.col, STARTY
+ gridHeight * this.row, paintRect);
// 畫表格的行和列,先畫行後畫列
paintRect.setStrokeWidth(1);
for (int i = 0; i < this.row - 1; i++) {
canvas.drawLine(STARTX, STARTY + (i + 1) * gridHeight, STARTX
+ this.col * gridWidth, STARTY + (i + 1) * gridHeight,
paintRect);
}
for (int j = 0; j < this.col - 1; j++) {
canvas.drawLine(STARTX + (j + 1) * gridWidth, STARTY, STARTX
+ (j + 1) * gridWidth, STARTY + this.row * gridHeight,
paintRect);
}
// 在單元格填充數字—若是行數大於60而且列數大於30,就不顯示數字;大於10,就改變字大小
if (this.row <= 50 && this.col <= 30) {
Paint paint = new Paint();
paint.setColor(Color.rgb(79, 129, 189));
paint.setStyle(Style.STROKE);
paint.setTextAlign(Align.CENTER);
if (this.row > 40 || this.col > 25) {
paint.setTextSize(7);
} else if (this.row > 30 || this.col > 20) {
paint.setTextSize(8);
} else if (this.row > 20 || this.col > 15) {
paint.setTextSize(9);
} else if (this.row > 10 || this.col > 10) {
paint.setTextSize(10);
}
FontMetrics fontMetrics = paint.getFontMetrics();
float fontHeight = fontMetrics.bottom - fontMetrics.top;
int text = 0;
for (int i = 0; i < this.row; i++) {
for (int j = 0; j < this.col; j++) {
float mLeft = j * gridWidth + STARTX;
float mTop = i * gridHeight + STARTY;
float mRight = mLeft + gridWidth;
text++;
float textBaseY = (int) (gridHeight + fontHeight) >> 1;
canvas.drawText(text + "", (int) (mLeft + mRight) >> 1,
textBaseY + mTop, paint);
}
}
}
}
}
-----------------------------------------------------------------------------------------------------------------
在寫以上代碼的時候,出現了代碼執行效率的問題。好比上面代碼中,除法運算若是不使用移位運算而使用BigDecimal大數字運算,那麼生成表格的速度就很是的慢,
而使用移位運算速度就快了好幾倍。因此在寫代碼的時候,不單單是代碼寫出來了達到這個效果就能夠了,還要考慮效率問題,讓咱們寫的代碼在執行的時候,用的時間最短。