通過Gesture的監聽我們将實作一個,手指的快速滑動顯示坐标的變化,我們先來看一看效果圖:

1.Android中手勢互動的執行順序
- 1.手指觸碰螢幕時,觸發MotionEvent事件!
- 2.該事件被OnTouchListener監聽,可在它的onTouch()方法中獲得該MotionEvent對象!
- 3.通過GestureDetector轉發MotionEvent對象給OnGestureListener
- 4.我們可以通過OnGestureListener獲得該對象,然後擷取相關資訊,以及做相關處理!
這三個經常用到的類的作用:
- MotionEvent: 這個類用于封裝手勢、觸摸筆、軌迹球等等的動作事件。其内部封裝了兩個重要的屬性X和Y,這兩個屬性分别用于記錄橫軸和縱軸的坐标。
- GestureDetector: 識别各種手勢。
- OnGestureListener: 這是一個手勢互動的監聽接口,其中提供了多個抽象方法,并根據GestureDetector的手勢識别結果調用相對應的方法。
2.GestureListener詳解:
從1中我們知道了監聽手勢的關鍵是:GestureListener他給我們提供了下述的回調方法:
- 按下(OnDown): 剛剛手指接觸到觸摸屏的那一刹那,就是觸的那一下。
- 抛擲(OnFling): 手指在觸摸屏上迅速移動,并松開的動作。
- 長按(OnLongPress): 手指按在持續一段時間,并且沒有松開。
- 滾動(OnScroll): 手指在觸摸屏上滑動。
- 按住(OnShowPress): 手指按在觸摸屏上,它的時間範圍在按下起效,在長按之前。
- 擡起(OnSingleTapUp):手指離開觸摸屏的那一刹那。
上述資料的來源:http://www.runoob.com/w3cnote/android-tutorial-gestures.html
3.xamarin android gestues的簡單用法
編輯布局頁Main.axml 内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_show"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="随便一按,你的坐标變化是:"
android:textColor="#ff0000"
android:textSize="30dp"
/>
</LinearLayout>
編輯MainActivity.cs,使得它實作
Android.Views.GestureDetector.IOnGestureListener
并通過該接口所需要的方法。更多的功能将被添加到該
OnFling
方法進一步上。注意重寫了方法OnTouchEvent
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Debug = System.Diagnostics.Debug;
namespace Gestures
{
[Activity(Label = "GesturesDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity,GestureDetector.IOnGestureListener
{
private GestureDetector mDetector;
private GestureDetector _gestureDetector;
TextView tv_show = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
tv_show = FindViewById<TextView>(Resource.Id.tv_show);
tv_show.Text = "Fling Velocity,随便一劃,坐标變化為:";
mDetector = new GestureDetector(this);
}
public override bool OnTouchEvent(MotionEvent e)
{
mDetector.OnTouchEvent(e);
return false;
}
public bool OnDown(MotionEvent e)
{
//onDown 按下
Debug.WriteLine("onDown 按下");
return false;
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
//迅速滑動,并松開
Debug.Write($"OnFling随便一劃,坐标變化為X:{velocityX},Y:{velocityY}");
tv_show.Text = $"OnFling随便一劃,坐标變化為:X:{velocityX},Y:{velocityY}";
return false;
}
public void OnLongPress(MotionEvent e)
{
Debug.Write($"長按不放");
}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
//在螢幕上滑動
Debug.Write("OnScroll:在螢幕上滑動");
return false;
}
public void OnShowPress(MotionEvent e)
{
Debug.Write("OnShowPress:手指按下一段時間,還沒到長按");
//手指按下一段時間,不過還沒到長按
}
public bool OnSingleTapUp(MotionEvent e)
{
//手指離開螢幕的一瞬間
return false;
}
}
}
好了,在裝置上随手一劃,就會顯示坐标的變化。現在來看一下正常的手勢操作對應的執行的方法
1.手指按下立即松開:
2.随手一劃,化的越快OnScroll執行的次數就越少,迅速松開:
3.長按後松開:
4.按住後不放連續做滑動操作:這個在虛拟器和手機總感覺有點差别,自己試試吧
引用菜鳥上的一段總結:
PS:從上述結果來看,我們發現了一個問題:我們實作OnGestureListener需要實作所有的手勢,可能我針對的僅僅是滑動,但是你還是要去重載,這顯得很逗逼,是吧,官方肯定會給出解決方法滴,官方另外給我們提供了一個SimpleOnGestureListener類隻需把上述的OnGestureListener替換成SimpleOnGestureListener即可!