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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Android图片边框旋转,可以使用Canvas的旋转和绘制矩形的功能。以下是一个简单的代码示例: ```java // 获取图片 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); // 创建一个新的Bitmap,用于绘制旋转后的图像 Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); // 创建Canvas对象,将绘制操作绘制到rotatedBitmap上 Canvas canvas = new Canvas(rotatedBitmap); // 设置旋转角度 float degrees = 45; canvas.rotate(degrees, bitmap.getWidth() / 2, bitmap.getHeight() / 2); // 绘制原始图像 canvas.drawBitmap(bitmap, 0, 0, null); // 绘制矩形边框 Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); Rect rect = new Rect(50, 50, bitmap.getWidth() - 50, bitmap.getHeight() - 50); canvas.drawRect(rect, paint); // 将旋转后的图像显示在ImageView上 ImageView imageView = findViewById(R.id.image_view); imageView.setImageBitmap(rotatedBitmap); ``` 在上面的代码中,我们首先获取原始图片,并创建一个新的Bitmap用于绘制旋转后的图像。然后创建Canvas对象,将绘制操作绘制到rotatedBitmap上。接着设置旋转角度,并使用Canvas的drawBitmap方法绘制原始图像。最后,使用Canvas的drawRect方法绘制矩形边框,并将旋转后的图像显示在ImageView上。 注意,在设置旋转角度时,需要将旋转中心点设置为原始图像的中心点,这里我们使用了bitmap.getWidth() / 2和bitmap.getHeight() / 2来计算中心点位置。另外,在绘制矩形边框时,我们使用了Paint对象来设置笔的颜色、样式和宽度,以及Rect对象来设置矩形的位置和大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值