xml布局自定义SurfaceView模板

前端之家收集整理的这篇文章主要介绍了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,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模板

猜你在找的XML相关文章