Working with Images in Google's Android
Putting All the Pieces Together
From having read previous sections, you know how to import and export an image file, create a view for it, and process each pixel in the image. One thing you want to add is to provide a simple user interaction so that a sequence of events can happen in response to the user's input. The APIs in android.view.KeyEvent allow you to handle the key presses. The software is set to wait for the key press event of the direction pad's center key. When it is pressed, it will tint the picture by an angle of 20 degrees and save the result into a file.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// Perform the tinting operation
TintThePicture(20);
// Display a short message on screen
nm.notifyWithText(56789, "Picture was tinted",
NotificationManager.LENGTH_SHORT, null);
// Save the result
SaveThePicture();
return (true);
}
return super.onKeyDown(keyCode, event);
}
Some Advice Before Moving In
- If it takes too long to process all the image pixels (approximately five seconds), you could get a popup dialog called Application Not Responding, or ANR. You should create a child thread, and do the complicated calculation there. That should keep the main thread running without interruption.
- If the child thread needs to change the main view (for example, update the image), you should use a message handler in the main thread to listen for the notification from the child thread and then update the view accordingly. A child thread cannot modify the view in the main thread directly.
- Some enhancements can be made. For example, it can be modified to read the image file from browsing the folders or from a URL. Image animation can be done carefully through multi-threading and message handling along with rectangle clipping to produce smooth transition effects.
Conclusion
Hopefully, the information provided here has given you a rough idea how Android works. I hope you come up with your own application for the challenge as well.
References
- Download and save the entire software project
- Android: An Open Handset Alliance Project at http://code.google.com/android/
- Android Development Community at http://www.anddev.org
- Androidlet at http://www.androidlet.com
- Rafael C. Gonzalez and Richard E. Woods, Digital Image Processing, Addison-Wesley Publishing, 1994
About the Author
Chunyen Liu has been with the engineering department at a world's leading GPS company for a while. Some of his applications were among winners at programming contests administered by SUN, ACM, and IBM. He also had co-authored U.S. patents and written articles for various publishers. He holds advanced degrees in computer science and operates a hobby site called The J Maker. On the non-technical side, he is a tournament-rated table tennis player, certified umpire, and certified coach of USA Table Tennis.
