Xamarin Android Gestures详解

通过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即可!


posted @ 2016-09-28 14:21  张林-布莱恩特  阅读(345)  评论(0编辑  收藏  举报
博客统计by flagcounter: