1.线
线
代码语言:javascript复制 Paint mPaint = new Paint();
/** 起始X 起始Y 结束X 结束Y **/
float[] pts={20,20,200,20, //第一根线
200,20,200,200, //第二根线
200,200,20,200, //第三根线
20,200,20,20}; //第四根线
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.BLACK);//颜色
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setAntiAlias(true);//抗锯齿
mPaint.setStrokeWidth(5);
canvas.drawLines(pts,mPaint);
}
2.点
点
代码语言:javascript复制 Paint mPaint = new Paint();
float[] pst = {20,20,40,40,60,60};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStrokeWidth((float)10);
mPaint.setColor(Color.BLACK);
/**
* BUTT, 平头
* ROUND, 圆头
* SQUARE 方头
*/
mPaint.setStrokeCap(Paint.Cap.ROUND);
canvas.drawPoints(pst,mPaint);
}
3.圆
圆
代码语言:javascript复制 Paint mPaint = new Paint();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.GREEN);//颜色
mPaint.setStyle(Paint.Style.STROKE);//类型
mPaint.setStrokeWidth(15);//线条宽度
mPaint.setAntiAlias(true);//抗锯齿
canvas.drawCircle(200,200,100,mPaint);
}
4.矩形
矩形
代码语言:javascript复制 Paint mPaint = new Paint();
Rect mRect = new Rect();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.GRAY);//颜色
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setStrokeWidth(15);//线条宽度
/**上下左右的坐标*/
mRect.top = 50;
mRect.bottom = 300;
mRect.left = 50;
mRect.right = 300;
canvas.drawRect(mRect,mPaint);
}
5.圆角矩形
圆角矩形
代码语言:javascript复制 Paint mPaint = new Paint();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.BLACK);//颜色
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setAntiAlias(true);//抗锯齿
mPaint.setStrokeWidth(5);
canvas.drawRoundRect(100,100,400,300,55,55,mPaint);
}
6.椭圆
椭圆
代码语言:javascript复制 Paint mPaint = new Paint();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.BLACK);//颜色
mPaint.setStyle(Paint.Style.STROKE);//类型
mPaint.setAntiAlias(true);//抗锯齿
mPaint.setStrokeWidth(20);
/** 左 上 右 下 **/
canvas.drawOval((float) 10,(float)50,(float)400,(float)200,mPaint);
}
7.弧形
弧形
代码语言:javascript复制 Paint mPaint = new Paint();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.GREEN);//颜色
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setAntiAlias(true);//抗锯齿
/**左 上 右 下 起始角度 弧形角度**/
canvas.drawArc(15,15,215,210,45,45,true,mPaint);
}
8.弧形组合
弧形组合
代码语言:javascript复制 Paint mPaint = new Paint();
public mAres(Context context, int i) {
super(context);
switch (i){
case 1: {
mPaint.setColor(Color.GREEN);//颜色
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setAntiAlias(true);//抗锯齿
startAngle = 45;
sweepAngle = 45;
left = 15;
top = 10;
break;
}
case 2: {
mPaint.setColor(Color.RED);//颜色
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setAntiAlias(true);//抗锯齿
startAngle = 90;
sweepAngle = 45;
left = 10;
top = 10;
break;
}
case 3: {
mPaint.setColor(Color.BLACK);//颜色
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setAntiAlias(true);//抗锯齿
startAngle = 135;
sweepAngle = 90;
left = 8;
top = 5;
break;
}
case 4: {
mPaint.setColor(Color.BLUE);//颜色
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setAntiAlias(true);//抗锯齿
startAngle = 225;
sweepAngle = 90;
left = 13;
top = 2;
break;
}
case 5: {
mPaint.setColor(Color.YELLOW);//颜色
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setAntiAlias(true);//抗锯齿
startAngle = 315;
sweepAngle = 90;
left = 18;
top = 5;
break;
}
}
}
int left = 0;
int top = 0;
int startAngle = 0;
int sweepAngle = 0;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**左 上 右 下 起始角度 弧形角度**/
canvas.drawArc(left,top,200 left,200 top,startAngle,sweepAngle,true,mPaint);
}
9.位图
位图
代码语言:javascript复制 Paint mPaint = new Paint();
Bitmap bitmap;
Rect src;
RectF dst;
int ia = 0;
public mBitMap(Context context,int i) {
super(context);
ia = i;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (ia==1){
bitmap = BitmapFactory.decodeResource(this.getResources(),R.mipmap.myview0);
/** 左偏移 上偏移*/
canvas.drawBitmap(bitmap,10,10,mPaint);
}else {
bitmap = BitmapFactory.decodeResource(this.getResources(),R.mipmap.myview);
int mBitMapWidth = bitmap.getWidth();
int mBitMapHeight = bitmap.getHeight();
src = new Rect(0,0,120,150); //是对图片进行裁截,若是空null则显示整个图片
dst = new RectF(0,0,mBitMapWidth,mBitMapHeight); //是图片在Canvas画布中显示的区域,大于src则把src的裁截区放大, 小于src则把src的裁截区缩小
canvas.drawBitmap(bitmap,src,dst,mPaint); }
}
10.文字
文字
代码语言:javascript复制 Paint mPaint = new Paint();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.RED);//颜色
mPaint.setTextSize(30);
mPaint.setStyle(Paint.Style.FILL);//类型
mPaint.setAntiAlias(true);//抗锯齿
mPaint.setStrokeWidth(5);
/** x y轴的偏移量 **/
canvas.drawText("老子问有没有瓶子,都他妈聋了?",100,100,mPaint);
}
11.自定义图形-心形
自定义图形
代码语言:javascript复制 Paint mPaint = new Paint();
Path mPath = new Path();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.RED);//颜色
mPath.addArc(200,200,400,400,-225,255);
mPath.arcTo(400,200,600,400,-180,225,false);
mPath.lineTo(400,542);
canvas.drawPath(mPath,mPaint);
}
12.自定义图像2-双圆叠加镂空
自定义图像2
代码语言:javascript复制 Paint mPaint = new Paint();
Path mPath = new Path();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.RED);//颜色
mPath.setFillType(Path.FillType.EVEN_ODD);
mPaint.setAntiAlias(true);//抗锯齿
mPath.addCircle(150,200,100, Path.Direction.CW);
mPath.addCircle(200,200,100, Path.Direction.CW);
canvas.drawPath(mPath,mPaint);
}