1 / 17

Programming the Android Platform

Touch & Gestures. Programming the Android Platform. Topics. MotionEvents Touch Handling Gestures. MotionEvents. Represents a movement on the UI e.g., Pen, trackball, mouse, finger Records event’s time, action, pressure, location, source, etc. MotionEvents (cont.).

lise
Télécharger la présentation

Programming the Android Platform

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Touch & Gestures Programming the Android Platform

  2. Topics • MotionEvents • Touch Handling • Gestures

  3. MotionEvents • Represents a movement on the UI • e.g., Pen, trackball, mouse, finger • Records event’s time, action, pressure, location, source, etc.

  4. MotionEvents (cont.) • Some common MotionEvent actions • ACTION_DOWN • ACTION_MOVE • ACTION_UP • ACTION_POINTER_DOWN • ACTION_POINTER_UP

  5. Handling Touch Events • View.OnTouchListener defines touch event callback methods • View.OnTouchListener.onTouch() is called when user performs a touch event, such as pressing, releasing or dragging an item on the screen • onTouch() should return true if it has consumed the event

  6. Handling Touch Events (cont.) • 1-touch – each touch is a single event • Process ACTION_DOWN, ACTION_MOVE & ACTION_UP independently • 2+-touch – multiple touches combined to form a gesture • Identify & process combinations of touches such as ACTION_DOWN, followed by ACTION_POINTER_DOWN

  7. Two-touch Handling • Some useful MotionEvent methods • getPointerCount() • getActionIndex() • getActionMasked() • getX() • getY()

  8. Two-touch Handling (cont.)

  9. Two-touch Handling (cont.)

  10. Multi-touch Handling

  11. Touch Example public class IndicateTouchLocationActivity extends Activity { public void onCreate(BundlesavedInstanceState) { … final FrameLayout frame = … frame.setOnTouchListener(newOnTouchListener() { public booleanonTouch(Viewv, MotionEvent event) { intactionCode = event.getActionMasked(); for (inti = 0; i < event.getPointerCount(); i++) { switch (actionCode) { case /* interesting motion events */ : { // make a view showing touch location frame.addView(/* new view */); …

  12. Custom Gestures • GestureBuilder allows developers to create & save custom gestures • GestureLibraries supports loading custom gestures & recognizing then at runtime • GestureOverlayView intercepts user gestures and invokes Application code to handle them

  13. GestureBuilder • Comes bundled with emulator • Stores gestures to /mnt/sdcard/gestures • Copy this file to /res/raw directory

  14. GestureOverlayView • View intercepts gestures • Invokes OnGesturePerformedListener

  15. GestureActivity Example public class GesturesActivity extends Activity implements OnGesturePerformedListener { private GestureLibrarymLibrary; public void onCreate(BundlesavedInstanceState) { … mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); if (!mLibrary.load()) { finish(); } GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures); gestures.addOnGesturePerformedListener(this); … } …

  16. GestureActivity Example (cont.) … public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { ArrayList<Prediction> predictions = mLibrary.recognize(gesture); if (predictions.size() > 0) { Prediction prediction = predictions.get(0); if (prediction.score > 1.0) { Toast.makeText(this, prediction.name,…).show(); } } } }

  17. Lab

More Related