X
Tech

How to use Multi-touch in Android 2: Part 4, Setting up for Image Transformation

Today's entry in the Android multi-touch series is a short one. In order to move and zoom the image we’ll use a neat little feature on the ImageView class called matrix transformation. Using a matrix we can represent any kind of translation, rotation, or skew that we want to do to the image...
Written by Ed Burnette, Contributor

Today's entry in the Android multi-touch series is a short one. In it, we set up the matrices that will be used later for moving and resizing the image. All source code can be downloaded from the web site for Hello, Android! (3rd edition).

The Android multi-touch series:

  1. Introducing Multi-Touch
  2. Building the Touch Example
  3. Understanding Touch Events
  4. Setting up for Image Transformation
  5. Implementing the Drag Gesture
  6. Implementing the Pinch Zoom Gesture

Setting up for Image Transformation

In order to move and zoom the image we’ll use a neat little feature on the ImageView class called matrix transformation. Using a matrix we can represent any kind of translation, rotation, or skew that we want to do to the image. We already turned it on by specifying android:scaleType="matrix" in the res/layout/main.xml file. In the Touch class, we need to declare two matrices as fields (one for the current value and one for the original value before the transformation). We’ll use them in the onTouch( ) method to transform the image. We also need a mode variable to tell whether we’re in the middle of a drag or zoom gesture:

From Touchv1/src/org/example/touch/Touch.java:

public class Touch extends Activity implements OnTouchListener {
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

// We can be in one of these 3 states static final int NONE = 0; static final int DRAG = 1; static final int ZOOM = 2; int mode = NONE;

@Override public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v;

// Dump touch event to log dumpEvent(event);

// Handle touch events here... switch (event.getAction() & MotionEvent.ACTION_MASK) { }

// Perform the transformation view.setImageMatrix(matrix);

return true; // indicate event was handled } } The matrix variable will be calculated inside the switch statement when we implement the gestures.

Continue reading Part 5, Implementing the Drag Gesture >

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