安卓触摸屏的手势识别在很多时候会使用户操作更加方便:
实现原理主要是将该界面的onTouchEvent设置为GestureDetector的onTouchEvent:
具体实现代码:
1.在Activity中响应事件:
MainActivity.java
package com.hu.gesturedemo; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.widget.Toast; public class MainActivity extends Activity { GestureDetector gestureDetector = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gestureDetector = new GestureDetector(this, new SimpleOnGestureListener(){ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, //滑动事件 float velocityX, float velocityY) { if(Math.abs(velocityX) > Math.abs(velocityY)){ //如果X偏移量大于Y偏移量 if(velocityX > 0){ Toast.makeText(MainActivity.this, "Right Fling", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "Left Fling", Toast.LENGTH_SHORT).show(); } }else{ if(velocityY > 0){ Toast.makeText(MainActivity.this, "Down Fling", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "Up Fling", Toast.LENGTH_SHORT).show(); } } return super.onFling(e1, e2, velocityX, velocityY); } @Override public boolean onDoubleTap(MotionEvent e) { //双击事件 Toast.makeText(MainActivity.this, "On double Tap", Toast.LENGTH_SHORT).show(); return super.onDoubleTap(e); } }); } @Override public boolean onTouchEvent(MotionEvent event) { return gestureDetector.onTouchEvent(event); } }
2.在View中响应事件
MyView.java
package com.hu.gesturedemo; import android.content.Context; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; public class MyView extends View{ GestureDetector gestureDetector = null; public MyView(Context context) { super(context); gestureDetector = new GestureDetector(context, new SimpleOnGestureListener(){ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, //滑动事件 float velocityX, float velocityY) { if(Math.abs(velocityX) > Math.abs(velocityY)){ //如果X偏移量大于Y偏移量 if(velocityX > 0){ Toast.makeText(getContext(), "Right Fling", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getContext(), "Left Fling", Toast.LENGTH_SHORT).show(); } }else{ if(velocityY > 0){ Toast.makeText(getContext(), "Down Fling", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getContext(), "Up Fling", Toast.LENGTH_SHORT).show(); } } return super.onFling(e1, e2, velocityX, velocityY); } @Override public boolean onDoubleTap(MotionEvent e) { //双击事件 Toast.makeText(getContext(), "On double Tap", Toast.LENGTH_SHORT).show(); return super.onDoubleTap(e); } }); this.setClickable(true); this.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); } }
相关推荐
Android手势识别算法,除识别基本点击,双击,长按操作,主要用于识别上,下,左,右,左上,左下,右上,右下8个触摸方向
主要介绍了android使用手势识别的方法,介绍了单击触摸屏触发的事件和双击事件的使用等方法,大家参考使用吧
对于Android中的手势识别可以从以下三个Listener入手——OnTouchListener、OnGestureListener、OnDoubleTapListener。这三个监听器分别是触摸监听、手势滑动监听和屏幕双击操作监听。很多的时候我们需要这些手势识别...
Gallery实现一组图片播放 双击放大缩小,手势也能实现放大缩小功能。滑动播放图片。嵌套在自己的项目中,直接能用,我就是在实际项目中进行整理的
今天有个需求,要监听控件的双击、拖动、滑动等事件,这时onTouchEvent()很明显不能满足我们的需求,经多方打听,找到了今天的主角GestureDetector,下面就对它进行简单的学习。 构造方法: 已过时的有2个,不推荐...
相册管家系统借助Android平台,采用异步加载图片、软引用缓存,手势识别和图片处理等技术。实现了图片的搜索,排序,应用,删除,浏览和拍照等功能,图片浏览支持滑动切换,双击放大,触摸缩放和幻灯播放等操作。...
除此之外,还有缩放、拖拽、双击放大等功能,支持viewpager和scaletype,并伴有动画效果。 sharedConstructing private void sharedConstructing(Context context) { super.setClickable(true); this.context = ...
适用于可绘制对象和样式的Android演示,包括: 带有动画的淡入淡出和滑动视图演示重复动画演示使用自定义动画在活动之间进行过渡的演示用于视图的手势识别器演示(双击,滑动) 将视图拖到可放置的目标区域中的演示...
|--手势识别器GestureDetector的用法 |--拍照之调用系统相机并显示及保存 |--拨打电话 |--按健之长按menu事件屏蔽 |--按健监听按返回健回桌面 |--搜索之调用系统Searchable的用法 |--数据库CURD通过execSQL与...