PaintCanvas.2
Posted gali
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PaintCanvas.2相关的知识,希望对你有一定的参考价值。
1:使用Cavans画个简单图形
2:过程
2.1:绘制最外部的圆
/*** 初始化 paint */
Paint paint;
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(3);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
/*** 画圆 圆心,200,200,半径为100 */
canvas.drawCircle(startX,startY,100,paint);
canvas.save();
2.2:绘制文字made in china
/**** drawtextOnPath 绘制文字 */
paint.setTextSize(16);
Path path=new Path();
path.addArc(130,130,280,280,-130,130);
paint.setTextSize(14);
paint.setStrokeWidth(1);
paint.setColor(Color.GRAY);
canvas.drawTextOnPath("made in china",path ,10,10,paint);
canvas.save();
2.3:绘制表盘指针
/*** 绘制中心点圆心*/
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(200,200,10,paint);
canvas.save();
/*** 绘制红色线段,充当 秒针 */
paint.setColor(Color.RED);
canvas.drawLine(200,200,200,130,paint);
canvas.save();
/*** 绘制黑色线段,充当分针 */
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawLine(200,200,160,240,paint);
canvas.save();
/*** 绘制黑色线段,充当时针 */
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5);
canvas.drawLine(200,200,240,200,paint);
canvas.save();
2.4:绘制表盘刻度
/*** 画表盘刻度 ****/
Paint timePaint = new Paint(paint);
timePaint.setStrokeWidth(1);
timePaint.setColor(Color.BLACK);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
int count=60;
for(int i=0;i<=count;i++){
if(i % 5==0){
/*** 整点刻度 */
canvas.drawLine(200, 100, 200, 108, paint);
if(i!=0){
canvas.drawText(String.valueOf(i / 5), 195, 124, timePaint);
}
}else{
/*** 小刻度 */
canvas.drawLine(200, 100, 200, 106, timePaint);
}
/*** 旋转坐标系 */
canvas.rotate(360 / count, 200, 200);
}
3:完成绘制。
/***示例中是传参是200,200,表示的表盘的中心点*/ private void drawDialPlate(Canvas canvas, int startX, int startY){ canvas.save(); /*** 初始化 paint */ Paint paint; paint = new Paint(); paint.setColor(Color.BLACK); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth(3); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); /*** 画圆 圆心,200,200,半径为100 */ canvas.drawCircle(startX,startY,100,paint); canvas.save(); /**** drawtextOnPath 绘制文字 */ paint.setTextSize(16); Path path=new Path(); path.addArc(130,130,280,280,-130,130); paint.setTextSize(14); paint.setStrokeWidth(1); paint.setColor(Color.GRAY); canvas.drawTextOnPath("made in china",path ,10,10,paint); canvas.save(); /*** 绘制中心点圆心*/ paint.setStyle(Paint.Style.FILL); canvas.drawCircle(200,200,10,paint); canvas.save(); /*** 绘制红色线段,充当 秒针 */ paint.setColor(Color.RED); canvas.drawLine(200,200,200,130,paint); canvas.save(); /*** 绘制黑色线段,充当分针 */ paint.setColor(Color.BLACK); paint.setStrokeWidth(3); canvas.drawLine(200,200,160,240,paint); canvas.save(); /*** 绘制黑色线段,充当时针 */ paint.setColor(Color.BLACK); paint.setStrokeWidth(5); canvas.drawLine(200,200,240,200,paint); canvas.save(); /*** 画表盘刻度 ****/ Paint timePaint = new Paint(paint); timePaint.setStrokeWidth(1); timePaint.setColor(Color.BLACK); paint.setColor(Color.BLACK); paint.setStrokeWidth(3); int count=60; for(int i=0;i<=count;i++){ if(i % 5==0){ /*** 整点刻度 */ canvas.drawLine(200, 100, 200, 108, paint); if(i!=0){ canvas.drawText(String.valueOf(i / 5), 195, 124, timePaint); } }else{ /*** 小刻度 */ canvas.drawLine(200, 100, 200, 106, timePaint); } /*** 旋转坐标系 */ canvas.rotate(360 / count, 200, 200); } }
以上是关于PaintCanvas.2的主要内容,如果未能解决你的问题,请参考以下文章