Android手势识别和双击事件

安卓触摸屏的手势识别在很多时候会使用户操作更加方便:

实现原理主要是将该界面的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手势识别和双击事件
 

相关推荐