/** 构造函数,主要对一些对象初始化 */ public MySurfaceView(Context context) { super(context); mHolder = this.getHolder(); // 获得SurfaceHolder对象 mHolder.addCallback(this); // 添加状态监听 mPaint = new Paint(); // 创建一个画笔对象 mPaint.setColor(Color.WHITE); // 设置画笔的颜色为白色 qPaint = new Paint(); // 创建一个画笔对象 qPaint.setAntiAlias(true); // 消除锯齿 qPaint.setStyle(Paint.Style.STROKE); // 设置画笔风格为描边 qPaint.setStrokeWidth(3); // 设置描边的宽度为3 qPaint.setColor(Color.GREEN); // 设置画笔的颜色为绿色 // 创建路径对象 mPath = new Path(); qPath = new Path(); tPath = new Path(); // 设置坐标为50,100 mX = 50; mY = 100; // 设置贝塞尔曲线的开始坐标为(10,200) qStartX = 10; qStartY = 200; setFocusable(true); // 设置焦点 }
public void draw() { Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); RectF rect = new RectF(8, 8, 192, 192); Paint p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.STROKE); p.setColor(ColS); p.setStrokeWidth(16); int S = 162; int H = 300; // S canvas.drawArc(rect, -90, S, false, p); p.setStyle(Paint.Style.FILL); p.setColor(ColM); H -= 90; canvas.drawCircle(100, 100, 72, p); p.setColor(ColH); p.setStrokeWidth(2); float sx, sy; sx = (float) (72 * Math.cos(Math.PI * H / 180)); sy = (float) (72 * Math.sin(Math.PI * H / 180)); canvas.drawLine(100, 100, sx + 100, sy + 100, p); ImageView clokImg; clokImg = (ImageView) findViewById(R.id.clokImage); clokImg.setImageBitmap(bitmap); // Hour picker Bitmap ColPickBmp = Bitmap.createBitmap(80, 50, Bitmap.Config.ARGB_8888); Canvas ColPickCanvas = new Canvas(ColPickBmp); ImageView colPickImg = (ImageView) findViewById(R.id.colorHou); rect = new RectF(0, 0, 80, 50); p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.FILL); p.setColor(ColH); ColPickCanvas.drawRect(rect, p); colPickImg.setImageBitmap(ColPickBmp); // Minute picker ColPickBmp = Bitmap.createBitmap(80, 50, Bitmap.Config.ARGB_8888); ColPickCanvas = new Canvas(ColPickBmp); colPickImg = (ImageView) findViewById(R.id.colorMin); rect = new RectF(0, 0, 80, 50); p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.FILL); p.setColor(ColM); ColPickCanvas.drawRect(rect, p); colPickImg.setImageBitmap(ColPickBmp); // Second picker ColPickBmp = Bitmap.createBitmap(80, 50, Bitmap.Config.ARGB_8888); ColPickCanvas = new Canvas(ColPickBmp); colPickImg = (ImageView) findViewById(R.id.colorSec); rect = new RectF(0, 0, 80, 50); p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.FILL); p.setColor(ColS); ColPickCanvas.drawRect(rect, p); colPickImg.setImageBitmap(ColPickBmp); }