要实现滑屏等触发事件,视情况而定;

如果实现的触屏或者手势效果较多,则使用第一种方法,实现OnGestureListener 接口(参考OnGestureListener);

如果只是实现较少的效果,则考虑使用SimpleOnGestureListener(参考SimpleOnGestureListenter);主要分为以下三步:

第一种方法:

1、继承OnTouchListener , OnGestureListener ;

//继承OnTouchListener,OnGestureListener
public class MainActivity extends Activity implements OnTouchListener,OnGestureListener{

:此处使用的OnGestureListener来自包 android.view.GestureDetector.OnGestureListener;而不是 android.gesture.GestureOverlayView.OnGestureListener;

2、初始化OnGestureListener ,即注册手势监听 ;获得当前接受操作的布局id ; 注册触屏监听;

//手势监听
mGestureDetector = new GestureDetector(MainActivity.this,(OnGestureListener) this);//(OnGestureListener) 可以省略
//获得当前接受操作的布局id
RelativeLayout mActivity = (RelativeLayout)findViewById(R.id.main_activity);
//触屏监听
mActivity.setOnTouchListener(this);
mActivity.setLongClickable(true);

3、重载onFling()和onTouch();

public boolean onTouch(View v, MotionEvent event) {return mGestureDetector.onTouchEvent(event);
}public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {//省略
}

第二种方法如下:

1、继承OnTouchListener  ;

//继承OnTouchListener
public class MainActivity extends Activity implements OnTouchListener{

2、初始化OnGestureListener ,即注册手势监听 ;获得当前接受操作的布局id ; 注册触屏监听;

//手势监听
mGestureDetector = new GestureDetector(MainActivity.this,new MySimpleOnGestureListener());//下边定义MySimpleOnGestureListener,//并使其继承SimpleOnGestureListener类
//获得当前接受操作的布局id
RelativeLayout mActivity = (RelativeLayout)findViewById(R.id.main_activity);
//触屏监听
mActivity.setOnTouchListener(this);
mActivity.setLongClickable(true);

3、定义SimpleOnGestureListener子类,并重载onTouch()和所需方法;

class MySimpleGestureListener extends SimpleOnGestureListener{@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {//大于设定的最小滑动距离并且在水平/竖直方向速度绝对值大于设定的最小速度,则执行相应方法if(e1.getX()-e2.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){Toast.makeText(MainActivity.this, "turn left", Toast.LENGTH_SHORT).show();}else if(e2.getX() - e1.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){Toast.makeText(MainActivity.this, "turn right", Toast.LENGTH_SHORT).show();}else if(e1.getY()-e2.getY() > 20 && Math.abs(velocityY) > 10){Toast.makeText(MainActivity.this, "turn up", Toast.LENGTH_SHORT).show();}else if(e2.getY()-e1.getY() > 20 && Math.abs(velocityY) > 10){Toast.makeText(MainActivity.this, "turn down", Toast.LENGTH_SHORT).show();}return false;}@Overridepublic boolean onDown(MotionEvent e){Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();return false;}}

具体代码如下:

(第一种方法)

import android.app.Activity;
import android.content.Intent;import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.*;public class MainActivity extends Activity implements OnTouchListener,OnGestureListener{private static final String tag = "mainActivity";private GestureDetector mGestureDetector;private int verticalMinistance = 20;          //水平最小识别距离private int minVelocity = 10;            //最小识别速度@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//手势监听mGestureDetector = new GestureDetector(MainActivity.this,(OnGestureListener) this);//(OnGestureListener) 可以省略//获得当前接受操作的布局idRelativeLayout mActivity = (RelativeLayout)findViewById(R.id.main_activity);//触屏监听mActivity.setOnTouchListener(this);mActivity.setLongClickable(true);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onTouch(View v, MotionEvent event) {return mGestureDetector.onTouchEvent(event);}public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {//大于设定的最小滑动距离并且在水平/竖直方向速度绝对值大于设定的最小速度,则执行相应方法if(e1.getX()-e2.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){//在此处实现跳转//Intent intent  = new Intent(MainActivity.this,nextActivity.class);//startActivity(intent);Toast.makeText(MainActivity.this, "turn left", Toast.LENGTH_SHORT).show();}else if(e2.getX() - e1.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){Toast.makeText(MainActivity.this, "turn right", Toast.LENGTH_SHORT).show();}else if(e1.getY()-e2.getY() > 20 && Math.abs(velocityY) > 10){Toast.makeText(MainActivity.this, "turn up", Toast.LENGTH_SHORT).show();}else if(e2.getY()-e1.getY() > 20 && Math.abs(velocityY) > 10){Toast.makeText(MainActivity.this, "turn down", Toast.LENGTH_SHORT).show();}return false;}//只要有触发就会调用次方法@Overridepublic boolean onDown(MotionEvent e) {Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();return false;}@Overridepublic void onLongPress(MotionEvent e) {Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_SHORT).show();}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {//       Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_SHORT).show();return false;}@Overridepublic void onShowPress(MotionEvent e) {
//      Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();}@Overridepublic boolean onSingleTapUp(MotionEvent e) {
//      Toast.makeText(MainActivity.this, "onsingleTapup", Toast.LENGTH_SHORT).show();return false;}
}

(第二种方法)

import android.app.Activity;
import android.content.Intent;import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.*;public class MainActivity extends Activity implements OnTouchListener{private static final String tag = "mainActivity";private GestureDetector mGestureDetector;private int verticalMinistance = 20;            //水平最小识别距离private int minVelocity = 10;            //最小识别速度@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//手势监听mGestureDetector = new GestureDetector(MainActivity.this,new LearnGestureListener());//获得当前接受操作的布局idRelativeLayout mActivity = (RelativeLayout)findViewById(R.id.main_activity);//触屏监听mActivity.setOnTouchListener(this);mActivity.setLongClickable(true);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onTouch(View v, MotionEvent event) {return mGestureDetector.onTouchEvent(event);}class LearnGestureListener extends SimpleOnGestureListener{@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {//大于设定的最小滑动距离并且在水平/竖直方向速度绝对值大于设定的最小速度,则执行相应方法if(e1.getX()-e2.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){Toast.makeText(MainActivity.this, "turn left", Toast.LENGTH_SHORT).show();}else if(e2.getX() - e1.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){Toast.makeText(MainActivity.this, "turn right", Toast.LENGTH_SHORT).show();}else if(e1.getY()-e2.getY() > 20 && Math.abs(velocityY) > 10){Toast.makeText(MainActivity.this, "turn up", Toast.LENGTH_SHORT).show();}else if(e2.getY()-e1.getY() > 20 && Math.abs(velocityY) > 10){Toast.makeText(MainActivity.this, "turn down", Toast.LENGTH_SHORT).show();}return false;}@Overridepublic boolean onDown(MotionEvent e){Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();return false;}}}

转载于:https://www.cnblogs.com/jiangu66/p/3202732.html

GestureDetector学习之左右滑动,上下滑动屏幕切换页面相关推荐

  1. swiper的介绍以及使用(移动端滑动屏幕切换页面案例)

    一.swiper介绍 Swiper常用于移动端网站的内容触摸滑动  Swiper是纯javascript打造的滑动特效插件,面向手机.平板电脑等移动终端.  Swiper能实现轮播图.触屏焦点图.触屏 ...

  2. 使用vue-touch实现移动端左右滑动屏幕切换页面(左右滑动切换路由)

    1.安装vue-touch npm install vue-touch@next --save 2.在main.js中引入 import  VueTouch from 'vue-touch' Vue. ...

  3. Android学习笔记之滑动翻页(屏幕切换)

    如何实现手机上手动滑动翻页效果呢?呵呵,在这里我们就给你们介绍一下吧. 一般实现这个特效会用到一个控件:ViewFlipper <1>View切换的控件-ViewFlipper 这个控件是 ...

  4. java 滑块控制图片渐变切换_Android实现滑动屏幕切换图片

    本文实例为大家分享了Android实现滑动屏幕切换图片的具体代码,供大家参考,具体内容如下 activity_main.xml 文件代码: xmlns:app="http://schemas ...

  5. Android ViewFlipper滑动屏幕切换

    最近看到ViewFlipper和Animation在一起的用法,感觉很好,我就自己写了一下,感觉灰常好用,效果比gallery的滚动查看图片好用多了,这个也是实现滚动屏幕切换图片,但是用户体验要好很多 ...

  6. 转 Android的Activity屏幕切换动画(一)-左右滑动切换

    这段时间一直在忙Android的项目,总算抽出点时间休息一下,准备把一些项目用到的Android经验分享一下. 在Android开发过程中,经常会碰到Activity之间的切换效果的问题,下面介绍一下 ...

  7. Android的屏幕切换动画—左右滑动切换

    预备知识: 在使用自定义视图的时候,对触屏事件的处理是比不可少的,有能力的可以自己写代码处理,这样更加的灵活.如果不想这么麻烦,Android提供了一个手势监听类GestureDetector,可以供 ...

  8. Android的Activity屏幕切换动画(一)-左右滑动切换

    为什么80%的码农都做不了架构师?>>>    这段时间一直在忙Android的项目,总算抽出点时间休息一下,准备把一些项目用到的Android经验分享一下. 在Android开发过 ...

  9. android群英传 自定义滑动view,Android群英传学习之路-View的滑动

    滑动效果是如何产生的? 滑动一个View从本质上来说就是移动一个View,改变其当前的坐标.所以,想要滑动一个View,就必须监听该View的触摸事件,并且根据事件的坐标,不断的改变View的坐标从而 ...

最新文章

  1. python opencv生成模糊图像
  2. java jmf 视屏监控的核心代码_Java中利用JMF编写摄像头拍照程序_java
  3. html5调用手机摄像头,实现拍照上传功能
  4. 使用electron-vue搭建桌面应用程序项目
  5. POJ 1228 Grandpa's Estate --深入理解凸包
  6. python socketio_python3--socketIO_client 摸索怕坑指南
  7. tsd3dmapper软件使用方法_TOYO模组选型软件使用方法
  8. 虚拟专题:联邦学习 | 联邦学习算法综述
  9. 修改Jupyter Notebook默认工作目录
  10. 此计算机必须为委派而被信任_实际利用Kerberos Bronze Bit漏洞绕过委派限制(CVE202017049)...
  11. grafana 迁移
  12. android打开cad文件怎么打开方式,dwg文件怎么打开_手机上dwg文件用什么程序可以打开...
  13. iphone开发:ipa打包
  14. Nginx的安装使用----反向代理服务器
  15. 三、Redis在SpringBoot中使用案例
  16. android多屏幕多分辨率的一些概念
  17. 当遇到error: stray ‘\241‘ in program错误的解决方法
  18. 用户显示图片的服务器是什么,显示服务器上的图片怎么写
  19. 工业物联网安全需要一揽子服务商
  20. skin卓沿护肤品怎么样_卓沿白金护肤品怎么样 卓沿白金护肤品是哪个国家的

热门文章

  1. React开发(274):ant design 时间显示秒
  2. React开发(185):ant design table控制居中和宽度
  3. React开发(176):导出所有接口供使用
  4. javascript学习系列(24):数组中的substring方法
  5. Taro+react开发(7)--控制跳转
  6. 前端学习(3253):vs code中组件化编码
  7. 前端学习(3252):react脚手架
  8. [html] 当html中使用map标签时,area中coords值如何精确定位呢?
  9. [css] 如何重写行内样式?方法有哪些(至少两种)?
  10. [css] 说说position:sticky有什么应用场景