Canvas 又稱爲畫布,在遊戲開發過程當中是一個常用的類。下面舉一個實例在完成畫布的基本功能 canvas
代碼以下: ide
Java代碼
- public class MySurfaceView extends SurfaceView implements Callback, Runnable {
- //用於控制SurfaceView
- private SurfaceHolder sfh;
- //聲明一個畫筆
- private Paint paint;
- //聲明一條線程
- private Thread th;
- //線程消亡的標識位
- private boolean flag;
- //聲明一個畫布
- private Canvas canvas;
- //聲明屏幕的寬高
- private int screenW, screenH;
- //設置畫布繪圖無鋸齒
- private PaintFlagsDrawFilter pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
- /**
- * SurfaceView初始化函數
- */
- public MySurfaceView(Context context) {
- super(context);
- //實例SurfaceHolder
- sfh = this.getHolder();
- //爲SurfaceView添加狀態監聽
- sfh.addCallback(this);
- //實例一個畫筆
- paint = new Paint();
- //設置畫筆顏色爲白色
- paint.setColor(Color.WHITE);
- //設置焦點
- setFocusable(true);
- }
-
- /**
- * SurfaceView視圖建立,響應此函數
- */
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- screenW = this.getWidth();
- screenH = this.getHeight();
- flag = true;
- //實例線程
- th = new Thread(this);
- //啓動線程
- th.start();
- }
-
- /**
- * 遊戲繪圖
- */
- public void myDraw() {
- try {
- canvas = sfh.lockCanvas();
- if (canvas != null) {
- //----設置畫布繪圖無鋸齒
- canvas.setDrawFilter(pfd);
- //----利用填充畫布,刷屏
- canvas.drawColor(Color.BLACK);
- //----繪製文本
- canvas.drawText("drawText", 10, 10, paint);
- //----繪製像素點
- canvas.drawPoint(10, 20, paint);
- //----繪製多個像素點
- canvas.drawPoints(new float[] { 10, 30, 30, 30 }, paint);
- //----繪製直線
- canvas.drawLine(10, 40, 50, 40, paint);
- //----繪製多條直線
- canvas.drawLines(new float[] { 10, 50, 50, 50, 70, 50, 110, 50 }, paint);
- //----繪製矩形
- canvas.drawRect(10, 60, 40, 100, paint);
- //----繪製矩形2
- Rect rect = new Rect(10, 110, 60, 130);
- canvas.drawRect(rect, paint);
- canvas.drawRect(rect, paint);
- //----繪製圓角矩形
- RectF rectF = new RectF(10, 140, 60, 170);
- canvas.drawRoundRect(rectF, 20, 20, paint);
- //----繪製圓形
- canvas.drawCircle(20, 200, 20, paint);
- //----繪製弧形
- canvas.drawArc(new RectF(150, 20, 200, 70), 0, 230, true, paint);
- //----繪製橢圓
- canvas.drawOval(new RectF(150, 80, 180, 100), paint);
- //----繪製指定路徑圖形
- Path path = new Path();
- //設置路徑起點
- path.moveTo(160, 150);
- //路線1
- path.lineTo(200, 150);
- //路線2
- path.lineTo(180, 200);
- //路徑結束
- path.close();
- canvas.drawPath(path, paint);
- //----繪製指定路徑圖形
- Path pathCircle = new Path();
- //添加一個圓形的路徑
- pathCircle.addCircle(130, 260, 20, Path.Direction.CCW);
- //----繪製帶圓形的路徑文本
- canvas.drawTextOnPath("PathText", pathCircle, 10, 20, paint);
- }
- } catch (Exception e) {
- // TODO: handle exception
- } finally {
- if (canvas != null)
- sfh.unlockCanvasAndPost(canvas);
- }
- }
-
- /**
- * 觸屏事件監聽
- */
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- return true;
- }
-
- /**
- * 按鍵事件監聽
- */
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- return super.onKeyDown(keyCode, event);
- }
-
-
- @Override
- public void run() {
- while (flag) {
- long start = System.currentTimeMillis();
- myDraw();
- long end = System.currentTimeMillis();
- try {
- if (end - start < 50) {
- Thread.sleep(50 - (end - start));
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * SurfaceView視圖狀態發生改變,響應此函數
- */
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- }
-
- /**
- * SurfaceView視圖消亡時,響應此函數
- */
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- flag = false;
- }