1.Canvas相当于画布,所有的图形都在其上面绘制并显示出来。Paint相当于画笔,可以设置不同颜色等,画出不同图形。
以下均在onDraw(Canvas canvas)执行,定义了Paint paint = new Paint();
2.背景设置颜色 canvas.drawColor(),例如:canvas.drawColor(Color.WHITE);
3.去锯齿paint.setAntiAlias(true);
4.设置paint的颜色paint.setColor(Color.RED);
5.设置paint的style 空心:paint.setStyle(Paint.Style.STROKE);
实心:paint.setStyle(Paint.Style.FILL);
6.设置paint的外框宽度 paint.setStrokeWidth(3);
7.画圆:canvas.drawCircle(cx, cy, radius, paint);
8.画正方形:canvas.drawRect(left, top, right, bottom, paint);
9.画长方形:canvas.drawRect(left, top, right, bottom, paint);
10.画椭圆:RectF re = new RectF(left, top, right, bottom);
11.画三角形:Path path = new Path();
path.moveTo(10, 330);//第一个点
path.lineTo(70, 330);//第二个点
path.lineTo(40, 270);//第三个点
path.close();
canvas.drawPath(path, paint);
12.画梯形:Path path1 = new Path();
path1.moveTo(10, 410);
path1.lineTo(70, 410);
path1.lineTo(55, 350);
path1.lineTo(25, 350);
path1.close();
canvas.drawPath(path1, paint);
13.画扇形:canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint)
例如:canvas.drawArc(new RectF(330, 0, 480, 150), 0, 270, true, paint);
14.设置渐变色:Shader mShader = new LinearGradient(0, 0, 100, 100, new int[] {
Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW }, null,
Shader.TileMode.REPEAT);
paint.setShader(mShader);
15.写字:paint.setTextSize(24);
canvas.drawText(text, x, y, paint);
16.这样生成一个View后,要使得主Activity调用这个View,而不是调用Layout里面的布局。假设我们的View名是MyView,则应setContentView(new MyView(this)。