android canvas 使用

Canvas 又稱爲畫布,在遊戲開發過程當中是一個常用的類。下面舉一個實例在完成畫布的基本功能 canvas

代碼以下: ide

Java代碼   收藏代碼
  1. public class MySurfaceView extends SurfaceView implements Callback, Runnable {  
  2.     //用於控制SurfaceView  
  3.     private SurfaceHolder sfh;  
  4.     //聲明一個畫筆  
  5.     private Paint paint;  
  6.     //聲明一條線程  
  7.     private Thread th;  
  8.     //線程消亡的標識位  
  9.     private boolean flag;  
  10.     //聲明一個畫布  
  11.     private Canvas canvas;  
  12.     //聲明屏幕的寬高  
  13.     private int screenW, screenH;  
  14.     //設置畫布繪圖無鋸齒  
  15.     private PaintFlagsDrawFilter pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);  
  16.     /** 
  17.      * SurfaceView初始化函數 
  18.      */  
  19.     public MySurfaceView(Context context) {  
  20.         super(context);  
  21.         //實例SurfaceHolder  
  22.         sfh = this.getHolder();  
  23.         //爲SurfaceView添加狀態監聽  
  24.         sfh.addCallback(this);  
  25.         //實例一個畫筆  
  26.         paint = new Paint();  
  27.         //設置畫筆顏色爲白色  
  28.         paint.setColor(Color.WHITE);  
  29.         //設置焦點  
  30.         setFocusable(true);  
  31.     }  
  32.   
  33.     /** 
  34.      * SurfaceView視圖建立,響應此函數 
  35.      */  
  36.     @Override  
  37.     public void surfaceCreated(SurfaceHolder holder) {  
  38.         screenW = this.getWidth();  
  39.         screenH = this.getHeight();  
  40.         flag = true;  
  41.         //實例線程  
  42.         th = new Thread(this);  
  43.         //啓動線程  
  44.         th.start();  
  45.     }  
  46.   
  47.     /** 
  48.      * 遊戲繪圖 
  49.      */  
  50. public void myDraw() {  
  51.     try {  
  52.         canvas = sfh.lockCanvas();  
  53.         if (canvas != null) {  
  54.             //----設置畫布繪圖無鋸齒  
  55.             canvas.setDrawFilter(pfd);  
  56.             //----利用填充畫布,刷屏  
  57.             canvas.drawColor(Color.BLACK);  
  58.             //----繪製文本  
  59.             canvas.drawText("drawText"1010, paint);  
  60.             //----繪製像素點  
  61.             canvas.drawPoint(1020, paint);  
  62.             //----繪製多個像素點  
  63.             canvas.drawPoints(new float[] { 10303030 }, paint);  
  64.             //----繪製直線  
  65.             canvas.drawLine(10405040, paint);  
  66.             //----繪製多條直線  
  67.             canvas.drawLines(new float[] { 10505050705011050 }, paint);  
  68.             //----繪製矩形  
  69.             canvas.drawRect(106040100, paint);  
  70.             //----繪製矩形2  
  71.             Rect rect = new Rect(1011060130);  
  72.             canvas.drawRect(rect, paint);  
  73.             canvas.drawRect(rect, paint);  
  74.             //----繪製圓角矩形  
  75.             RectF rectF = new RectF(1014060170);  
  76.             canvas.drawRoundRect(rectF, 2020, paint);  
  77.             //----繪製圓形  
  78.             canvas.drawCircle(2020020, paint);  
  79.             //----繪製弧形  
  80.             canvas.drawArc(new RectF(1502020070), 0230true, paint);  
  81.             //----繪製橢圓  
  82.             canvas.drawOval(new RectF(15080180100), paint);  
  83.             //----繪製指定路徑圖形  
  84.             Path path = new Path();  
  85.             //設置路徑起點  
  86.             path.moveTo(160150);  
  87.             //路線1  
  88.             path.lineTo(200150);  
  89.             //路線2  
  90.             path.lineTo(180200);  
  91.             //路徑結束  
  92.             path.close();  
  93.             canvas.drawPath(path, paint);  
  94.             //----繪製指定路徑圖形  
  95.             Path pathCircle = new Path();  
  96.             //添加一個圓形的路徑  
  97.             pathCircle.addCircle(13026020, Path.Direction.CCW);  
  98.             //----繪製帶圓形的路徑文本  
  99.             canvas.drawTextOnPath("PathText", pathCircle, 1020, paint);  
  100.         }  
  101.     } catch (Exception e) {  
  102.         // TODO: handle exception  
  103.     } finally {  
  104.         if (canvas != null)  
  105.             sfh.unlockCanvasAndPost(canvas);  
  106.     }  
  107. }  
  108.   
  109.     /** 
  110.      * 觸屏事件監聽 
  111.      */  
  112.     @Override  
  113.     public boolean onTouchEvent(MotionEvent event) {  
  114.         return true;  
  115.     }  
  116.   
  117.     /** 
  118.      * 按鍵事件監聽 
  119.      */  
  120.     @Override  
  121.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  122.         return super.onKeyDown(keyCode, event);  
  123.     }  
  124.   
  125.   
  126.     @Override  
  127.     public void run() {  
  128.         while (flag) {  
  129.             long start = System.currentTimeMillis();  
  130.             myDraw();  
  131.             long end = System.currentTimeMillis();  
  132.             try {  
  133.                 if (end - start < 50) {  
  134.                     Thread.sleep(50 - (end - start));  
  135.                 }  
  136.             } catch (InterruptedException e) {  
  137.                 e.printStackTrace();  
  138.             }  
  139.         }  
  140.     }  
  141.   
  142.     /** 
  143.      * SurfaceView視圖狀態發生改變,響應此函數 
  144.      */  
  145.     @Override  
  146.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {  
  147.     }  
  148.   
  149.     /** 
  150.      * SurfaceView視圖消亡時,響應此函數 
  151.      */  
  152.     @Override  
  153.     public void surfaceDestroyed(SurfaceHolder holder) {  
  154.         flag = false;  
  155.     }  
相關文章
相關標籤/搜索