Android下通過Canvas類和Paint類畫一個表格的方法


首先介紹PaintCanvas類的用法:

     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大數字運算,那么生成表格的速度就非常的慢,

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM