博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 上下左右手势判断 根据别人的改的
阅读量:6555 次
发布时间:2019-06-24

本文共 4406 字,大约阅读时间需要 14 分钟。

原文地址:

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();     }         }

 

转载于:https://www.cnblogs.com/android88/archive/2012/07/18/3602642.html

你可能感兴趣的文章
Mysql数据库大小查询
查看>>
#78 Reimplement Trampoline
查看>>
使用Java制作图文验证码
查看>>
java学习笔记----之多线程开发
查看>>
使用javap分析return和finally的执行字节码
查看>>
java 代理
查看>>
数据库设计三范式
查看>>
Eclipse插件开发- view to view drag drop
查看>>
Linux 技巧:让进程在后台可靠运行的几种方法
查看>>
ORACLE特殊字符的处理方法
查看>>
根据Servlet的Filter自定义实现字符编码过滤器
查看>>
shiro之Remembered vs. Authenticated
查看>>
碉堡了!又一只会跑酷的狗狗!
查看>>
python入门(一)-- 简介与基本语法
查看>>
oh-my-zsh安装与配置
查看>>
pyramid学习笔记整理
查看>>
common lisp asdf
查看>>
git修改远程仓库地址
查看>>
Guess the number
查看>>
iscsi网络存储
查看>>