Recipe Listening for Fling Gestures

As discussed in the beginning of the chapter, each view has an onTouchEvent() method associated with it. In this recipe, it is overridden with a gesture detector that sets a gesture listener.The possible gestures in the OnGestureListener interface are

■ onDown()—Notifies when a tap down event occurs

■ onFling()—Notifies when a tap down, movement, and matching up event occurs

■ onLongPress()—Notifies when a long press occurs

■ onScroll()—Notifies when a scroll occurs

■ onShowPress()—Notifies when a tap down occurs before any movement or release

■ onSingleTapUp()—Notifies when a tap up event occurs

When only a subset of gestures are needed, the SimpleOnGestureListener class can be extended instead. It returns false for any of the previous methods not explicitly implemented.

A fling consists of two events: a touch down (the first MotionEvent) and a release (the second MotionEvent). Each motion event has a specified location on the screen given by an (x,y) coordinate pair, where x is the horizontal axis and y is the vertical axis.The (x,y) velocity of the event is also provided.

Listing 5.11 shows an activity that implements the onFling() method.When the movement is large enough (here, defined as 60 pixels), the event is consumed and appends the statement describing the event to the screen.

Listing 5.11 src/com/cookbook/fling_ex/FlingExample.java package com.cookbook.fling_ex;

import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent;

import android.view.GestureDetector.SimpleOnGestureListener;

import android.widget.TextView;

public class FlingExample extends Activity { private static final int LARGE_MOVE = 60; private GestureDetector gestureDetector;

TextView tv;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

tv = (TextView) findViewById(R.id.text_result);

gestureDetector = new GestureDetector(this, new SimpleOnGestureListener() { @Override public boolean onFling(MotionEvent el, MotionEvent e2, float velocityX, float velocityY) {

tv.append("\nFling Up with velocity " + velocityY); return true;

} else if (e2.getY() - e1.getY() > LARGE_MOVE) {

tv.append("\nFling Down with velocity " + velocityY); return true;

} else if (e1.getX() - e2.getX() > LARGE_MOVE) {

tv.append("\nFling Left with velocity " + velocityX); return true;

} else if (e2.getX() - e1.getX() > LARGE_MOVE) {

tv.append("\nFling Right with velocity " + velocityX); return true;

@Override public boolean onTouchEvent(MotionEvent event) { return gestureDetector.onTouchEvent(event);

The TextView that contains the descriptive text in the previous activity is defined in the main XML layout shown in Listing 5.12.

Listing 5.12 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView android:id="@+id/text_result"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:textSize="16sp"

android:text="Fling right, left, up, or down\n" />

</LinearLayout>

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment