xml佈局自定義SurfaceView模板

  1. package com.dream.apm;

  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.RectF;
  7. import android.util.AttributeSet;
  8. import android.util.Log;
  9. import android.view.MotionEvent;
  10. import android.view.SurfaceHolder;
  11. import android.view.SurfaceView;

  12. import java.io.UnsupportedEncodingException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Random;

  16. /**
  17. * Created by HuangZhiLong on 2015/1/22.
  18. */
  19. public class MySurfaceViewone extends SurfaceView implements SurfaceHolder.Callback,Runnable {

  20.     private Thread th;
  21.     private SurfaceHolder sfh;
  22.     private Canvas canvas;
  23.     private Paint paint;
  24.     private boolean flag;

  25.     public int screenW=0,screenH=0;


  26.     /**
  27.      * SurfaceView初始化函數
  28.      */
  29.     public MySurfaceViewone(Context context, AttributeSet attrs) {
  30.         super(context, attrs);

  31.         this.setKeepScreenOn(true);
  32.         sfh = this.getHolder();
  33.         sfh.addCallback(this);
  34.         paint = new Paint();
  35.         paint.setAntiAlias(true);//消除鋸齒
  36.         //paint.setTypeface(Typeface.DEFAULT_BOLD);
  37.         this.setFocusable(true);
  38.         this.setFocusableInTouchMode(true);


  39.     }

  40.     /**
  41.      * SurfaceView視圖創建,響應此函數
  42.      */
  43.     @Override
  44.     public void surfaceCreated(SurfaceHolder holder) {

  45.         screenW = this.getWidth();
  46.         screenH = this.getHeight();

  47.         myDraw();

  48.         //tt=new Rect(0,0,this.getWidth(),this.getHeight());
  49.         flag = true;
  50.         th = new Thread(this);
  51.         th.start();

  52.     }
  53.     /**
  54.      * 遊戲繪圖
  55.      */
  56.     public void myDraw() {
  57.         try {


  58.             canvas = sfh.lockCanvas();
  59.             canvas.drawColor(Color.rgb(90, 151, 161));//背景

  60.             paint.setStrokeWidth(0);
  61.             paint.setColor(Color.BLACK);
  62.             //paint.setTextSize(size);
  63.             //繪製表格


  64.         } catch (Exception e) {

  65.         } finally {
  66.             if (canvas != null)
  67.                 sfh.unlockCanvasAndPost(canvas);
  68.         }
  69.     }

  70.     /**
  71.      * 圖片的線程運行
  72.      */
  73.     public void run() {
  74.         while (flag) {
  75.             myDraw();

  76.             try {
  77.                 Thread.sleep(40);
  78.             } catch (Exception ex) {
  79.                 Log.e("ERROR", "Thread is Error!");
  80.             }
  81.         }
  82.     }

  83.     /**
  84.      * 觸屏事件監聽
  85.      */
  86.     @Override
  87.     public boolean onTouchEvent(MotionEvent event) {


  88.         switch (event.getAction())
  89.         {
  90.             //移動
  91.             case MotionEvent.ACTION_MOVE:


  92.                 break;
  93.             //按下
  94.             case MotionEvent.ACTION_DOWN:

  95.                 break;
  96.             //放開
  97.             case MotionEvent.ACTION_UP:

  98.                 break;

  99.         }
  100.         return true;
  101.     }



  102.     /**
  103.      * SurfaceView視圖狀態發生改變,響應此函數
  104.      */
  105.     @Override
  106.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  107.         System.out.println("ImageSurfaceView is surfaceChanged");

  108.     }
  109.     /**
  110.      * SurfaceView視圖消亡時,響應此函數
  111.      */
  112.     @Override
  113.     public void surfaceDestroyed(SurfaceHolder holder) {
  114.         System.out.println("ImageSurfaceView is surfaceDestroyed");
  115.         flag = false;//停止線程
  116.     }


  117. }
複製代碼
android中使用xml佈局自定義SurfaceView模板
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章