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

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

Summary: 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...

SHARE:
TOPICS: Hardware
2

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.

Topic: Hardware

Ed Burnette

About Ed Burnette

Ed Burnette is a software industry veteran with more than 25 years of experience as a programmer, author, and speaker. He has written numerous technical articles and books, most recently "Hello, Android: Introducing Google's Mobile Development Platform" from the Pragmatic Programmers.

Kick off your day with ZDNet's daily email newsletter. It's the freshest tech news and opinion, served hot. Get it.

Talkback

2 comments
Log in or register to join the discussion
  • Gosling is still a douche bag

    All these years and still no delegates? How
    laughable that java still needs interfaces to
    implement the listener pattern. What a horrific
    poorly conceived language.
    jackbond
  • Actual size of image changed!!

    Hi Ed Burnette,
    Unfortunately when I use this line:

    view.setImageMatrix(matrix);

    The size of image was change when I pressing only on image ??? why
    Try use this command:
    view.getWidth();
    You should see the width of actual size of image was change :(
    Belal Shakhatrah