ie8 fix
madison

How to use Multi-touch in Android 2: Part 5, Implementing the Drag Gesture

By | March 12, 2010, 5:41am PST

Summary: This will probably get lost in all of today’s iPad coverage, but it’s time for another entry in the Android multi-touch series. In this part we fill in the touch event code to let us drag an image around on the screen. Next week we’ll tie everything together by implementing the multi-touch pinch zoom gesture.

This will probably get lost in all of today’s iPad coverage, but it’s time for another entry in the Android multi-touch series. In this part we fill in the touch event code to let us drag an image around on the screen. Next week we’ll tie everything together by implementing the multi-touch pinch zoom gesture. All source code can be downloaded from the web site for Hello, Android! (3rd edition).

Implementing the Drag Gesture

A drag gesture starts when the first finger is pressed to the screen (ACTION_DOWN) and ends when it is removed (ACTION_UP or ACTION_POINTER_UP).

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

switch (event.getAction() & MotionEvent.ACTION_MASK) {
   case MotionEvent.ACTION_DOWN:
      savedMatrix.set(matrix);
      start.set(event.getX(), event.getY());
      Log.d(TAG, "mode=DRAG" );
      mode = DRAG;
      break;
   case MotionEvent.ACTION_UP:
   case MotionEvent.ACTION_POINTER_UP:
      mode = NONE;
      Log.d(TAG, "mode=NONE" );
      break;
   case MotionEvent.ACTION_MOVE:
      if (mode == DRAG) {
         matrix.set(savedMatrix);
         matrix.postTranslate(event.getX() - start.x,
         event.getY() - start.y);
      }
      break;
}

When the gesture starts we remember the current value of the transformation matrix and the starting position of the pointer. Every time the finger moves, we start the transformation matrix over at its original value and call the postTranslate( ) method to add a translation vector, the difference between the current and starting positions.

If you run the program now you should be able to drag the image around the screen using your finger. Neat, huh?

Joe Asks: Why not use the built-in gesture library?

The android.gesture package provides a way to create, recognize, load, and save gestures. Unfortunately it is not very useful in practice. Among other problems, it doesn’t come with a standard collection of built-in gestures (like tap and drag) and it doesn’t support multi-touch gestures such as pinch zoom. Perhaps a future version of Android will include a better gesture library so the code in this chapter could be simplified.

Continued in Part 6, Implementing the Pinch Zoom 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.

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

Topics

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.

Disclosure

Ed Burnette

Ed Burnette is a Manager of Mobile Development at SAS. However the postings on this site are his own and do not represent the positions, strategies, or opinions of his employer.

Biography

Ed Burnette

Ed Burnette has been hooked on computers ever since he laid eyes on a TRS-80 in the local Radio Shack. Since graduating from NC State University he has programmed everything from serial device drivers and debuggers to web servers. After a delightful break working on commercial video games, Ed reluctantly returned to business software. He currently develops enterprise software for Android phones and tablets.

In his copious spare time, Ed writes and speaks about all kinds of technology and software. His most recent books include the Eclipse IDE Pocket Guide from O'Reilly and Hello, Android: Introducing Google's Mobile Development Platform from the Pragmatic Programmers.

10
Comments

Join the conversation!

Just In

what is start veriable in start.set(event.getX(), event.getY())
serefakyuz.com 16th Nov
please help me about this topic. it is a fault in my project. i followed all the instructions and did them. but i got a problem about "start"...
0 Votes
+ -
Something education on ZDNet?
davidr69 12th Mar 2010
Really appreciate this post. Just about every other post incites flame wars and nonsense that isn't worth my time reading.
0 Votes
+ -
Contributr
Thanks
Ed Burnette 12th Mar 2010
It's good to know somebody finds them useful. Unfortunately the educational posts get far fewer page views than the controversial ones.

Now if I could just learn to count. 1, 2, 3, 4, ... uh, 5 yeah. Reminds me of an old post:

http://blogs.zdnet.com/Burnette/?p=323

"Five is right out"
android.gesture is mostly to recognize shapes. To recognize
scrolls, flings, etc. you can use the GestureDetector. FroYo
will add a new detector class to recognize "pinch-zoom"
easily.
0 Votes
+ -
Contributr
Good to hear
Ed Burnette 12th Mar 2010
I'm glad this will be addressed in FroYo. By the way I need another technical reviewer for Hello, Android 3e. Send me an email if you can do it.
Hi Ed,

Great post. It's what I was looking for happy I've
implemented this code as saw that the image may be moved
only in its image view. Is there a way to move an image
among various views? Thanks.
hi there.
this is really a great post to learn to program and recognize the gesture.
however i got stuck when in part 5:
what is the start variable?
please advise.
thanks.
0 Votes
+ -
I know the answer
yestbag 8th Aug
@calvinlyp hi, I've bought the book and I'm sure that the author must have missed three declarations. And they are PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
which are just below the sentence "int mode = NONE;"
Hope that can help you?
Thank you for such a thoughtful and easy-to-understand explanation about multi-touch. Most of the other blogs are difficult, and condescending. I'm off to buy the book!
please help me about this topic. it is a fault in my project. i followed all the instructions and did them. but i got a problem about start...
please help me about this topic. it is a fault in my project. i followed all the instructions and did them. but i got a problem about "start"...

Join the conversation!

Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]
ie8 fix
Click Here
ie8 fix

The best of ZDNet, delivered

ZDNet Newsletters

Get the best of ZDNet delivered straight to your inbox

Facebook Activity

White Papers, Webcasts, & Resources
ie8 fix
ie8 fix