GestureDetector 手势滑动识别

将activity侦听到的触摸事件委托给GestureDetector手势识别对象的触摸事件处理。
mDetector.onTouchEvent(ev)

 GestureDetector mDetector = new GestureDetector(this, new SimpleOnGestureListener(){     //简单的手势侦听。
            @Override
              //fling  一撇   监听手势滑动事件。
              //在这里e1表示手势滑动的起点
              //e2 表示滑动手势的终点;  
              // velocityX : 表示水平方向滑动的速度
              //velocityY  竖直方向滑动的速度。

            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                // TODO Auto-generated method stub
                return super.onFling(e1, e2, velocityX, velocityY);
            }

        });

注意的是:
getRawX : 得到是整个屏幕的坐标体系。 手势滑动识别用的是getRawX;

getX: 区别; 父类控件的坐标体系。

demo:

package com.zh.mobilesafe.activity;

import com.zh.mobilesafe.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.Toast;

/**
 * 设置引导页面的基类。 //不需要在清单文件中注册 不需要展示。
 * 
 * @author zh
 *
 */
public abstract class BaseSetupActivity extends Activity {

    private GestureDetector mDetector;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        mDetector = new GestureDetector(this, new SimpleOnGestureListener() {
            @Override
            // fling 一撇 监听手势滑动事件。
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                // TODO Auto-generated method stub
                //判断纵向滑动的幅度不要太大  
                if(Math.abs(e2.getRawY()-e1.getRawY())>100){
                    Toast.makeText( BaseSetupActivity.this,"不能这样划哦", 0).show();
                    return  true ;
                }
                if(Math.abs(velocityX)<100){
                    //判断滑动是否太慢。
                    Toast.makeText(BaseSetupActivity.this, "划得太慢了", 0).show();
                    return true;
                }

                // 向右滑动
                if (e2.getRawX() - e1.getRawX() > 200) {
                    showPreviouspage();
                    return true;

                }
                // e1>e2 表示手势向左划。
                if (e1.getRawX() - e2.getRawX() > 200) {
                    showNextpage();
                    return true;
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }

        });
    }

    public  abstract void showNextpage();

    // 显示上一页的activity;
    public   abstract void showPreviouspage();

    // 点击按钮进入下一页。
    public void next(View v) {
        showNextpage();
    }

    // 进入上一步的方法: click的点击事件。d点击上一页的按钮。
    public void previous(View v) {
        showPreviouspage();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值