歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved