自定义View(2)canas绘制基本图形的示例

效果

自定义View(2)canas绘制基本图形的示例

代码:

   void drawSample(Canvas canvas) {
/*
* 方法 说明 drawRect 绘制矩形 drawCircle 绘制圆形 drawOval 绘制椭圆 drawPath 绘制路径
* drawLine 绘制直线 drawPoin 绘制点
*/
// 创建画笔
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
int txtSize = ;
paint.setTextSize(txtSize);
/*画点*/
paint.setStyle(Paint.Style.FILL);
canvas.drawText("画点:", , txtSize, paint);
paint.setColor(Color.RED);
float stroke = paint.getStrokeWidth();
paint.setStrokeWidth();
canvas.drawPoint(, , paint);// 画一个点
canvas.drawPoints(new float[] { , , , , , }, paint);// 画多个点
paint.setStrokeWidth(stroke); /*画线*/
paint.setColor(Color.BLACK);
canvas.drawText("画线:", , , paint);
canvas.drawLine(, , , , paint);// 画线
canvas.drawLine(, , , , paint);// 斜线 /*画虚线*/
canvas.drawText("画虚线:", ,, paint);
paint.setStyle(Paint.Style.STROKE);
Path path3 = new Path();
path3.moveTo(, );
path3.lineTo(,);
/*DashPathEffect第一个参数必须为偶数个整数,其中偶数位上的整数表示绘制的间隔,奇数位上的整数表示不绘制的间隔。*/
PathEffect effects = new DashPathEffect(new float[]{,,,,,},);
PathEffect oldEffect = paint.getPathEffect();
paint.setPathEffect(effects);
canvas.drawPath(path3, paint); /*画弧线*/
paint.setPathEffect(oldEffect);
canvas.drawText("画弧线:", , , paint); RectF oval1 = new RectF(, , , ); // 弧线是画在矩形中的
canvas.drawArc(oval1, , , false, paint); // 左眼弧线 ^_^ oval1.set(, , , );
canvas.drawArc(oval1, , , false, paint); // 右眼弧线 ^_^ oval1.set(, , , );
canvas.drawArc(oval1, , , false, paint); // 嘴弧线 ^_^ /*画贝塞尔曲线*/
/* Path类封装复合(多轮廓几何图形的路径
* 由直线段*、二次曲线,和三次方曲线,也可画以油画。drawPath(路径、油漆),要么已填充的或抚摸
* (基于油漆的风格),或者可以用于剪断或画画的文本在路径。
*/
canvas.drawText("画贝塞尔曲线:", , , paint);
Path path2 = new Path();
path2.moveTo(, ); // 设置Path的起点
path2.quadTo(, , , ); // 设置贝塞尔曲线的控制点坐标和终点坐标
canvas.drawPath(path2, paint); // 画出贝塞尔曲线 /*画圆*/
canvas.drawText("画圆:", , , paint); // 画文本
canvas.drawCircle(, , , paint); // 小圆
paint.setAntiAlias(true); // 设置画笔的锯齿效果。true是去除
canvas.drawCircle(, , , paint); // 大圆 /*画扇形和椭圆*/
canvas.drawText("画扇形和椭圆:", , , paint);
/*设置渐变色 这个正方形的颜色是改变的 */
Shader mShader = new LinearGradient(, , , ,
new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW,
Color.LTGRAY }, null, Shader.TileMode.REPEAT); // 一个材质,打造出一个线性梯度沿著一条线。
paint.setShader(mShader);
// p.setColor(Color.BLUE); // 画扇形,drawArc第一个参数是RectF:该类是第二个参数是角度的开始,第三个参数是多少度,第四个参数是真的时候画扇形,是假的时候画弧线
RectF oval2 = new RectF(, , , );// 设置个新的长方形,扫描测量
canvas.drawArc(oval2, , , true, paint); // 画椭圆,把oval改一下
oval2.set(, ,,);
canvas.drawOval(oval2, paint); /*画矩形*/
canvas.drawText("画矩形:", , , paint);
paint.setStyle(Paint.Style.FILL);// 设置填满
canvas.drawRect(, , , , paint);// 正方形
canvas.drawRect(, , , , paint);// 长方形 /*画圆角矩形*/
canvas.drawText("画圆角矩形:", , , paint);
paint.setStyle(Paint.Style.FILL);// 充满
paint.setColor(Color.LTGRAY);
paint.setAntiAlias(true);// 设置画笔的锯齿效果
RectF oval3 = new RectF(, , , );// 设置个新的长方形
canvas.drawRoundRect(oval3, , , paint);// 第二个参数是x半径,第三个参数是y半径 /*画三角形*/
canvas.drawText("画三角形:", , , paint);
// 绘制这个三角形,你可以绘制任意多边形
Path path = new Path();
path.moveTo(, );// 此点为多边形的起点
path.lineTo(, );
path.lineTo(, );
// path.lineTo(160, 380);
path.close(); // 使这些点构成封闭的多边形,就不用画回到起点 path.lineTo(160, 380);
canvas.drawPath(path, paint); /*画任意多边形,*/
paint.setShader(null);
canvas.drawText("画任意多边形:", , , paint);
paint.setColor(Color.LTGRAY);
paint.setStyle(Paint.Style.STROKE);// 设置空心
Path path1 = new Path();
path1.moveTo(, );
path1.lineTo(, );
path1.lineTo(, );
path1.lineTo(, );
path1.close(); // 封闭形状
canvas.drawPath(path1, paint); /*画图片,就是贴图*/
canvas.drawText("画图片:", , , paint);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
canvas.drawBitmap(bitmap, , , paint); /*画文本*/
paint.setColor(Color.BLACK);
canvas.drawText("画文本:", , , paint);
paint.setTextSize(txtSize * );
paint.setStyle(Paint.Style.FILL); FontMetrics fontMetrics = paint.getFontMetrics();
float originX = ;
float originY = ;
float topY = originY + fontMetrics.top;
float ascentY = originY + fontMetrics.ascent;
float descentY = originY + fontMetrics.descent;
float bottomY = originY + fontMetrics.bottom;
float leading = fontMetrics.leading; //文本
paint.setColor(Color.RED);
paint.setTextAlign(Align.CENTER);
canvas.drawText("text1", originX , originY, paint); //origin point
canvas.drawCircle(originX, originY, , paint);
paint.setTextAlign(Align.LEFT); //baseline
paint.setColor(Color.LTGRAY);
paint.setTextSize(txtSize);
canvas.drawText("baseline", originX - , originY, paint);
canvas.drawLine(originX, originY, originX + , originY, paint); //top line
paint.setColor(Color.GREEN);
canvas.drawText("topline", originX - , topY, paint);
canvas.drawLine(originX, topY, originX + , topY, paint); //ascent line
paint.setColor(Color.CYAN);
canvas.drawText("ascent line", originX + , ascentY, paint);
canvas.drawLine(originX+, ascentY, originX + , ascentY, paint); //descent line
paint.setColor(Color.YELLOW);
canvas.drawText("descent line", originX + , descentY, paint);
canvas.drawLine(originX+, descentY, originX + , descentY, paint); //bottom line
paint.setColor(Color.BLUE);
canvas.drawText("bottom line", originX - , descentY, paint);
canvas.drawLine(originX-, bottomY, originX + , bottomY, paint);
}

注意其中画弧形时要指定一个矩形区域,画文字时各种线的位置

上一篇:Linux平台下快速搭建FTP服务器


下一篇:Linux下快速搭建php开发环境