欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android GestureDetector手势滑动使用实例讲解

程序员文章站 2024-03-05 12:24:24
gesture在 viewgroup中使用 gesturedetector类可以让我们快速的处理手势事件,如点击,滑动等。 使用gesturedetector分三步:...

gesture在 viewgroup中使用

gesturedetector类可以让我们快速的处理手势事件,如点击,滑动等。
使用gesturedetector分三步:
1. 定义gesturedetector类
2. 初始化手势类,同时设置手势监听
3. 将touch事件交给gesture处理

先来了解一下如何使用,后面会有示例:

package com.example.y2222.myview;

import android.content.context;
import android.util.attributeset;
import android.util.log;
import android.view.gesturedetector;
import android.view.motionevent;
import android.widget.linearlayout;

/**
 * created by raise.yang on 2016/06/29.
 */
public class gesturedemoview extends linearlayout {
 //1,定义gesturedetector类
 private gesturedetector m_gesturedetector;

 public gesturedemoview(context context, attributeset attrs) {
  this(context, attrs, 0);
 }

 public gesturedemoview(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  //设置为可点击
  setclickable(true);
  //2,初始化手势类,同时设置手势监听
  m_gesturedetector = new gesturedetector(context, ongesturelistener);
  //双击监听-一般很少用到
  m_gesturedetector.setondoubletaplistener(ondoubletaplistener);
 }

 @override
 public boolean ontouchevent(motionevent event) {
  //3,将touch事件交给gesture处理
  m_gesturedetector.ontouchevent(event);
  return super.ontouchevent(event);
 }

 //初始化手势监听对象,使用gesturedetector.ongesturelistener的实现抽象类,因为实际开发中好多方法用不上
 private final gesturedetector.ongesturelistener ongesturelistener = new gesturedetector.simpleongesturelistener() {
  @override
  public boolean onsingletapup(motionevent e) {
   log.d("gesturedemoview", "onsingletapup() ");
   return super.onsingletapup(e);
  }

  @override
  public void onlongpress(motionevent e) {
   log.d("gesturedemoview", "onlongpress() ");
   super.onlongpress(e);
  }

  @override
  public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) {
   log.d("gesturedemoview", "onscroll() distancex = " + distancex);
   return super.onscroll(e1, e2, distancex, distancey);
  }

  @override
  public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {
   log.d("gesturedemoview", "onfling() velocityx = " + velocityx);
   return super.onfling(e1, e2, velocityx, velocityy);
  }

  @override
  public void onshowpress(motionevent e) {
   log.d("gesturedemoview", "onshowpress() ");
   super.onshowpress(e);
  }

  @override
  public boolean ondown(motionevent e) {
   log.d("gesturedemoview", "ondown() ");
   return super.ondown(e);
  }

  @override
  public boolean ondoubletap(motionevent e) {
   log.d("gesturedemoview", "ondoubletap() ");
   return super.ondoubletap(e);
  }

  @override
  public boolean ondoubletapevent(motionevent e) {
   log.d("gesturedemoview", "ondoubletapevent() ");
   return super.ondoubletapevent(e);
  }

  @override
  public boolean onsingletapconfirmed(motionevent e) {
   log.d("gesturedemoview", "onsingletapconfirmed() ");
   return super.onsingletapconfirmed(e);
  }

  @override
  public boolean oncontextclick(motionevent e) {
   log.d("gesturedemoview", "oncontextclick() ");
   return super.oncontextclick(e);
  }
 };
 private final gesturedetector.ondoubletaplistener ondoubletaplistener = new gesturedetector.ondoubletaplistener() {
  @override
  public boolean onsingletapconfirmed(motionevent e) {
   log.d("gesturedemoview", "onsingletapconfirmed() ondoubletaplistener");
   return false;
  }

  @override
  public boolean ondoubletap(motionevent e) {
   log.d("gesturedemoview", "ondoubletap() ondoubletaplistener");
   return false;
  }

  @override
  public boolean ondoubletapevent(motionevent e) {
   log.d("gesturedemoview", "ondoubletapevent() ondoubletaplistener");
   return false;
  }
 };

}

注意:setclickable(true);一定要加,不然只会收到下例3个事件,被这个整了好长时间才找到原因.(⊙﹏⊙)b

Android GestureDetector手势滑动使用实例讲解

对于单击,双击,拖动等事件调用见下图:

Android GestureDetector手势滑动使用实例讲解

根据上图,每个方法大致都调用了,说明几个容易弄混的回调方法
1. onscroll()
public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey)
e1:滑动事件的起点(也就是说ondown()的时候)
e2:当前滑动位置点(手指的位置)
distancex:上次滑动(调用onscroll)到这次滑动的x轴的距离px,不是e1点到e2点的x轴的距离
distancey:上次滑动(调用onscroll)到这次滑动的y轴的距离px,不是e1点到e2点的y轴的距离
2. onfling()
public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy)
e1:拖动动事件的起点(也就是说ondown()的时候)
e2:onfling()调用时,手指的位置
velocityx:x轴上每秒滑动像素值
velocityy:y轴上每秒滑动像素值
注意:当拖动速率velocityx或velocityy超过viewconfiguration.getminimumflingvelocity()最小拖动速率时,才会调用onfling(),也就是如果只拖动一点,或是慢慢的拖动,是不会触发该方法。
对应源码:

  if ((math.abs(velocityy) > mminimumflingvelocity)
      || (math.abs(velocityx) > mminimumflingvelocity)){
     handled = mlistener.onfling(mcurrentdownevent, ev, velocityx, velocityy);
    }

实践:使用gesturedetector实现左滑删除

在很多listview中都有该效果,现在自己实现下,顺便熟悉gesturedetector的使用。
效果图:

Android GestureDetector手势滑动使用实例讲解

gesturedemoview.java:

package com.example.y2222.myview;

import android.content.context;
import android.util.attributeset;
import android.util.log;
import android.view.gesturedetector;
import android.view.layoutinflater;
import android.view.motionevent;
import android.widget.linearlayout;

import com.example.y2222.myapplication.r;

/**
 * created by raise.yang on 2016/06/29.
 */
public class gesturedemoview extends linearlayout {
 //1,定义gesturedetector类
 private gesturedetector m_gesturedetector;

 private int m_max_scrollx;

 public gesturedemoview(context context, attributeset attrs) {
  this(context, attrs, 0);
 }

 public gesturedemoview(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  //设置为可点击
  setclickable(true);
  //2,初始化手势类,同时设置手势监听
  m_gesturedetector = new gesturedetector(context, ongesturelistener);

  layoutinflater.from(context).inflate(r.layout.view_gesture, this);
 }

 @override
 public boolean ontouchevent(motionevent event) {
  //3,将touch事件交给gesture处理
  m_gesturedetector.ontouchevent(event);
  if (event.getaction() == motionevent.action_up) {
   // gesturedetector没有处理up事件的方法,只能在这里处理了。
   int scrollx = getscrollx();
   if (scrollx > m_max_scrollx / 2) {
    show_right_view();
   } else {
    hide_right_view();
   }
  }
  return super.ontouchevent(event);
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  super.onmeasure(widthmeasurespec, heightmeasurespec);
  int childcount = getchildcount();
  for (int i = 0; i < childcount; i++) {
   //测量子view的宽高,?不测量,右侧布局会不显示,这里有点疑问
   measurechild(getchildat(i), widthmeasurespec, heightmeasurespec);
   if (i == 1) {
    m_max_scrollx = getchildat(i).getmeasuredwidth();
   }
  }
 }

 //初始化手势监听对象,使用gesturedetector.ongesturelistener的实现抽象类,因为实际开发中好多方法用不上
 private final gesturedetector.ongesturelistener ongesturelistener = new gesturedetector.simpleongesturelistener() {

  @override
  public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) {
   log.d("gesturedemoview", "onscroll() distancex = " + distancex + " getscrollx = " + getscrollx() + " max_scrollx = " + m_max_scrollx);
   int scrollx = getscrollx();
   int minscrollx = -scrollx;
   int maxscrolly = m_max_scrollx - scrollx;
   // 对滑动的距离边界控制
   if (distancex > maxscrolly) {
    distancex = maxscrolly;
   } else if (distancex < minscrollx) {
    distancex = minscrollx;
   }
   scrollby((int) distancex, 0);
   return true;
  }

  @override
  public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {
   log.d("gesturedemoview", "onfling() velocityx = " + velocityx);
   if (velocityx < 0) {
    //快速向左滑动
    show_right_view();
   } else {
    hide_right_view();
   }
   return super.onfling(e1, e2, velocityx, velocityy);
  }
 };

 private void show_right_view() {
  scrollto(m_max_scrollx, 0);
 }

 private void hide_right_view() {
  scrollto(0, 0);
 }

}

 view_gesture.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

 <textview
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="左侧布局"/>

 <linearlayout
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  >

  <button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="收藏"/>

  <button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="删除"/>
 </linearlayout>
</merge>

xml文件中根标签使用<merge>,可减少一层view树嵌套,并且使用getchildcount()能得到我们想要的子view个数。

关于<merge>标签的使用,详见郭神的blog:http://blog.csdn.net/guolin_blog/article/details/43376527 

实现也很简单,在scroll和fling的时候,得到滑动距离或滑动速度,再调用view自己的scrollto()或scrollby()滑动内部元素即可。
从效果图中,当滑动到一半松手时,立即滑动到最左边,完全没有动画,这样的体验很差,所以还需优化。关于滑动时增加动画效果,可以使用scroller类完成,准备下期补上。

gesture在 view中使用

和在viewgroup中一样,在view中,同样是经过三步来实现:
1. 定义gesturedetector类
2. 初始化手势类,同时设置手势监听
3. 将touch事件交给gesture处理
举个荔枝:
做了一个小球跟随手指移动的效果,先绘制小球,当手指放在小球上滑动时,会调用onscroll(),在这个方法中,修改圆心的位置进行重绘,这样小球就能移动了。
这里有2个难点:
1. 如何判断手指落在了小球上;
2. 滑动到边界时,不能超过边界;

效果图:

Android GestureDetector手势滑动使用实例讲解

gestureview.java代码:

package com.example.y2222.myview;

import android.content.context;
import android.graphics.canvas;
import android.graphics.paint;
import android.util.attributeset;
import android.view.gesturedetector;
import android.view.motionevent;
import android.view.view;

/**
 * created by raise.yang on 2016/07/05.
 */
public class gestureview extends view {

 private gesturedetector m_gesturedetector;
 private paint m_paint;
 //小球的中心点
 private float centerx;
 private float centery;
 //小球的半径
 private int radius;
 //是否touch在小球上
 private boolean touch_bool;

 public gestureview(context context, attributeset attrs) {
  this(context, attrs, 0);
 }

 public gestureview(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  // 初始画笔
  m_paint = new paint(paint.anti_alias_flag);
  m_paint.setcolor(getresources().getcolor(android.r.color.holo_blue_light));
  //设置为可点击
  setclickable(true);
  //2,初始化手势类,同时设置手势监听
  m_gesturedetector = new gesturedetector(context, ongesturelistener);
  radius = 50;
 }

 @override
 public boolean ontouchevent(motionevent event) {
  //3,将touch事件交给gesture处理
  m_gesturedetector.ontouchevent(event);
  if (event.getaction() == motionevent.action_down) {
   //判断手指落在了小球上
   if (getdistancebypoint((int) centerx, (int) centery, (int) event.getx(), (int) event.gety()) < radius) {
    touch_bool = true;
   } else {
    touch_bool = false;
   }
  }
  return super.ontouchevent(event);
 }

 @override
 protected void onsizechanged(int w, int h, int oldw, int oldh) {
  // 默认圆心在中心点
  if (w > 0) {
   centerx = w / 2;
  }
  if (h > 0) {
   centery = h / 2;
  }
 }

 @override
 protected void ondraw(canvas canvas) {
  canvas.drawcircle(centerx, centery, radius, m_paint);
 }

 gesturedetector.ongesturelistener ongesturelistener = new gesturedetector.simpleongesturelistener() {
  @override
  public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) {
   if (touch_bool) {
    centery -= distancey;
    centerx -= distancex;
    //处理边界问题
    if (centerx < radius) {
     centerx = radius;
    } else if (centerx > getwidth() - radius) {
     centerx = getwidth() - radius;
    }
    if (centery < radius) {
     centery = radius;
    } else if (centery > getheight() - radius) {
     centery = getheight() - radius;
    }
    //修改圆心后,通知重绘
    postinvalidate();
   }
   return true;
  }
 };

 /**
  * 计算两点间的距离
  */
 private int getdistancebypoint(int x1, int y1, int x2, int y2) {
  double temp = math.abs((x2 - x1) * (x2 - x1) - (y2 - y1) * (y2 - y1));
  return (int) math.sqrt(temp);
 }

}

在处理问题1时,我设置了一个boolean值,在用户触摸的时候去判断,当前点和圆心点的距离是否小于半径,若小于,说明在圆内。这样在滑动的时候,就去判断一下,是否需要滑动小球。
控制边界,其实就是控制圆心点的坐标,只要保证落在(radius,radius),(getwidth()-radius,getheight()-radius)两点矩形中即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。