Statically Typed

because Hindley-Milner rocks

Touch and Drag in Android (cont’)


Last time I was talking about a bug I had with a very tiny application I had written.  I still have not resolved the issue but I did stumble upon a very interesting effect.  I made previously draw icons fade into the background!

I know what you’re thinking, “So what?”  However, I never intended for this to happen.  It’s like I’m programming for the first time in my life and I’m reliving all those wonderful moments when you sat there saying to yourself, “I made this happen.  I’m the man.  I’m awesome.”  Better yet, that phrase isn’t being followed by, “What time is it?  I need to go to bed.  I need some sleep.  I can finish this assignment in the morning.  Crap, it is morning.”

What I wanted to happen was a wash to the canvas so that all the pixels were set jet black like the background.  Following the advice of the Android docs on Graphics:

On each pass you retrieve the Canvas from the SurfaceHolder, the previous state of the Canvas will be retained. In order to properly animate your graphics, you must re-paint the entire surface. For example, you can clear the previous state of the Canvas by filling in a color with drawColor() or setting a background image with drawBitmap(). Otherwise, you will see traces of the drawings you previously performed.

But if you’re going to go down the drawColor road the choice of color evidently matters.  So without adieu here is the small snippet that caused the thing to happen:

    public void draw(float left, float top){
	Canvas canvas = null;
	try{
		canvas = mSurfaceHolder.lockCanvas();
		if( canvas != null ){
			canvas.drawColor(color.background_dark);
			canvas.drawBitmap(mBitmap, left, top, null);
		}
		else{
			Log.i("BITMAP", "Canvas null valued");
		}
	}
	finally{
		if( canvas != null ){
			mSurfaceHolder.unlockCanvasAndPost(canvas);
		}
	}
}

and here’s what it looks like:

Neat, no?

One more thing, finally found a decent unit testing framework for testing screen actions/drawing actions: Robotium.  That’ll be another post (potentially praising all the fine work the people have done.)

Advertisements

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

Information

This entry was posted on December 13, 2010 by in Android, Java, Unit Testing.
%d bloggers like this: