
- •Contents
- •Acknowledgments
- •Preface
- •What Makes Android Special?
- •Who Should Read This Book?
- •Online Resources
- •Fast-Forward >>
- •Introducing Android
- •Quick Start
- •Installing the Tools
- •Creating Your First Program
- •Running on the Emulator
- •Running on a Real Phone
- •Key Concepts
- •The Big Picture
- •Building Blocks
- •Using Resources
- •Safe and Secure
- •Android Basics
- •Designing the User Interface
- •Introducing the Sudoku Example
- •Designing by Declaration
- •Creating the Opening Screen
- •Using Alternate Resources
- •Implementing an About Box
- •Applying a Theme
- •Adding a Menu
- •Adding Settings
- •Starting a New Game
- •Debugging
- •Exiting the Game
- •Exploring 2D Graphics
- •Learning the Basics
- •Adding Graphics to Sudoku
- •Handling Input
- •The Rest of the Story
- •Making More Improvements
- •Multimedia
- •Playing Audio
- •Playing Video
- •Adding Sounds to Sudoku
- •Storing Local Data
- •Adding Options to Sudoku
- •Continuing an Old Game
- •Remembering the Current Position
- •Accessing the Internal File System
- •Accessing SD Cards
- •Beyond the Basics
- •The Connected World
- •Browsing by Intent
- •Web with a View
- •From JavaScript to Java and Back
- •Using Web Services
- •Locating and Sensing
- •Location, Location, Location
- •Set Sensors to Maximum
- •Putting SQL to Work
- •Introducing SQLite
- •Hello, Database
- •Data Binding
- •Using a ContentProvider
- •Implementing a ContentProvider
- •3D Graphics in OpenGL
- •Understanding 3D Graphics
- •Introducing OpenGL
- •Building an OpenGL Program
- •Rendering the Scene
- •Building a Model
- •Lights, Camera, ...
- •Action!
- •Applying Texture
- •Peekaboo
- •Measuring Smoothness
- •Fast-Forward >>
- •The Next Generation
- •Multi-Touch
- •Building the Touch Example
- •Understanding Touch Events
- •Setting Up for Image Transformation
- •Implementing the Drag Gesture
- •Implementing the Pinch Zoom Gesture
- •Hello, Widget
- •Live Wallpaper
- •Write Once, Test Everywhere
- •Gentlemen, Start Your Emulators
- •Building for Multiple Versions
- •Evolving with Android APIs
- •Bug on Parade
- •All Screens Great and Small
- •Installing on the SD Card
- •Publishing to the Android Market
- •Preparing
- •Signing
- •Publishing
- •Updating
- •Closing Thoughts
- •Appendixes
- •Bibliography
- •Index

Chapter 11
Multi-Touch
With each new version of Android, new features are added to the platform. In this part, we’ll concentrate on those newer features and on making your programs available to others.
In this chapter, we’ll learn how to use the new multi-touch features in Android 2.0, warts and all. Then we’ll cover home screen widgets, introduced in Android 1.6, and live wallpaper, introduced in Android 2.1. Android’s popularity and its rapid pace of development have created a problem with fragmentation, so there’s a whole chapter dedicated to dealing with all the different versions and screen sizes you’ll encounter in the field. Finally, there’s a chapter that discusses how to get your program into users’ hands by publishing it on the Android Market.
11.1Introducing Multi-Touch
Multi-touch is simply an extension of the regular touch-screen user interface, using two or more fingers instead of one. We’ve used singlefinger gestures before,1 although we didn’t call it that. Remember in Section 4.3, Entering Numbers, on page 89 when we let the user touch a tile in the Sudoku game in order to change it? That’s called a tap gesture. Another gesture is called drag. That’s where you hold one finger on the screen and move it around, causing the content under your finger to scroll.
Tap, drag, and a few other single-fingered gestures have always been supported in Android. However, because of the popularity of the Apple iPhone, early Android users suffered from a kind of gesture envy. The
1. Some people use them more than others.

INTRODUCING MULTI-TOUCH 221
Figure 11.1: Three common touch gestures: a) tap, b) drag, and c) pinch zoom
iPhone supported multi-touch, in particular the “pinch zoom” gesture (see Figure 11.1).
With pinch zoom, you place two fingers on the screen and squeeze them together to make the item you’re viewing smaller, or you pull them apart to make it bigger. Before Android 2.0, you had to use a clunky zoom control with icons that you pressed to zoom in and out (see the setBuiltInZoomControls( ) method in Section 8.3, Getting Ready, on page 175). But thanks to its new multi-touch support, you can now pinch zoom on Android too—as long as the application supports it, of course.
Note: Android 2.2 introduced a new class called ScaleGestureDetector that recognizes the pinch zoom gesture. However, I decided not to use it in order to be compatible with 2.0 and 2.1 devices. If you only need to target 2.2, see the online documentation for more information.2
2. http://d.android.com/reference/android/view/ScaleGestureDetector.html

BUILDING THE TOUCH EXAMPLE 222
Warning: Multi-bugs Ahead
Multi-touch, as implemented on current Android phones, is extremely buggy. In fact, it’s so buggy that it borders on the unusable. The API routinely reports invalid or impossible data points, especially during the transition from one finger to two fingers on the screen, and vice versa.
On the developer forums, you can find complaints of fingers getting swapped, x- and y-axes flipping, and multiple fingers sometimes being treated as one. Some of these problems can be traced back to hardware limitations in the touch-screen sensors used in certain phones, but many could be fixed or improved with software updates.
With a lot of trial and error, I was able to get the example in this chapter working because the gesture it implements is so simple. Until Google acknowledges and fixes the issues with multitouch, that may be about all you can do. Luckily, pinch zoom seems to be the only multi-touch gesture most people want.
If you try to run the example in this chapter on Android 1.5 or 1.6, it will crash because those versions do not support multi-touch. We’ll learn how to work around that in Section 13.3, Evolving with Android APIs, on page 259.
11.2 Building the Touch Example
To demonstrate multi-touch, we’re going to build a simple image viewer application that lets you zoom in and scroll around an image. The finished product is shown in Figure 11.2, on the following page.
Begin by creating a new “Hello, Android” project with the following parameters in the New Android Project dialog box:
Project name: Touch
Build Target: Android 2.2
Application name: Touch
Package name: org.example.touch
Create Activity: Touch
Min SDK Version: 8

BUILDING THE TOUCH EXAMPLE 223
Figure 11.2: The touch example implements a simple image viewer with drag-and-pinch zoom.

BUILDING THE TOUCH EXAMPLE 224
This will create Touch.java to contain your main activity. Let’s edit it to show a sample image, put in a touch listener, and add a few imports we’ll need later:
Download Touchv1/src/org/example/touch/Touch.java
package org.example.touch;
import android.app.Activity; import android.graphics.Matrix; import android.graphics.PointF; import android.os.Bundle; import android.util.FloatMath; import android.util.Log;
import android.view.MotionEvent; import android.view.View;
import android.view.View.OnTouchListener; import android.widget.ImageView;
public class Touch extends Activity implements OnTouchListener { private static final String TAG = "Touch";
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
ImageView view = (ImageView) findViewById(R.id.imageView); view.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) { // Handle touch events here...
}
}
We’ll fill out that onTouch( ) method in a moment. First we need to define the layout for our activity:
Download Touchv1/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/butterfly" android:scaleType="matrix">
</ImageView>
</FrameLayout>