How to use Multi-touch in Android 2: Part 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. Begin by creating a new "Hello, Android" project with the following parameters in the New Android Project dialog box....
Written by Ed Burnette, Contributor

In part 2 of the Android multi-touch series, we start building the sample program introduced in part 1. Excerpted with permission from "Hello, Android! (3rd edition)". All source code can be downloaded from the book's web site.

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. See Part 1 for a screenshot of the finished product.

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.1 Application name: Touch Package name: org.example.touch Create Activity: Touch

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:

From 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:

From Touchv1/res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
android:layout_height="fill_parent" >
<ImageView android:id="@+id/imageView"
android:scaleType="matrix" >
The entire interface is a big ImageView control that covers the whole screen. The android:src="@drawable/butterfly" value refers to the butterfly image used in the example. You can use any JPG or PNG format image you like, just put it in the res/drawables-nodpi directory. The android:scaleType="matrix" attribute indicates we’re going to use a matrix to control the position and scale of the image. More on that later. The AndroidManifest.xml file is untouched except for the addition of the android:theme= attribute:

From Touchv1/AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionName="1.0" >
<application android:icon="@drawable/icon"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<activity android:name=".Touch"
android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="7" />
@android:style/Theme.NoTitleBar.Fullscreen, as the name suggests, tells Android to use the entire screen with no title bar or status bar at the top. You can run the application now and it will simply display the picture.

Continued in Part 3, Understanding touch events >

Copyright notice: This is an excerpt from Hello, Android 3rd edition, published by the Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, please visit http://www.pragprog.com/titles/eband3.

Copyright © 2010 The Pragmatic Programmers, LLC. All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher.

Editorial standards