Android下通过Canvas类和Paint类画一个表格的方法

来源:互联网 发布:js date unix时间戳 编辑:程序博客网 时间:2024/04/30 04:38

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

而使用移位运算速度就快了好几倍。所以在写代码的时候,不仅仅是代码写出来了达到这个效果就可以了,还要考虑效率问题,让我们写的代码在执行的时候,用的时间最短。

0 0