Advertisement
Guest User

Untitled

a guest
Jul 14th, 2011
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. public class DrawActivity extends Activity {
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. dp=new DrawingPanel(this);
  5. setContentView(dp);
  6.  
  7. mPaint = new Paint();
  8. mPaint.setDither(true);
  9. mPaint.setColor(0xFFFF0000);
  10. mPaint.setStyle(Paint.Style.STROKE);
  11. mPaint.setStrokeJoin(Paint.Join.ROUND);
  12. mPaint.setStrokeCap(Paint.Cap.ROUND);
  13. mPaint.setStrokeWidth(8);
  14. }
  15.  
  16. class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
  17. // private DrawingThread _thread;
  18. public DrawingPanel(Context context) {
  19.  
  20. super(context);
  21. System.out.println(" i'm in Drawing panel context");
  22. getHolder().addCallback(this);
  23. _thread = new DrawingThread(getHolder(), this);
  24.  
  25. }
  26.  
  27. public boolean onTouchEvent(MotionEvent event) {
  28. synchronized (_thread.getSurfaceHolder()) {
  29.  
  30. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  31. System.out.println("i'm in ACTION_DOWN ");
  32.  
  33.  
  34. path.addCircle(event.getX(), event.getY(), 2,
  35. Path.Direction.CCW);
  36. _graphics.add(path);
  37. }
  38.  
  39. return true;
  40. }
  41. }
  42.  
  43. public void onDraw(Canvas canvas) {
  44. canvas1=canvas;
  45. canvas.drawColor(Color.WHITE);
  46. if (onSelect == true) {
  47. onSelect = false;
  48. _graphics.clear();
  49. }
  50. for (Path path : _graphics) {
  51. canvas.drawPath(path, mPaint);
  52. }
  53.  
  54. }
  55.  
  56.  
  57. @Override
  58. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  59. int hight) {
  60. // TODO Auto-generated method stub
  61. }
  62.  
  63. @Override
  64. public void surfaceCreated(SurfaceHolder holder) {
  65. // TODO Auto-generated method stub
  66. System.out.println(" I'M in surface created method method");
  67. _thread.setRunning(true);
  68. _thread.start();
  69.  
  70. }
  71.  
  72. @Override
  73. public void surfaceDestroyed(SurfaceHolder holder) {
  74. // TODO Auto-generated method stub
  75. // canvas.drawPath(path, mPaint);
  76. boolean retry = true;
  77. _thread.setRunning(false);
  78. while (retry) {
  79. try {
  80. _thread.join();
  81. retry = false;
  82. System.out.println(" I'M in surface DESTROYED method");
  83. } catch (InterruptedException e) {
  84. // we will try it again and again...
  85. }
  86. }
  87. }
  88. }
  89.  
  90. class DrawingThread extends Thread {
  91.  
  92. private boolean _run = false;
  93. public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
  94.  
  95. _surfaceHolder = surfaceHolder;
  96. _panel = panel;
  97. }
  98.  
  99. public void setRunning(boolean run) {
  100. _run = run;
  101. }
  102.  
  103. public SurfaceHolder getSurfaceHolder() {
  104. return _surfaceHolder;
  105. }
  106.  
  107. @Override
  108. public void run() {
  109.  
  110. //Point point = new Point();
  111. while (_run) {
  112. c = null;
  113. try {
  114. c = _surfaceHolder.lockCanvas(null);
  115. synchronized (_surfaceHolder) {
  116. _panel.onDraw(c);
  117. }
  118. }
  119. finally {
  120. // do this in a finally so that if an exception is thrown
  121. // during the above, we don't leave the Surface in an
  122. // inconsistent state
  123. if (c != null) {
  124. _surfaceHolder.unlockCanvasAndPost(c);
  125. }
  126. }
  127. }
  128. }
  129. }
  130.  
  131. // TODO Auto-generated method stub
  132. public boolean onCreateOptionsMenu(Menu menu) {
  133.  
  134. MenuInflater inflater = getMenuInflater();
  135. inflater.inflate(R.menu.menu1, menu);
  136. return true;
  137. }
  138.  
  139. public boolean onOptionsItemSelected(MenuItem item) {
  140. switch (item.getItemId()) {
  141. case R.id.changeScreen:
  142. onSelect = true; ----->this means i clear the screen first then want to made changes
  143.  
  144. //here i want to draw different to that canvas by calling this method
  145. changeScreen();
  146.  
  147. break;
  148. }
  149. return true;
  150. }
  151. private void changeScreen()
  152. {
  153. Path path1=new Path();
  154. path1.addCircle(50, 60, 12,Path.Direction.CCW);
  155. canvas1.drawPath(path1, mPaint);
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement