• [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();
    }
  • 相关阅读:
    [转]22条经典的编程引言 朱燚:
    [转]Windbg的学习记录(一) 朱燚:
    C#7.0 模式匹配与if语句
    使用switch表达式简化switch语句
    .Net 5 在函数中使用Lambda
    MongoDB find getmore操作慢问题排查
    multikey索引和wildCard索引场景比较
    一个高性能跨平台基于Python的Waitress WSGI Server的介绍!
    郁闷的一天!
    互联网项目管理要点
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5380696.html
Copyright © 2020-2023  润新知