天天看點

android canvas畫邊框,Android下通過Canvas類和Paint類畫一個表格的方法

首先介紹Paint和Canvas類的用法:

Paint:就是一個畫筆,使用之前首先要調整好畫筆,然後就可以在畫布上繪圖了,這樣就可以顯示在手機螢幕上。

主要方法有:setColor() 設定畫筆的顔色

setTextSize() 設定字型大小

setStyle() 設定畫筆的風格,空心還是實心

setStrokWidth() 設定空心的邊框寬度

setTextAlign() 設定文字的對齊方式

setTypeface() 設定字型,如粗細、傾斜

在設定畫筆顔色的時候,使用到了Color類,這個類定義了一些顔色常量和顔色轉換。如Color.RED、Color.GRENN等,還可以通過Color類的靜态方法rgb(int,int,int)

來定一個顔色,這三個參數的的值範圍是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大數字運算,那麼生成表格的速度就非常的慢,

而使用移位運算速度就快了好幾倍。是以在寫代碼的時候,不僅僅是代碼寫出來了達到這個效果就可以了,還要考慮效率問題,讓我們寫的代碼在執行的時候,用的時間最短。