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

Tweaking the Performance of An Android Drawing App

This post presents a simple drawing app for Android. However, there is a glitch. As it collects each single small paths while the finger is stroking on the canvas,

 @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());
 }

 // collect every time the method is called!!!!
 mGraphics.add(path);

 return true;
 }

the ArrayList<Path> grows quickly and the redraw loop takes longer and longer.

for (Path path : mGraphics) {
 canvas.drawPath(path, mPaint);
 }

To  solve this, we simply make two changes:

1) Update mGraphics at the very end of a stroke;

 @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());
  // not updating until now
  mGraphics.add(path);
 }

 // the line below has been moved
 // mGraphics.add(path);

 return true;
 }

2) Draw the changing path while stroking, as shown below

public void onDraw(Canvas canvas) {
// draw those updated and stored
  for (Path path : mGraphics)
  {
      canvas.drawPath(path, mPaint);
  }

// draw those that are not
 if(isStroking)
 {
     canvas.drawPath(path, paint);
 }
}

In this way, we avoid the excessive growth of the array of paths and streamline the redraw process.

Advertisements

About Xiang 'Anthony' Chen

Making an Impact in Your Life

Discussion

No comments yet.

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: