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

Android畫圖Path類的使用

Android畫圖Path類的使用:

  1.     package com.javaeye.android.my;     
  2.          
  3.     import android.app.Activity;     
  4.     import android.os.Bundle;     
  5.     import android.view.View;     
  6.     import android.content.Context;     
  7.     import android.graphics.Canvas;     
  8.     import android.graphics.Color;     
  9.     import android.graphics.Paint;     
  10.     import android.graphics.RectF;     
  11.     import android.graphics.Path;     
  12.     import android.graphics.Shader;     
  13.     import android.graphics.LinearGradient;     
  14.   
  15. //主程序中繼承自Android.view.View的 MyView類,重寫 MyView的onDraw()方法,      
  16. //一開始就會運行繪制的工作,在onDraw()中以Paint將幾何圖形繪制在Canvas上,      
  17. //以 paint.setColor() 改變圖形顏色、以 paint.setStyle()的設置來控制畫出的的      
  18. //圖形是空心還是實心。程序的最後一段,就是直接在Canvas寫上文字,隨著Paint對      
  19. //象裡的屬性設置,也會有不同的外觀模式。   
  20.   
  21.     public class GraphicesActivity extends Activity {     
  22.         /** Called when the activity is first created. */    
  23.         @Override    
  24.         public void onCreate(Bundle savedInstanceState) {     
  25.             super.onCreate(savedInstanceState);     
  26.             /*設置ContentView為自定義的MyVieW*/    
  27.             MyView myView=new MyView(this);     
  28.             setContentView(myView);     
  29.         }     
  30.               
  31.         /* 自定義繼承View 的MyView*/    
  32.         private class MyView extends View {     
  33.              public MyView(Context context){     
  34.                 super(context) ;     
  35.              }     
  36.                   
  37.              /*重寫onDraw()*/    
  38.              @Override    
  39.              protected void onDraw(Canvas canvas)     
  40.              {        
  41.                   super.onDraw(canvas);     
  42.                   /*設置背景為白色*/    
  43.                   canvas.drawColor(Color.WHITE);     
  44.                         
  45.                   Paint paint=new Paint();     
  46.                   /*去鋸齒*/    
  47.                   paint.setAntiAlias(true);     
  48.                   /*設置paint的顏色*/    
  49.                   paint.setColor(Color.RED);     
  50.                   /*設置paint的 style 為STROKE:空心*/    
  51.                   paint.setStyle(Paint.Style.STROKE);     
  52.                   /*設置paint的外框寬度*/    
  53.                   paint.setStrokeWidth(3);     
  54.                         
  55.                   /*畫一個空心圓形*/    
  56.                   canvas.drawCircle(404030, paint);     
  57.                   /*畫一個空心正方形*/    
  58.                   canvas.drawRect(109070150, paint);     
  59.                   /*畫一個空心長方形*/    
  60.                   canvas.drawRect(1017070,200, paint);     
  61.                   /*畫一個空心橢圓形*/    
  62.                   canvas.drawOval(new RectF(10,220,70,250), paint);     
  63.                   /*畫一個空心三角形*/    
  64.                   Path path=new Path();     
  65.                   path.moveTo(10330);     
  66.                   path.lineTo(70,330);     
  67.                   path.lineTo(40,270);     
  68.                   path.close();     
  69.                   canvas.drawPath(path, paint);     
  70.                   /*畫一個空心梯形*/    
  71.                   Path path1=new Path();     
  72.                   path1.moveTo(10410);     
  73.                   path1.lineTo(70,410);     
  74.                   path1.lineTo(55,350);     
  75.                   path1.lineTo(25350);     
  76.                   path1.close();     
  77.                   canvas.drawPath(path1, paint);     
  78.                         
  79.                   /*設置paint的顏色*/    
  80.                   paint.setColor(Color.BLUE);     
  81.                   /*設置paint 的style為 FILL:實心*/    
  82.                   paint.setStyle(Paint.Style.FILL);     
  83.                   /*畫一個實心圓*/    
  84.                   canvas.drawCircle(120,40,30, paint);     
  85.                   /*畫一個實心正方形*/    
  86.                   canvas.drawRect(9090150150, paint);     
  87.                   /*畫一個實心長方形*/    
  88.                   canvas.drawRect(90170150,200, paint);     
  89.                   /*畫一個實心橢圓*/    
  90.                   RectF re2=new RectF(90,220,150,250);     
  91.                   canvas.drawOval(re2, paint);     
  92.                   /*畫一個實心三角形*/    
  93.                   Path path2=new Path();     
  94.                   path2.moveTo(90330);     
  95.                   path2.lineTo(150,330);     
  96.                   path2.lineTo(120,270);     
  97.                   path2.close();     
  98.                   canvas.drawPath(path2, paint);     
  99.                   /*畫一個實心梯形*/    
  100.                   Path path3=new Path();     
  101.                   path3.moveTo(90410);     
  102.                   path3.lineTo(150,410);     
  103.                   path3.lineTo(135,350);     
  104.                   path3.lineTo(105350);     
  105.                   path3.close();     
  106.                   canvas.drawPath(path3, paint);     
  107.                         
  108.                   /*設置漸變色*/    
  109.                   Shader mShader=new LinearGradient(0,0,100,100,     
  110.                           new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},     
  111.                           null,Shader.TileMode.REPEAT);     
  112.                   //Shader.TileMode三種模式      
  113.                   //REPEAT:沿著漸變方向循環重復      
  114.                   //CLAMP:如果在預先定義的范圍外畫的話,就重復邊界的顏色      
  115.                   //MIRROR:與REPEAT一樣都是循環重復,但這個會對稱重復      
  116.                   paint.setShader(mShader);//用Shader中定義定義的顏色來話      
  117.                      
  118.                   /*畫一個漸變色圓*/    
  119.                   canvas.drawCircle(200,40,30, paint);     
  120.                   /*畫一個漸變色正方形*/    
  121.                   canvas.drawRect(17090230150, paint);     
  122.                   /*畫一個漸變色長方形*/    
  123.                   canvas.drawRect(170170230,200, paint);     
  124.                   /*畫一個漸變色橢圓*/    
  125.                   RectF re3=new RectF(170,220,230,250);     
  126.                   canvas.drawOval(re3, paint);     
  127.                   /*畫一個漸變色三角形*/    
  128.                   Path path4=new Path();     
  129.                   path4.moveTo(170,330);     
  130.                   path4.lineTo(230,330);     
  131.                   path4.lineTo(200,270);     
  132.                   path4.close();     
  133.                   canvas.drawPath(path4, paint);     
  134.                   /*畫一個漸變色梯形*/    
  135.                   Path path5=new Path();     
  136.                   path5.moveTo(170410);     
  137.                   path5.lineTo(230,410);     
  138.                   path5.lineTo(215,350);     
  139.                   path5.lineTo(185350);     
  140.                   path5.close();     
  141.                   canvas.drawPath(path5, paint);     
  142.                         
  143.                   /*寫字*/    
  144.                   paint.setTextSize(24);     
  145.                   canvas.drawText("圓形"24050, paint);     
  146.                   canvas.drawText("正方形"240120, paint);     
  147.                   canvas.drawText("長方形"240190, paint);     
  148.                   canvas.drawText("橢圓形"240250, paint);     
  149.                   canvas.drawText("三角形"240320, paint);     
  150.                   canvas.drawText("梯形"240390, paint);       
  151.              }     
  152.         }     
  153.     }   
Copyright © Linux教程網 All Rights Reserved