Advertisement
Guest User

Code of Surfaceview

a guest
Mar 13th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. public class MySurfaceview extends SurfaceView implements SurfaceHolder.Callback {
  2.  
  3. private MySurfaceThread thread;
  4.  
  5. public MySurfaceview(Context context) {
  6. this(context,null);
  7. }
  8.  
  9. public MySurfaceview(Context context, AttributeSet attrs) {
  10. super(context,attrs);
  11. mContext = context;
  12. //retreivig bitmaps from resources
  13. getHolder().addCallback(this);
  14.  
  15. thread = new MySurfaceThread(getHolder(),this);
  16.  
  17. setOnTouchListener(new OnTouchListener() {
  18. @Override
  19. public boolean onTouch(View v, MotionEvent event) {
  20. if(event.getAction() == MotionEvent.ACTION_DOWN) {
  21. .........
  22. .........
  23. }
  24. else if(event.getAction() == MotionEvent.ACTION_MOVE) {
  25. .......
  26. ......
  27. }
  28. else if(event.getAction() == MotionEvent.ACTION_UP) {
  29. .......
  30. ......
  31. }
  32. return true;
  33. }
  34. });
  35. }
  36.  
  37. protected void onDraw(Canvas canvas) {
  38. super.onDraw(canvas);
  39. canvas.drawRGB(0, 0, 0);
  40. // Drawing bitmaps on to the view here
  41. }
  42.  
  43. @Override
  44. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  45. int height) {
  46. // TODO Auto-generated method stub
  47.  
  48. }
  49. @Override
  50. public void surfaceCreated(SurfaceHolder holder) {
  51. // TODO Auto-generated method stub
  52. thread.setRunning(true);
  53. thread.start();
  54. }
  55. @Override
  56. public void surfaceDestroyed(SurfaceHolder holder) {
  57. // TODO Auto-generated method stub
  58. boolean retry = true;
  59. thread.setRunning(false);
  60. while (retry) {
  61. try {
  62. thread.join();
  63. retry = false;
  64. }
  65. catch (InterruptedException e) {
  66. }
  67. }
  68. }
  69. public class MySurfaceThread extends Thread {
  70. private SurfaceHolder myThreadSurfaceHolder;
  71. private MySurfaceview myThreadSurfaceView;
  72. private boolean myThreadRun = false;
  73.  
  74. public MySurfaceThread(SurfaceHolder surfaceHolder, MySurfaceview surfaceView) {
  75. myThreadSurfaceHolder = surfaceHolder;
  76. myThreadSurfaceView = surfaceView;
  77. }
  78.  
  79. public void setRunning(boolean b) {
  80. myThreadRun = b;
  81. }
  82.  
  83. @Override
  84. public void run() {
  85. // TODO Auto-generated method stub
  86. while(myThreadRun){
  87. // myThreadSurfaceView.re
  88. Canvas c = null;
  89.  
  90. try{
  91. c = myThreadSurfaceHolder.lockCanvas(null);
  92. synchronized (myThreadSurfaceHolder){
  93. // sleep(100);
  94. myThreadSurfaceView.onDraw(c);
  95. }
  96. //
  97. } catch (Exception e) {
  98. // TODO Auto-generated catch block
  99. e.printStackTrace();
  100. }
  101. finally{
  102. // do this in a finally so that if an exception is thrown
  103. // during the above, we don't leave the Surface in an
  104. // inconsistent state
  105. if (c != null) {
  106. myThreadSurfaceHolder.unlockCanvasAndPost(c);
  107. }
  108. }
  109. }
  110. }
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement