Thứ Tư, 18 tháng 4, 2012

Android Guts: Intro to Loopers and Handlers

One of the reasons I love Android API is because it contains so many useful little things. Many of them are not even specific to the platform and I really miss them in Java SE. Today I’d like to give a short introduction to two nice classes – Looper and Handler. They are used in Android UI internally, and available to us developers so we can do some cool things with their help.
So what can you do with Loopers and Handlers? Basically, they implement a common concurrency pattern that I call the Pipeline Thread. Here’s how it works:
  • The Pipeline Thread holds a queue of tasks which are just some units of work that can be executed or processed.
  • Other threads can safely push new tasks into the Pipeline Thread’s queue at any time.
  • The Pipeline Thread processes the queued tasks one after another. If there are no tasks queued, it blocks until a task appears in the queue.
  • Sometimes tasks can called messages and other names.

Android Beginners: From Bare Windows to Your First App

Want to develop Android apps but don’t know where to start?
Follow these steps and we will build a complete Android development environment from scratch, starting on a fresh Windows installation. The whole process should not take more than 20-30 minutes, provided that you have a good internet connection.
I need to mention that we will not develop a full-blown app here. The focus is on setting up the environment so we will develop the simplest app possible.
Here’s what we start with: a nice fresh XP installation. I intentionally left everything intact, including the ugly wallpaper and Internet Explorer. (Note: all screenshots are clickable.)

Android Beginners: NDK Setup Step by Step

Most Android developers start their learning from pure Java, Android SDK based apps. While everyone is aware that there is the NDK (the Native Development Kit), they don’t face the need to use it. Since NDK is distributed separately from the SDK, including documentations and samples, you are not likely to get familiar with NDK before you actually try it as a solution to one of your development challenges.
Because of that, many people think of NDK as of “black magic” of Android development. Many developers who are not familiar with NDK think it is 1) complex to understand and use, and at the same time a lot of developers will think it is a sort of a 2) silver bullet that can solve any problem that can’t be solved with SDK.
Well, both opinions are rather wrong, as I hope to show further in this post. Although it does have a maintenance cost and does add technical complexity to your project, NDK is not difficult to install or use in your project. And, while there are cases where NDK will be really helpful for your app, NDK has a rather limited API that’s mostly focused on several performance-critical areas, such as:
  • OpenGL, including support for some newer versions that the (Java) SDK supports
  • Math (some, but not all, calculation-intensive algorithms might benefit from being done on the native layer)
  • 2D graphics – pixelbuffer support (only starting with 2.2)
  • libc – it’s there for compatibility and perhaps to allow you to port existing native code
In this tutorial we will take our basic Android development environment that we created in one of the previous articles and add NDK support to it. We will also create a basic skeleton project that uses NDK that you can use as the foundation for your NDK-powered apps.
The downloads that are necessary for the initial configuration of the environment might take some time (around 30 minutes total), so be prepared.
Ready? Let’s go!

Play an MP3 file on an AudioTrack

Introduction
I really suggest that you read the article about playing a WAV first. It contains some general 101 on digital audio that you need to understand what PCM is and how decoders are used.
While WAV files that we loaded in the previous article contained raw PCM data (that we might need to only trivially preprocess), MP3s contain audio data that is encoded with a complex algorithm that we don’t want to learn or implement. Thus, we need some third-party code that will allow us to convert MP3 data to raw PCM that is playable by AudioTrack.
There are two ways we can go – find a pure Java MP3 decoder or try compile a native MP3 decoder on Android. In this article we will only discuss the pure Java solution in detail. It is fast enough for many purposes, although I have to admit that I use a native solution in my production code (but I still did try the Java one and it worked well – it was just not fast enough for my use case that was very demanding in terms of performance).
Now, let’s see if we can find a good pure Java library and how we use it to play some MP3s.

Thứ Hai, 16 tháng 4, 2012

OpenGL with Live wallpaper on Android

GLWallpaperService - Version 0.9.2
==================================
http://groups.google.com/group/glwallpaperservice

A library for making OpenGL Live Wallpapers for Android.

Provided as free open source software under the Apache License version 2.0.
Parts of this software are derived from code provided by the Android Open Source Project.

Getting started making your wallpaper

Android UI: Making a Live Wallpaper (Fire Simulation)

In the previous article we used 2D graphics via the Android Canvas to draw a custom UI widget. The Canvas approach is very useful when you need to draw relatively static graphics or when there is not too much complex animation. That is the typical case for UI widgets.
In this article we will develop a live wallpaper that displays a fire simulation that you might remember from the good old days of demo scene and DOS. Since we need very fast graphics, we will use OpenGL as the rendering engine. Although OpenGL is typically used for 3D graphics, it can also be used for hardware-accelerated 2D graphics, such as in this case.

GLSurfaceView adapted for 3D Live Wallpapers

I've been using GLSurfaceView since it was introduced in Android 1.5 and I was a little let down to find that the new Live Wallpaper APIs didn't include anything like that. I like the design because it makes it very easy to quickly start working in OpenGL. Without it, there is quite a bit of tedious initialization and thread management code that isn't necessary for the vast majority of apps. Fortunately, for my first live wallpaper (Live Waterpaper), I adapted the GLSurfaceView's code and created a GLWallpaperService with a GLEngine which takes a Renderer and does the job for me.
This is very similar in design to how things work with GLSurfaceView, except the GLWallpaperService does nothing more than create Engines and I have to move around a few of the fields for the GLThread management and configurations. Any event handling code you have should go into your GLEngine subclass and can be passed directly to your Renderer, which can be an exact port of an existing Renderer, with the exception of specifying the different package name on the interface.

Chủ Nhật, 15 tháng 4, 2012

Creating Live Wallpapers on Android

Android has a number of personalization features to help users customize many aspects of their device user experience. One of these features is live wallpaper. Live wallpapers don’t remain as static background images but instead have interactive features. Learn how to create a live wallpaper in this tutorial!
A live wallpaper, on Android, is normally used as a background on the home screen that animates or changes over time in some way. If you have an Android device, you’ve probably seen a couple of the built-in live wallpapers, like the one where leaves appear to fall into rippling water.