Android软件开发:在Canvas中利用Path绘制基本图形

在Android中绘制基本的集合图形,本程序就是自定义一个View组件,程序重写该View组件的onDraw(Canvase)方法,然后在该Canvas上绘制大量的基本的集合图形。

直接上代码:

1.自定义的View组件代码:

  1. package com.infy.configuration;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.LinearGradient;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Path;  
  9. import android.graphics.RectF;  
  10. import android.graphics.Shader;  
  11. import android.util.AttributeSet;  
  12. import android.view.View;  
  13.   
  14. public class MyView extends View{  
  15.   
  16.     public MyView(Context context, AttributeSet attrs) {  
  17.         super(context, attrs);  
  18.     }  
  19.   
  20.     @Override  
  21.     protected void onDraw(Canvas canvas) {  
  22.         // TODO Auto-generated method stub   
  23.         super.onDraw(canvas);  
  24.           
  25.         //把整张画布绘制成白色   
  26.         canvas.drawColor(Color.WHITE);  
  27.         Paint paint = new Paint();  
  28.           
  29.         //去锯齿   
  30.         paint.setAntiAlias(true);  
  31.         paint.setColor(Color.BLUE);  
  32.         paint.setStyle(Paint.Style.STROKE);  
  33.         paint.setStrokeWidth(3);  
  34.           
  35.         //绘制圆形   
  36.         canvas.drawCircle(404030, paint);  
  37.         //绘制正方形   
  38.         canvas.drawRect(108070140, paint);  
  39.         //绘制矩形   
  40.         canvas.drawRect(1015070190, paint);  
  41.   
  42.         RectF rel = new RectF(10,240,70,270);  
  43.         //绘制椭圆   
  44.         canvas.drawOval(rel, paint);  
  45.         //定义一个Path对象,封闭一个三角形   
  46.         Path path1 = new Path();  
  47.         path1.moveTo(10340);  
  48.         path1.lineTo(70340);  
  49.         path1.lineTo(40290);  
  50.         path1.close();  
  51.         //根据Path进行绘制,绘制三角形   
  52.         canvas.drawPath(path1, paint);  
  53.           
  54.         //定义一个Path对象,封闭一个五角星   
  55.         Path path2 = new Path();  
  56.         path2.moveTo(27360);  
  57.         path2.lineTo(54360);  
  58.         path2.lineTo(70392);  
  59.         path2.lineTo(40420);  
  60.         path2.lineTo(10392);  
  61.         path2.close();  
  62.         //根据Path进行绘制,绘制五角星   
  63.         canvas.drawPath(path2, paint);  
  64.           
  65.         //设置填丛风格后进行绘制   
  66.         paint.setStyle(Paint.Style.FILL);  
  67.         paint.setColor(Color.RED);  
  68.         canvas.drawCircle(1204030, paint);  
  69.         //绘制正方形   
  70.         canvas.drawRect(9080150140, paint);  
  71.         //绘制矩形   
  72.         canvas.drawRect(90150150190, paint);  
  73.           
  74.         //绘制圆角矩形   
  75.         RectF re2 = new RectF(90,200,150,230);  
  76.         canvas.drawRoundRect(re2, 1515, paint);  
  77.         //绘制椭圆   
  78.         RectF re21 = new RectF(90240150270);  
  79.         canvas.drawOval(re21, paint);  
  80.           
  81.         Path path3 = new Path();  
  82.         path3.moveTo(90340);  
  83.         path3.lineTo(150340);  
  84.         path3.lineTo(120290);  
  85.         path3.close();  
  86.         //绘制三角形   
  87.         canvas.drawPath(path3,paint);  
  88.           
  89.         //绘制五角形   
  90.         Path path4 = new Path();  
  91.         path4.moveTo(106360);  
  92.         path4.lineTo(134360);  
  93.         path4.lineTo(150392);  
  94.         path4.lineTo(120420);  
  95.         path4.lineTo(90392);  
  96.         path4.close();  
  97.         canvas.drawPath(path4, paint);  
  98.           
  99.         //设置渐变器后绘制   
  100.         //为Paint设置渐变器   
  101.         Shader mShasder = new LinearGradient(004060new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW}, null, Shader.TileMode.REPEAT);  
  102.         paint.setShader(mShasder);  
  103.         //设置阴影   
  104.         paint.setShadowLayer(451010, Color.GRAY);  
  105.         //绘制圆形   
  106.         canvas.drawCircle(2004030, paint);  
  107.         //绘制正方形   
  108.         canvas.drawRect(17080230140, paint);  
  109.         //绘制矩形   
  110.         canvas.drawRect(170150230190, paint);  
  111.           
  112.         //绘制圆角的矩形   
  113.         RectF re31 = new RectF();  
  114.         canvas.drawRoundRect(re31, 1515, paint);  
  115.           
  116.         //绘制椭圆   
  117.         RectF re32 =new RectF();  
  118.         canvas.drawOval(re32, paint);  
  119.           
  120.         //根据Path,绘制三角形   
  121.         Path path5 = new Path();  
  122.         path5.moveTo(170340);  
  123.         path5.lineTo(230340);  
  124.         path5.lineTo(200290);  
  125.         path5.close();  
  126.         canvas.drawPath(path5, paint);  
  127.           
  128.         //根据PAth,进行绘制五角形   
  129.         Path path6 = new Path();  
  130.         path6.moveTo(186360);  
  131.         path6.lineTo(214360);  
  132.         path6.lineTo(230392);  
  133.         path6.lineTo(200420);  
  134.         path6.lineTo(170392);  
  135.         path6.close();  
  136.         canvas.drawPath(path6, paint);  
  137.          
  138.     }  
  139.   
  140. }  

2. 使用一个基本的Activity来实现自定义的MyView组件,

定义一个ZiDingYiViewTes的Activity:

  1. package com.infy.configuration;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class ZiDingYiViewTes extends Activity{  
  7.       
  8.     private MyView myView =null;  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         // TODO Auto-generated method stub   
  13.         super.onCreate(savedInstanceState);  
  14.           
  15.         myView = new MyView(thisnull);  
  16.           
  17.         setContentView(myView);  
  18.           
  19.     }  
  20. }  

Android的Canvas既可以绘制简单的集合图形,也可以直接将一个Bitmap绘制到画布上。

最后附上效果图(多了一个椭圆Android软件开发:在Canvas中利用Path绘制基本图形):

Android软件开发:在Canvas中利用Path绘制基本图形

相关推荐