最近在研究自定义控件,遇到了几何图形的绘制,这里就贴出来常见的例子:
①首先在主Activity中代码:
package com.example.mycustomwidget;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyCustomView myView = new MyCustomView(MainActivity.this);
setContentView(myView);
}
}
②MyCustomView中代码:
package com.example.mycustomwidget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class MyCustomView extends View{
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public MyCustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//设置画布的颜色
canvas.drawColor(Color.BLUE);
//-----------------------------①画一个空心正方形--------------------------------
//创建一个画笔
Paint mPaint = new Paint();
//设置画笔为空心
mPaint.setStyle(Paint.Style.STROKE);
//设置画颜色
mPaint.setColor(Color.RED);
//设置画笔宽度
mPaint.setStrokeWidth(10);
/**
* 创建一个矩形
* 第一个参数:矩形左顶点的X轴坐标
* 第二个参数:矩形左顶点的Y轴坐标
* 第三个参数:矩形右下角顶点的X轴坐标
* 第四个参数:矩形右下角顶点的Y轴坐标
*/
Rect mRect = new Rect(10, 10, 50, 50);
//开始画矩形了
canvas.drawRect(mRect, mPaint);
//-----------------------------②画一个实心正方形--------------------------------
//设置画笔为实心
mPaint.setStyle(Paint.Style.FILL);
//重新设置矩形的大小
mRect.set(60, 60, 100, 100);
canvas.drawRect(mRect, mPaint);
//-----------------------------③画一个空心长方形--------------------------------
//设置画笔的宽度
mPaint.setStrokeWidth(2);
mPaint.setStyle(Paint.Style.STROKE);
mRect.set(70, 110, 140, 140);
canvas.drawRect(mRect, mPaint);
//-----------------------------④画一个空心圆--------------------------------
//消除锯齿
mPaint.setAntiAlias(true);
/**
* 画一个空心圆
* 第一个参数:圆心的X轴坐标
* 第二个参数:圆心的Y轴坐标
* 第三个参数:圆的半径
* 第四个参数:画笔
*/
canvas.drawCircle(180, 180, 20, mPaint);
//-----------------------------⑤画一个实心圆--------------------------------
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(230, 220, 20, mPaint);
//-----------------------------⑥画一段实心扇形--------------------------------
//先画一个矩形
RectF oval = new RectF(240, 240, 300, 300);
/**
* 画一段实心扇形
* 第一个参数:一个矩形,用户定义扇形的大小
* 第二个参数:画扇形的起始角度
* 第三个参数:要画的角度
* 第四个参数是否包括圆形(true为包括,一般用于画扇形;false为不包括,一般用于画弧形)
* 第四个参数:画笔
*/
canvas.drawArc(oval, 0, 40, true, mPaint);
//-----------------------------⑦画一段空心扇形--------------------------------
mPaint.setStyle(Paint.Style.STROKE);
oval.set(290, 290, 350, 350);
canvas.drawArc(oval, 0, 60, true,mPaint);
//-----------------------------⑧画一段弧--------------------------------
oval.set(360, 360, 400, 400);
canvas.drawArc(oval, 0, 70, false, mPaint);
//-----------------------------⑨画一个内切圆--------------------------------
oval.set(360, 360, 400,400);
canvas.drawRect(oval, mPaint);
canvas.drawArc(oval, 0, 360, false, mPaint);
}
}