一、绘制内容表述
绘画的实现中,实际的过程是:用一支笔在指定的画布上绘制目标对象。
在整个过程实现中,使用三个对象:笔,画布,目标对象。
Android中绘制实现,也是使用笔在画布上绘制对象。
笔的属性包含:颜色;对象区域属性:填充,轮廓,填充与轮廓;轮廓间隙大小;字体大小;
画布对象可直接创建出来,也可以在View对象的onDraw()方法中获取,可以直接指定绘制对象:
二、绘制的具体实现
绘制类实现框架:
/**
* 功能:把绘制出来的View展示到Activity中
* 作者:vision
* 时间:2016/11/28
*/
public class DrawViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawView(this));
}
class DrawView extends View {
/**
* 上下文对象
*/
private Context context;
private Paint paint;
public DrawView(Context context) {
super(context);
this.context = context;
paint = new Paint();
paint.setColor(getResources().getColor(R.color.darkcyan));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.GRAY);
paint.setColor(getResources().getColor(R.color.red));
paint.setStyle(Paint.Style.FILL_AND_STROKE);
//绘制圆角矩形
rect = new RectF(50, 1100, 800, 1600);
canvas.drawRoundRect(rect,
30, //x轴的半径
60, //y轴的半径
paint);
}
}
}
点:
//1:画一个点
paint.setColor(getResources().getColor(R.color.red));
paint.setStrokeWidth(20);
canvas.drawPoint(10, 20, paint);
线:
//2,画一条线
paint.setColor(getResources().getColor(R.color.green));
paint.setStrokeWidth(10);
canvas.drawLine(30, 30, 40, 40, paint);
矩形:
//3,画一个矩形
paint.setColor(getResources().getColor(R.color.yellow));
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(30);
canvas.drawRect(40, 40, 80, 80, paint);//另一种方法是:传递一个RectF对象
椭圆:
//4,画一个椭圆
paint.setColor(getResources().getColor(R.color.gold));
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(10);
RectF oval = new RectF(200, 200, 400, 500);//即矩形的内接圆
canvas.drawOval(oval, paint);
圆:
//5,画一个圆
paint.setColor(getResources().getColor(R.color.paleturquoise));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
canvas.drawCircle(600, 400, 50, paint);
路径:
//6,路径:通过路径Path对象
paint.setColor(getResources().getColor(R.color.palevioletred));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
Path path = new Path();
path.moveTo(700, 20);//起始点
path.lineTo(730, 30);
path.lineTo(740, 60);
path.lineTo(670, 100);
canvas.drawPath(path, paint);
文字:
//7,绘制文字
paint.setColor(getResources().getColor(R.color.purple));
paint.setTextSize(64);
paint.setStrokeWidth(2);
canvas.drawText("从你的全世界路过~~", 500, 800, paint);
paint.setColor(getResources().getColor(R.color.mediumpurple));
paint.setTextSize(64);
paint.setStrokeWidth(10);
canvas.drawText("祝愿世界和平", 500, 1000, paint);
图片:
//8,绘制图片
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pyy);
canvas.drawBitmap(bitmap, 650, 660, paint);
弧线区域:
//绘制弧线区域
paint.setColor(getResources().getColor(R.color.blue));
RectF rect = new RectF(0, 0, 100, 100);
canvas.drawArc(rect, //弧线所使用的矩形区域大小
0, //开始角度
90, //扫过的角度
false, //是否使用中心
paint);
paint.setColor(getResources().getColor(R.color.cadetblue));
paint.setStyle(Paint.Style.STROKE);
rect = new RectF(100, 0, 300, 100);
canvas.drawArc(rect, //弧线所使用的矩形区域大小
0, //开始角度
90, //扫过的角度
true, //是否使用中心
paint);
按照指定位置绘制文字:
//按照既定点 绘制文本内容
canvas.drawPosText("天下武功无奇不破,唯快不破", new float[]{
10, 110, //第一个字母在坐标10,110
20, 120, //第二个字母在坐标20,120
30, 130, //....
40, 140,
50, 150,
60, 160,
70, 170,
80, 180,
90, 190,
100, 100,
110, 110,
120, 90,
130, 160
}, paint);
实现实例效果:
三、综合实现
依据以上的绘制基础,实现绘制钟表刻度盘:
paint.setColor(getResources().getColor(R.color.red));
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
canvas.translate(canvas.getWidth() / 2, 200); //将位置移动画纸的坐标点:150,150
canvas.drawCircle(0, 0, 100, paint); //画圆圈
//使用path绘制路径文字
canvas.save();
canvas.translate(-75, -75);
path = new Path();
path.addArc(new RectF(0, 0, 150, 150), -180, 180);
Paint citePaint = new Paint(paint);
citePaint.setTextSize(14);
citePaint.setStrokeWidth(1);
canvas.drawTextOnPath("http://www.666Android.com", path, 28, 0, citePaint);
canvas.restore();
Paint tmpPaint = new Paint(paint); //小刻度画笔对象
tmpPaint.setStrokeWidth(1);
float y = 100;
int count = 60; //总刻度数
for (int i = 0; i < count; i++) {
if (i % 5 == 0) {
canvas.drawLine(0f, y, 0, y + 12f, paint);
canvas.drawText(String.valueOf(i / 5 + 1), -4f, y + 25f, tmpPaint);
} else {
canvas.drawLine(0f, y, 0f, y + 5f, tmpPaint);
}
canvas.rotate(360 / count, 0f, 0f); //旋转画纸
}
//绘制指针
tmpPaint.setColor(Color.GRAY);
tmpPaint.setStrokeWidth(4);
canvas.drawCircle(0, 0, 7, tmpPaint);
tmpPaint.setStyle(Paint.Style.FILL);
tmpPaint.setColor(Color.YELLOW);
canvas.drawCircle(0, 0, 5, tmpPaint);
canvas.drawLine(0, 10, 0, -65, paint);
在控制定位中,位置计算比较繁杂,示例实现效果要想更好,需要在调整下。
我们限制了自己【如果现在是特种兵训练,你还是得熬;现在不是,就放松了】
"骑马,牵牛,赶猪,打狗"理论:人品很好,能力又很强的,是千里马,我们要骑着他;人品很好但能力普通的,是老黄牛,我们要牵着他;人品、能力皆普通的,就是"猪",我们要赶走他;人品很差能力很强的,那是"狗",我们要打击他。