Guest User

Untitled

a guest
Jul 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. public class Tutorial2D3 extends Activity {
  2.  
  3. Panel panel;
  4. /** Called when the activity is first created. */
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. panel = new Panel(this);
  9. setContentView(panel);
  10. }
  11.  
  12. @Override
  13. public boolean onCreateOptionsMenu(Menu menu) {
  14. menu.add(1, 1, 1, "Clean Canvas");
  15. return super.onCreateOptionsMenu(menu);
  16. }
  17.  
  18. @Override
  19. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  20. panel.cleanCanvas();
  21. return true;
  22. }
  23.  
  24. class Panel extends SurfaceView implements SurfaceHolder.Callback {
  25. TutorialThread thread;
  26. Bitmap icon;
  27. int iconWidth;
  28. int iconHeight;
  29. int touchX;
  30. int touchY;
  31. int mCount = 0;
  32.  
  33. public Panel(Context context) {
  34. super(context);
  35. icon = BitmapFactory
  36. .decodeResource(getResources(), R.drawable.icon);
  37. iconWidth = icon.getWidth();
  38. iconHeight = icon.getHeight();
  39. getHolder().addCallback(this);
  40. thread = new TutorialThread(getHolder(), this);
  41. setFocusable(true);
  42. }
  43.  
  44. @Override
  45. protected void onDraw(Canvas canvas) {
  46. int x = touchX - (iconWidth / 2);
  47. int y = touchY - (iconHeight / 2);
  48. if(mCount>0) {
  49. canvas.drawColor(Color.BLACK);
  50. mCount--;
  51. }
  52. canvas.drawBitmap(icon, (x > 0 ? x : 0), (y > 0 ? y : 0), null);
  53. }
  54.  
  55. @Override
  56. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  57. int height) {
  58. // TODO Auto-generated method stub
  59.  
  60. }
  61.  
  62. @Override
  63. public void surfaceCreated(SurfaceHolder holder) {
  64. thread.setRunning(true);
  65. thread.start();
  66. }
  67.  
  68. @Override
  69. public void surfaceDestroyed(SurfaceHolder holder) {
  70. boolean retry = true;
  71. thread.setRunning(false);
  72. do {
  73. try {
  74. thread.join();
  75. retry = false;
  76. } catch (InterruptedException e) {
  77. e.printStackTrace();
  78. }
  79. } while (retry);
  80. }
  81.  
  82. @Override
  83. public boolean onTouchEvent(MotionEvent event) {
  84. switch (event.getAction()) {
  85. case MotionEvent.ACTION_DOWN:
  86. touchX = (int) event.getX();
  87. touchY = (int) event.getY();
  88. break;
  89. case MotionEvent.ACTION_MOVE:
  90. touchX = (int) event.getX();
  91. touchY = (int) event.getY();
  92. break;
  93. case MotionEvent.ACTION_UP:
  94. break;
  95. default:
  96. break;
  97. }
  98. return true;
  99. }
  100.  
  101. private void cleanCanvas() {
  102. mCount = 2;
  103. }
  104. }
  105.  
  106.  
  107.  
  108. class TutorialThread extends Thread {
  109. private SurfaceHolder _surfaceHolder;
  110. private Panel _panel;
  111. private boolean _run = false;
  112.  
  113. public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
  114. _surfaceHolder = surfaceHolder;
  115. _panel = panel;
  116. }
  117.  
  118. public void setRunning(boolean run) {
  119. _run = run;
  120. }
  121.  
  122. @Override
  123. public void run() {
  124. Canvas c;
  125. while (_run) {
  126. c = null;
  127. try {
  128. c = _surfaceHolder.lockCanvas(null);
  129. synchronized (_surfaceHolder) {
  130. _panel.onDraw(c);
  131. }
  132. } finally {
  133. if (c != null) {
  134. _surfaceHolder.unlockCanvasAndPost(c);
  135. }
  136. }
  137. }
  138. }
  139. }
  140.  
  141. @Override
  142. public void run() {
  143. while (mRun) {
  144. Canvas c = null;
  145. try {
  146. c = mSurfaceHolder.lockCanvas(Rectangle);
  147. synchronized (mSurfaceHolder) {
  148. if (mMode == STATE_RUNNING) updatePhysics();
  149. doDraw(c);
  150. }
  151. } finally {
  152. // do this in a finally so that if an exception is thrown
  153. // during the above, we don't leave the Surface in an
  154. // inconsistent state
  155. if (c != null) {
  156. mSurfaceHolder.unlockCanvasAndPost(c);
  157. }
  158. }
  159. }
  160. }
Add Comment
Please, Sign In to add comment