Tutorial details

Android beginner tutorial: Part 87 - 2D Graphics in Android | App Code for Sale | Preview

Android beginner tutorial: Part 87 - 2D Graphics in Android | Android Tutorial

Drawing graphics in Android

Overview PAGE TOP

In this part we will talk about drawing graphics in Android.

The Android SDK provides a set of drawing APIs for rendering 2D graphics. There are two ways to draw in Android: directly into a View object, or into a Canvas.

Drawing inside a View object means simply giving the View a picture to display from your layout. The drawing of the graphics is done using the system's normal View hierarchy drawing process. This method is perfect for drawing simple graphics that aren't changed dynamically in real time and shouldn't be used in performance intentsive video games, but can be used to display an animation in a static application.

Drawing to a Canvas means using the appropriate class' onDraw() callback method (with your Canvas object passed as the parameter) or using one of the Canvas draw..() methods (for example, drawPicture() or drawBitmap()). This method is used when the application redraws the graphics regularly (for example, in a video game). But there are more than one ways to draw using the Canvas.

The first option is to handle the drawing in the same thread as the main Activity of your application, calling invalidate() each time the graphics need to be redrawn and handling the onDraw() callback function.

The second option is to draw in a separate thread, where you can manage a SurfaceView object and draw graphics in the Canvas as fast as it is possible in that thread.

Android SDK provides a library for drawing 2D shapes and images - Drawables. The Drawable class is extended by multiple subclasses, such as BitmapDrawable, NinePatchDrawable, PictureDrawable, ShapeDrawable, etc.

There are 3 ways to instantiate a Drawable object - using an image that is stored in res/drawable/ directory (which we already did in some of the previous tutorials), using an XML file, which contains properties of the Drawable object,or using the normal class constructors.

The easiest way to add graphics to an application is to use an external image stored in the resources. The supported file formats are PNG, JPEG and GIF, where PNG is the most preferable one and GIF is the least preferable.

Using images from the resources is suitable for displaying icons, images and other graphics in an application.

All the image resources located in res/drawable/ are optimized using aapt utility during compilation of the project. The quality of the pictures remain the game, but the file size is less. If you need to load bitmap images without compressing them, store them in res/raw/ directory, where they won't be reached by aapt. In that case you'll have to manually load and read the image data and display it, though.

That is all for today. Next time we'll take a closer look at Drawables.

Thanks for reading!

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.