Canvas上绘制几何图形

时间:2023-02-06 22:10:09
重要的类自定义View组件要重写View组件的onDraw(Canvase)方法,接下来是在该 Canvas上绘制大量的几何图形,点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形,等各种形状! DrawView.java

[java] view plaincopy
  1. public class DrawView extends View {  
  2.   
  3.     public DrawView(Context context) {  
  4.         super(context);  
  5.     }  
  6.   
  7.     @Override  
  8.     protected void onDraw(Canvas canvas) {  
  9.         super.onDraw(canvas);  
  10.         /* 
  11.          * 方法 说明 drawRect 绘制矩形 drawCircle 绘制圆形 drawOval 绘制椭圆 drawPath 绘制任意多边形 
  12.          * drawLine 绘制直线 drawPoin 绘制点 
  13.          */  
  14.         // 创建画笔  
  15.         Paint p = new Paint();  
  16.         p.setColor(Color.RED);// 设置红色  
  17.   
  18.         canvas.drawText("画圆:"1020, p);// 画文本  
  19.         canvas.drawCircle(602010, p);// 小圆  
  20.         p.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除,大家一看效果就明白了  
  21.         canvas.drawCircle(1202020, p);// 大圆  
  22.   
  23.         canvas.drawText("画线及弧线:"1060, p);  
  24.         p.setColor(Color.GREEN);// 设置绿色  
  25.         canvas.drawLine(604010040, p);// 画线  
  26.         canvas.drawLine(1104019080, p);// 斜线  
  27.         //画笑脸弧线  
  28.         p.setStyle(Paint.Style.STROKE);//设置空心  
  29.         RectF oval1=new RectF(150,20,180,40);  
  30.         canvas.drawArc(oval1, 180180false, p);//小弧形  
  31.         oval1.set(1902022040);  
  32.         canvas.drawArc(oval1, 180180false, p);//小弧形  
  33.         oval1.set(1603021060);  
  34.         canvas.drawArc(oval1, 0180false, p);//小弧形  
  35.   
  36.         canvas.drawText("画矩形:"1080, p);  
  37.         p.setColor(Color.GRAY);// 设置灰色  
  38.         p.setStyle(Paint.Style.FILL);//设置填满  
  39.         canvas.drawRect(60608080, p);// 正方形  
  40.         canvas.drawRect(6090160100, p);// 长方形  
  41.   
  42.         canvas.drawText("画扇形和椭圆:"10120, p);  
  43.         /* 设置渐变色 这个正方形的颜色是改变的 */  
  44.         Shader mShader = new LinearGradient(00100100,  
  45.                 new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW,  
  46.                         Color.LTGRAY }, null, Shader.TileMode.REPEAT); // 一个材质,打造出一个线性梯度沿著一条线。  
  47.         p.setShader(mShader);  
  48.         // p.setColor(Color.BLUE);  
  49.         RectF oval2 = new RectF(60100200240);// 设置个新的长方形,扫描测量  
  50.         canvas.drawArc(oval2, 200130true, p);  
  51.         // 画弧,第一个参数是RectF:该类是第二个参数是角度的开始,第三个参数是多少度,第四个参数是真的时候画扇形,是假的时候画弧线  
  52.         //画椭圆,把oval改一下  
  53.         oval2.set(210,100,250,130);  
  54.         canvas.drawOval(oval2, p);  
  55.   
  56.         canvas.drawText("画三角形:"10200, p);  
  57.         // 绘制这个三角形,你可以绘制任意多边形  
  58.         Path path = new Path();  
  59.         path.moveTo(80200);// 此点为多边形的起点  
  60.         path.lineTo(120250);  
  61.         path.lineTo(80250);  
  62.         path.close(); // 使这些点构成封闭的多边形  
  63.         canvas.drawPath(path, p);  
  64.   
  65.         // 你可以绘制很多任意多边形,比如下面画六连形  
  66.         p.reset();//重置  
  67.         p.setColor(Color.LTGRAY);  
  68.         p.setStyle(Paint.Style.STROKE);//设置空心  
  69.         Path path1=new Path();  
  70.         path1.moveTo(180200);  
  71.         path1.lineTo(200200);  
  72.         path1.lineTo(210210);  
  73.         path1.lineTo(200220);  
  74.         path1.lineTo(180220);  
  75.         path1.lineTo(170210);  
  76.         path1.close();//封闭  
  77.         canvas.drawPath(path1, p);  
  78.         /* 
  79.          * Path类封装复合(多轮廓几何图形的路径 
  80.          * 由直线段*、二次曲线,和三次方曲线,也可画以油画。drawPath(路径、油漆),要么已填充的或抚摸 
  81.          * (基于油漆的风格),或者可以用于剪断或画画的文本在路径。 
  82.          */  
  83.           
  84.         //画圆角矩形  
  85.         p.setStyle(Paint.Style.FILL);//充满  
  86.         p.setColor(Color.LTGRAY);  
  87.         p.setAntiAlias(true);// 设置画笔的锯齿效果  
  88.         canvas.drawText("画圆角矩形:"10260, p);  
  89.         RectF oval3 = new RectF(80260200300);// 设置个新的长方形  
  90.         canvas.drawRoundRect(oval3, 2015, p);//第二个参数是x半径,第三个参数是y半径  
  91.           
  92.         //画贝塞尔曲线  
  93.         canvas.drawText("画贝塞尔曲线:"10310, p);  
  94.         p.reset();  
  95.         p.setStyle(Paint.Style.STROKE);  
  96.         p.setColor(Color.GREEN);  
  97.         Path path2=new Path();  
  98.         path2.moveTo(100320);//设置Path的起点  
  99.         path2.quadTo(150310170400); //设置贝塞尔曲线的控制点坐标和终点坐标  
  100.         canvas.drawPath(path2, p);//画出贝塞尔曲线  
  101.           
  102.         //画点  
  103.         p.setStyle(Paint.Style.FILL);  
  104.         canvas.drawText("画点:"10390, p);  
  105.         canvas.drawPoint(60390, p);//画一个点  
  106.         canvas.drawPoints(new float[]{60,400,65,400,70,400}, p);//画多个点  
  107.           
  108.         //画图片,就是贴图  
  109.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);  
  110.         canvas.drawBitmap(bitmap, 250,360, p);  
  111.     }  
  112. }