http://www.developer.com/ws/android/devices/enable-hardware-acceleration-in-your-android-3.0-apps.html

Back to article

Enable Hardware Acceleration in Your Android 3.0 Apps


July 11, 2011

Android 3.0 (Honeycomb) boasts a new, improved OpenGL rendering pipeline for common 2D graphics operations that Android developers can harness easily from within their applications. But what types of applications will benefit from this new feature? How is it used? What are some of the problems one might encounter?

Just about every application draws on the screen in some form or another. Whether you're using standard views or custom drawing, 2D hardware acceleration can improve your application under almost all circumstances. We'll talk more about some of the problematic situations in a moment.

How Do I Enable Android 3.0 Hardware Acceleration?

Enabling your application to take advantage of the hardware acceleration available on devices running Android 3.0+ can be as easy as setting a flag in your application Manifest file and updating your application and you can fine-tune your acceleration needs at the application, activity, window, or view level. This means that you'll need to recompile your application in order to take advantage of the acceleration features.

First, you'll want to enable acceleration within your application by way of the application manifest file. This is as simple as adding and enabling the new attribute called android:hardwareAccelerated within your application tag. If your app uses only standard views, then you're good to go. Most operations involving Camera, Canvas, ColorFilter, Paint, Shader, and Xfermode will be accelerated automatically.

How Do I Fine-Tune Android 3.0 Hardware Acceleration?

If you've got custom drawing operations or work with Canvas and Paints in your app, you should pay attention to what features are available in Android hardware acceleration at this time. Certain Canvas and Paint operations are not currently supported, while others behave differently depending using hardware versus software acceleration. Test your app thoroughly, and if you run into problems, you've got a couple of options:

  • Work around the problem by re-implementing your drawing code using supported functionality.
  • Turn off hardware acceleration on that activity, window, or specific view control, and rely upon the default software acceleration you were using prior.

Which Graphics Operations Does Android 3.0 Hardware Acceleration Support?

The Android documentation does not list the specific operations supported or not supported by Honeycomb's hardware acceleration improvements. Instead, it simply recommends testing your application thoroughly and modifying your app as necessary. However, Googler Romain Guy gives a helpful list of methods that fall into these two categories in this Android Developer's Blog post.

How Do I Turn Android 3.0 Hardware Acceleration On and Off?

You can turn hardware acceleration on by setting the android:hardwareAccelerated attribute to true in your Android manifest file. If you want your entire app accelerated, you set this attribute in the application tag. If you want only specific activities to be accelerated, simply turn off acceleration for specific activities by setting the same attribute android:hardwareAccelerated at the activity level to false.

For finer-grained control, you can also enable hardware acceleration at the window level by setting the FLAG_HARDWARE_ACCELERATED parameter. See the Android SDK documentation for details, including sample code.

Finally, you can toggle hardware and software acceleration at the View level using the setLayerType() method. See the Android SDK documentation for details on the setLayerType() method.

Is Android 3.0 Hardware Acceleration Really That Easy?

Well, yes and no. You still need to follow best practices for application design and in some cases, your application may need to be modified to really take advantage of the GPU. For example, keep your screens simple and limit the number of View controls, amongst others. Again, see Romain Guy's post for some do's and don'ts for writing GPU-friendly apps.

Conclusion

As you upgrade your applications to 3.0 and beyond, there's little reason not to turn on hardware acceleration for a smoother, more responsive experience for your users. Enabling your application to take advantage of the hardware acceleration available on devices running Android 3.0+ can be as easy as setting a flag in your application Manifest file and updating your application. You can also fine-tune your application graphics acceleration at the application, activity, or view level. As more users migrate to these newer versions of the Android SDK, this simply change to your app can help differentiate your from your competitors and please your users.

About the Authors

Shane Conder Shane Conder and Lauren Darcey--Contributing Editors, Mobile Development--have coauthored two books on Android development: an in-depth programming book entitled Android Wireless Application Development (ISBN-13: 978-0-321-62709-4) and Sams Teach Yourself Android Application Development in 24 Hours (ISBN-13: 978-0-321-67335-0). When not writing, they spend their time developing mobile software at their company and providing consulting services.

Email | Blog | Twitter

Lauren Darcey

Sitemap | Contact Us