//
you're reading...
HCI, Tech

Writing A Drawing App for Android

Thanks to these two posts:

http://www.tutorialforandroid.com/2009/06/drawing-with-canvas-in-android.html

http://www.droidnova.com/playing-with-graphics-in-android-part-iv,182.html

I just copied and pasted their code, fixed some glitches, and had my first drawing app on Android.

The basic idea is to maintain a SurfaceView wherein you can update drawing paths from touches, and draw them to its canvas.

Most of the code below comes from that two posts:


package me.xiangchen.basicstuff;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class BasicSketchActivity extends Activity {

// Paint defines the drawn strokes' properties
 private Paint mPaint;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(new SketchView(this));

 mPaint = new Paint();
 mPaint.setDither(true);
 mPaint.setColor(0xFFFFFFFF);
 mPaint.setStyle(Paint.Style.STROKE);
 mPaint.setStrokeJoin(Paint.Join.ROUND);
 mPaint.setStrokeCap(Paint.Cap.ROUND);
 mPaint.setStrokeWidth(3);
 }

 private class SketchView extends SurfaceView implements SurfaceHolder.Callback {

private SketchThread mThread;
 private Path path;
 private ArrayList<Path> mGraphics = new ArrayList<Path>();

 public SketchView (Context context){
 super(context);

 //access to the underlying surface
 getHolder().addCallback(this);
 mThread = new SketchThread(getHolder(), this);
 setFocusable(true);
 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
 int height) {
 // TODO Auto-generated method stub

 }

@Override
 public void surfaceCreated(SurfaceHolder holder) {
 // TODO Auto-generated method stub
 mThread.setRunning(true);
 mThread.start();
 }

@Override
 public void surfaceDestroyed(SurfaceHolder holder) {
 // TODO Auto-generated method stub
 boolean retry = true;
 mThread.setRunning(false);
 while(retry)
 {
 try{
 mThread.join();
 retry = false;
 } catch (InterruptedException e){

 }
 }
 }

 @Override
 public void onDraw(Canvas canvas) {
 // take the paint to draw all the (small) paths in the array
 for (Path path : mGraphics) {
 canvas.drawPath(path, mPaint);
 }
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
 synchronized (mThread.getSurfaceHolder()) {
 if(event.getAction() == MotionEvent.ACTION_DOWN){
 path = new Path();
 // update the starting point of the new path
 path.moveTo(event.getX(), event.getY());
 path.lineTo(event.getX(), event.getY());
 }
 else if(event.getAction() == MotionEvent.ACTION_MOVE){
 // draw to the new point
 path.lineTo(event.getX(), event.getY());
 }
 else if(event.getAction() == MotionEvent.ACTION_UP){
 // last drawing
 path.lineTo(event.getX(), event.getY());
 }
 mGraphics.add(path);
 return true;
 }
 }

 }

 class SketchThread extends Thread {
 private SurfaceHolder mSurfaceHolder;
 private SketchView mSketchView;
 private boolean mRun = false;

 public SketchThread(SurfaceHolder surfaceHolder, SketchView sketchView) {
 mSurfaceHolder = surfaceHolder;
 mSketchView = sketchView;
 }

 public void setRunning(boolean run) {
 mRun = run;
 }

 public SurfaceHolder getSurfaceHolder() {
 return mSurfaceHolder;
 }

 @Override
 public void run() {
 Canvas c;
 while (mRun) {
 c = null;
 try {
 // obtain the canvas to draw
 c = mSurfaceHolder.lockCanvas(null);
 synchronized (mSurfaceHolder) {
 mSketchView.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) {
 // post the drawn canvas
 mSurfaceHolder.unlockCanvasAndPost(c);
 }
 }
 }
 }
 }

}
Advertisements

About Xiang 'Anthony' Chen

Making an Impact in Your Life

Discussion

Trackbacks/Pingbacks

  1. Pingback: Tweaking the Performance of An Android Drawing App « A Designer with Dirty Hands - December 6, 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Twitter Updates

%d bloggers like this: