id | title | brief | sdk | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
5D7E958B-287E-5D68-9BDD-E94E4B2FAFFB |
Create a Gesture Listener |
This recipe will show how to recognize a fling gesture. A fling gesture is when the user presses on the screen, and while maintaining contact with the screen moves their finger in a given direction. |
|
-
Create a new Xamarin.Android application named
RecognizeGesture
. -
Edit
Main.axml
so that its contents match the following:
<?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="wrap_content">
<TextView
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/velocity_text_view" />
</LinearLayout>
- Edit MainActivity.cs, and add the following instance variable to the class:
private GestureDetector _gestureDetector;
- Edit
MainActivity.cs
so that it implementsAndroid.Views.GestureDetector.IOnGestureListener
and the methods required by that interface. More functionality will be added to theOnFling
method further on.
public class Activity1 : Activity, GestureDetector.IOnGestureListener
{
public bool OnDown(MotionEvent e)
{
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
}
public void OnLongPress(MotionEvent e) {}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
}
public void OnShowPress(MotionEvent e) {}
public bool OnSingleTapUp(MotionEvent e)
{
return false;
}
}
- Edit the
OnFling
method so that it will display some values captured when the user "flings" on the screen:
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
_textView.Text = String.Format("Fling velocity: {0} x {1}", velocityX, velocityY);
return true;
}
- Create an instance of
GestureDetector
for theActivity
. ModifyOnCreate
as shown in the following snippet:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
_textView = FindViewById<TextView>(Resource.Id.velocity_text_view);
_textView.Text = "Fling Velocity: ";
_gestureDetector = new GestureDetector(this);
}
- Override the method
OnTouchEvent
in theActivity
. This will delegate the handling of the gesture to theGestureDetector.IOnGestureListener
methods implemented by the class.
public override bool OnTouchEvent(MotionEvent e)
{
_gestureDetector.OnTouchEvent(e);
return false;
}
- Run the application on a device, and fling your finger across the screen. You will see the X and Y velocities change:
Android provides the GestureDetector
class to
simplify the detection and handling of gestures on a View.
GestureDetectore.IOnGestureListener
is not the only interface for
gesture detection, there is also GestureDetector.IOnDoubleTapListener
and GestureDetector.SimpleOnGestureListener
.