Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DrawActivity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- dp=new DrawingPanel(this);
- setContentView(dp);
- mPaint = new Paint();
- mPaint.setDither(true);
- mPaint.setColor(0xFFFF0000);
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setStrokeJoin(Paint.Join.ROUND);
- mPaint.setStrokeCap(Paint.Cap.ROUND);
- mPaint.setStrokeWidth(8);
- }
- class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
- // private DrawingThread _thread;
- public DrawingPanel(Context context) {
- super(context);
- System.out.println(" i'm in Drawing panel context");
- getHolder().addCallback(this);
- _thread = new DrawingThread(getHolder(), this);
- }
- public boolean onTouchEvent(MotionEvent event) {
- synchronized (_thread.getSurfaceHolder()) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- System.out.println("i'm in ACTION_DOWN ");
- path.addCircle(event.getX(), event.getY(), 2,
- Path.Direction.CCW);
- _graphics.add(path);
- }
- return true;
- }
- }
- public void onDraw(Canvas canvas) {
- canvas1=canvas;
- canvas.drawColor(Color.WHITE);
- if (onSelect == true) {
- onSelect = false;
- _graphics.clear();
- }
- for (Path path : _graphics) {
- canvas.drawPath(path, mPaint);
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int hight) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- System.out.println(" I'M in surface created method method");
- _thread.setRunning(true);
- _thread.start();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- // canvas.drawPath(path, mPaint);
- boolean retry = true;
- _thread.setRunning(false);
- while (retry) {
- try {
- _thread.join();
- retry = false;
- System.out.println(" I'M in surface DESTROYED method");
- } catch (InterruptedException e) {
- // we will try it again and again...
- }
- }
- }
- }
- class DrawingThread extends Thread {
- private boolean _run = false;
- public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
- _surfaceHolder = surfaceHolder;
- _panel = panel;
- }
- public void setRunning(boolean run) {
- _run = run;
- }
- public SurfaceHolder getSurfaceHolder() {
- return _surfaceHolder;
- }
- @Override
- public void run() {
- //Point point = new Point();
- while (_run) {
- c = null;
- try {
- c = _surfaceHolder.lockCanvas(null);
- synchronized (_surfaceHolder) {
- _panel.onDraw(c);
- }
- }
- finally {
- // do this in a finally so that if an exception is thrown
- // during the above, we don't leave the Surface in an
- // inconsistent state
- if (c != null) {
- _surfaceHolder.unlockCanvasAndPost(c);
- }
- }
- }
- }
- }
- // TODO Auto-generated method stub
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu1, menu);
- return true;
- }
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.changeScreen:
- onSelect = true; ----->this means i clear the screen first then want to made changes
- //here i want to draw different to that canvas by calling this method
- changeScreen();
- break;
- }
- return true;
- }
- private void changeScreen()
- {
- Path path1=new Path();
- path1.addCircle(50, 60, 12,Path.Direction.CCW);
- canvas1.drawPath(path1, mPaint);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement