zoukankan      html  css  js  c++  java
  • [android] 手机卫士手势滑动切换屏幕

    定义手势识别器

    获取手势识别器GestureDetector对象,通过new GestureDetector(context,listener),参数:上下文,监听器

    匿名内部类实现简单手势监听器SimpleOnGestureListener接口,重写onFling()滑动方法

    传递进来四个参数:

    MotionEvent e1 MotionEvent e2velocityXvelocityY

    e1是第一个点,e2是第二个点,x轴的速度,y轴的速度

    当第一个点减去第二个点大于200时,我们认为它是从右往左划,下一页

    当第二个点减去第一个点大于200时,我们认为它是从左往右划,上一页

    调用MotionEvent 对象的getRawX()可以获取到X轴的坐标

    使用手势识别器识别手势

    重写activityonTouchEvent()方法,获取到手势在界面上的滑动事件

    传递进来一个参数MotionEvent对象

    调用GestureDetector对象的onTouchEvent(event)方法,参数:MotionEvent对象,把获取到的事件传递进去

    屏蔽斜着划

    两个点的y轴坐标之间的距离大于100时,我们认为它是斜着划的

    调用MotionEvent 对象的getRawY()可以获取到Y轴的坐标,两个点的差值取绝对值Math.abs(),判断大于100 就返回true,不往下进行

    如果找不到SimpleOnGestureListener类,使用new GestureDetector.SimpleOnGestureListener()

    抽取公用方法到基类抽象类 BaseSecActivity中,自己的activity只需要继承这个基类,实现上下页的抽象方法,就能实现左右滑动效果

    BaseSecGuideActivity.java

    package com.qingguow.mobilesafe;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    
    public abstract class BaseSecGuideActivity extends Activity {
        // 定义手势识别器
        protected GestureDetector gestureDetector;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            // 实例化
            gestureDetector = new GestureDetector(this,
                    new GestureDetector.SimpleOnGestureListener() {
                        @Override
                        public boolean onFling(MotionEvent e1, MotionEvent e2,
                                float velocityX, float velocityY) {
                            //屏蔽斜着划
                            if(Math.abs(e1.getRawY()-e2.getRawY())>100){
                                return true;
                            }
                            if ((e1.getRawX() - e2.getRawX()) > 100) {
                                System.out.println("从右往左划,下一页");
                                showNext();
                                return true;
                            }
                            if ((e2.getRawX() - e1.getRawX()) > 100) {
                                System.out.println("从左往右划,上一页");
                                showPre();
                                return true;
                            }
                            return super.onFling(e1, e2, velocityX, velocityY);
                        }
                    });
        }
        public abstract void showPre();
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            return super.onTouchEvent(event);
        }
        public abstract void showNext();
    }
  • 相关阅读:
    启动redis时报错:FATAL CONFIG FILE ERROR :Bad directive or wrong number of arguments
    转载Redis的三个框架:Jedis,Redisson,Lettuce
    转载Dubbo详解(一):Dubbo介绍和SpringBoot整合Dubbo+ZooKeeper
    Redisson如何实现类似incr的自增操作
    转载解决 com.alibaba.fastjson.JSONException: autoType is not support.
    转载springboot+mybatis实现数据库的读写分离
    转载dubbo服务被重复调用三次的原因
    js中实现函数防抖跟函数节流
    网站项目后台的目录命名为admin后,网页莫名其妙的变样了
    createreactapp不支持less的解决方式
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5380696.html
Copyright © 2011-2022 走看看