Android手势识别和双击事件
来源:互联网 发布:网络抓包原理 编辑:程序博客网 时间:2024/05/19 20:22
安卓触摸屏的手势识别在很多时候会使用户操作更加方便,实现原理主要是将该界面的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);
- }
- });
- }
- }
- gestureDemo.zip (1012.6 KB)
- 下载次数: 12
0 0
- Android手势识别和双击事件
- android识别 单击和双击事件
- android识别 单击和双击事件
- 手势 双击 拖动事件
- iOS 同一个View识别单击和双击手势
- UITapGestureRecognizer手势识别之双击和单击结合
- android手势创建和识别
- Android保存和识别手势
- ios触摸事件和手势识别
- IOS学习 触摸和手势 手势识别器:单击、双击、轻扫、滑动、长按、旋转
- 手势识别事件详解
- android手势和触摸事件
- Android 触屏事件处理_手势识别 鼠标事件
- 手势操作---识别单击还是双击
- Hololens入门之手势识别(单击、双击)
- Android 手势的识别和控制 GestureDetecor
- Android多点触摸和手势识别
- Android中添加和识别手势操作
- Hbase rowkey的设计 与 优化
- 关于人脑成像的疑问
- Lsat Time
- python3.x中的生成器generator调用next方法
- Oracle配置:监听程序
- Android手势识别和双击事件
- EasyUi dataGrid JavaBean
- iOS开发之UIWebView与JavaScript的交互
- hdu 5050 Divided Land(高精度)
- oracle日记
- ios应用版本号设置规则
- IOS高级开发~底层实现资源收集
- C++_字符串(string)
- mysql 查看数据库信息、表大小