Android开发:使用View绘制矩形及多边形


  1. import Android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Paint;  
  6. import android.graphics.RectF;  
  7. import android.graphics.Color;  
  8. import android.view.View;  
  9.   
  10. public class DrawActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(new DrawView(this));  
  16.     }  
  17.       
  18.     private static class DrawView extends View {  
  19.         //绘画类,设置绘图的样式和颜色。   
  20.         private Paint[] mPaints;      
  21.   
  22.         private Paint mFramePaint;  
  23.         //矩形(上,下,左,右)   
  24.         private RectF[] mOvals;  
  25.         private RectF mBigOval;  
  26.           
  27.         private float mStart;  
  28.         private float mSweep;  
  29.         private int mBigIndex;  
  30.         private boolean[] mUseCenters;  
  31.           
  32.         private static final float START_INC = 0;  //开始角度   
  33.         private static final float SWEEP_INC = 2;  //扫除的角度   
  34.           
  35.           
  36.         public DrawView(Context context) {  
  37.             super(context);  
  38.               
  39.             mPaints = new Paint[4];  
  40.             mUseCenters = new boolean[4];  
  41.             mOvals = new RectF[4];  
  42.       
  43.             mPaints[0] = new Paint();  
  44.             mPaints[0].setAntiAlias(true);  //抗锯齿   
  45.             mPaints[0].setColor(0x88111111);  
  46.             mUseCenters[0] = false;    //为true绕中心绘图   
  47.               
  48.             mPaints[1] = new Paint(mPaints[0]);  
  49.             mPaints[1].setStyle(Paint.Style.FILL); //填充实心   
  50.             mPaints[1].setColor(0x88FF0000);  
  51.             mUseCenters[1] = true;  
  52.               
  53.             mPaints[2] = new Paint(mPaints[0]);  
  54.             mPaints[2].setStyle(Paint.Style.STROKE);   
  55.             mPaints[2].setStrokeWidth(10);  //画笔的宽度   
  56.             mPaints[2].setColor(0x8800FF00);  
  57.             mUseCenters[2] = false;  
  58.   
  59.             mPaints[3] = new Paint(mPaints[2]);  
  60.             mPaints[3].setStyle(Paint.Style.FILL_AND_STROKE);              
  61.             mPaints[3].setColor(0x880000FF);  
  62.             mUseCenters[3] = true;  
  63.               
  64.             mBigOval = new RectF(4010280250);  
  65.               
  66.             mOvals[0] = new RectF( 10270,  70330);  
  67.             mOvals[1] = new RectF( 90270150330);  
  68.             mOvals[2] = new RectF(170270230330);  
  69.             mOvals[3] = new RectF(250270310330);  
  70.               
  71.             mFramePaint = new Paint();  
  72.             mFramePaint.setAntiAlias(true);  
  73.             mFramePaint.setStyle(Paint.Style.STROKE);  
  74.             mFramePaint.setStrokeWidth(2);  
  75.         }  
  76.           
  77.         private void drawArcs(Canvas canvas, RectF oval,   
  78.             boolean useCenter, Paint paint) {  
  79.             canvas.drawRect(oval, mFramePaint);  
  80.             canvas.drawArc(oval, mStart, mSweep, useCenter, paint);  
  81.         }  
  82.           
  83.         @Override   
  84.         protected void onDraw(Canvas canvas) {  
  85.             canvas.drawColor(Color.WHITE);  
  86.               
  87.             drawArcs(canvas, mBigOval, mUseCenters[mBigIndex],  
  88.                      mPaints[mBigIndex]);  
  89.               
  90.             for (int i = 0; i < 4; i++) {  
  91.                 drawArcs(canvas, mOvals[i], mUseCenters[i], mPaints[i]);  
  92.             }  
  93.               
  94.             mSweep += SWEEP_INC;  
  95.             if (mSweep > 360) {  
  96.                 mSweep -= 360;  
  97.                 mStart += START_INC;   //0+0=0,起始角度始终为0   
  98.                 if (mStart >= 360) {  
  99.                     mStart -= 360;  
  100.                 }  
  101.                 mBigIndex = (mBigIndex + 1) % mOvals.length;  
  102.             }  
  103.             System.out.println("mSweep-->"+mSweep);  
  104.             System.out.println("mStart-->"+mStart);  
  105.             invalidate(); //重绘视图   
  106.         }  
  107.     }  
  108. }  
Android上所有的视图都继承View类。我们可以继承View,然后再OnDraw()方法里绘图,

OnDraw()方法不需要我们去调用,使用 invalidate(); 可重绘视图

相关内容