zoukankan      html  css  js  c++  java
  • android 上下左右手势判断 根据别人的改的

    GestureUtils.java

    复制代码
    package com.gesture;

    import android.content.Context;
    import android.util.DisplayMetrics;
    import android.view.WindowManager;

    public class GestureUtils {



    //获取屏幕的大小
    public static Screen getScreenPix(Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(dm);
    return new Screen(dm.widthPixels,dm.heightPixels);
    }

    public static class Screen{

    public int widthPixels;
    public int heightPixels;

    public Screen(){

    }

    public Screen(int widthPixels,int heightPixels){
    this.widthPixels=widthPixels;
    this.heightPixels=heightPixels;
    }

    @Override
    public String toString() {
    return "("+widthPixels+","+heightPixels+")";
    }

    }

    }
    复制代码

      BuileGestureExt.java

    复制代码
    package com.gesture;

    import com.gesture.GestureUtils.Screen;

    import android.content.Context;
    import android.view.GestureDetector;
    import android.view.MotionEvent;

    public class BuileGestureExt {
    public static final int GESTURE_UP=0;
    public static final int GESTURE_DOWN=1;
    public static final int GESTURE_LEFT=2;
    public static final int GESTURE_RIGHT=3;
    private OnGestureResult onGestureResult;
    private Context mContext;
    public BuileGestureExt(Context c,OnGestureResult onGestureResult) {
    this.mContext=c;
    this.onGestureResult=onGestureResult;
    screen = GestureUtils.getScreenPix(c);
    }
    public GestureDetector Buile()
    {
    return new GestureDetector(mContext, onGestureListener);
    }

    private Screen screen;
    private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener(){

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
    float x = e2.getX() - e1.getX();
    float y = e2.getY() - e1.getY();
    //限制必须得划过屏幕的1/4才能算划过
    float x_limit = screen.widthPixels / 4;
    float y_limit = screen.heightPixels / 4;
    float x_abs = Math.abs(x);
    float y_abs = Math.abs(y);
    if(x_abs >= y_abs){
    //gesture left or right
    if(x > x_limit || x < -x_limit){
    if(x>0){
    //right
    doResult(GESTURE_RIGHT);
    }else if(x<=0){
    //left
    doResult(GESTURE_LEFT);
    }
    }
    }else{
    //gesture down or up
    if(y > y_limit || y < -y_limit){
    if(y>0){
    //down doResult(GESTURE_DOWN); }else if(y<=0){ //up doResult(GESTURE_UP); } } } return true; } }; public void doResult(int result) { if(onGestureResult!=null) { onGestureResult.onGestureResult(result); } } public interface OnGestureResult { public void onGestureResult(int direction); }}
    复制代码

      demo

    package com.gesture;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.widget.Toast;
     
    public class GesturetestActivity extends Activity {
        /** Called when the activity is first created. */
     
        private GestureDetector gestureDetector; 
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            gestureDetector = new BuileGestureExt(this,new BuileGestureExt.OnGestureResult() {
                @Override
                public void onGestureResult(int direction) {
                    show(Integer.toString(direction));
                }
            }
            ).Buile();
        }
     
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
        private void show(String value){
            Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
        }
     
         
    }
  • 相关阅读:
    关于javaScript substring()方法的一点点应用
    解决Vue报错:Invalid prop: type check failed for prop "id". Expected Number with value 14, got String with value "14".
    better-scroll滚动无效的查错
    Vue程序化导航---又称编程式导航
    Vue路由初始化Can't read prooerty '$createElement' of undefined
    在前台利用jquery对dom元素进行排序
    js/jquery如何获取获取父窗口的父窗口的元素
    jquery使鼠标滚轮暂时失效
    introJs写法
    用Intro.js创建站点分步指南
  • 原文地址:https://www.cnblogs.com/xgjblog/p/3875432.html
Copyright © 2011-2022 走看看