关闭

简单的安卓手势处理类

标签: 安卓java
299人阅读 评论(0) 收藏 举报
分类:
package com.example.activity;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

/**
 * @fileName HandyGesturer.java
 * @version 1.0
 * @date 2015-11-19 下午4:44:42
 * @description
 * 智能的手势处理类
 * 
 */
public class HandyGesturer {
	private HandyGestureListener listener = null;
	private Context context;
	private float maxSideLength = 50.0F;
	public HandyGesturer(Context context, HandyGestureListener listener,
			float maxSideLength) {
		super();
		this.listener = listener;
		this.context = context;
		this.maxSideLength = maxSideLength;
	}
	public HandyGesturer( Context context, HandyGestureListener listener) {
		super();
		this.listener = listener;
		this.context = context;
	}
	/**
	 * @return the listener
	 */
	public HandyGestureListener getListener() {
		return listener;
	}
	/**
	 * @param listener the listener to set
	 */
	public void setListener(HandyGestureListener listener) {
		this.listener = listener;
	}
	/**
	 * @return the maxSideLength
	 */
	public float getMaxSideLength() {
		return maxSideLength;
	}
	/**
	 * @param maxSideLength the maxSideLength to set
	 */
	public void setMaxSideLength(float maxSideLength) {
		this.maxSideLength = maxSideLength;
	}
	public interface HandyGestureListener{
		void onSideUp();
		void onSideDown();
		void onSideLeft();
		void onSideRight();
	}
	private float x1 = 0;
	private float x2 = 0;
	private float y1 = 0;
	private float y2 = 0;
	public void dealWithTouchEvent(MotionEvent event){
		if(listener == null){
			throw new NullPointerException("HandyGestureListener is null!");
		}
		switch (event.getAction()) {
		
		case MotionEvent.ACTION_DOWN:
			x1 = event.getX();
			y1 = event.getY();
			break;
		case MotionEvent.ACTION_UP:
			x2 = event.getX();
			y2 = event.getY();
			if(y1 - y2 > maxSideLength) {
				listener.onSideUp();
			} else if(y2 - y1 > maxSideLength) {
				listener.onSideDown();
			} else if(x1 - x2 > maxSideLength) {
				listener.onSideLeft();
			} else if(x2 - x1 > maxSideLength) {
				listener.onSideRight();
			}
			break;
		default:
			break;
		}

	}
}



<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">使用很简单。</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="java">package com.example.activity;



import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;

import com.example.activity.HandyGesturer.HandyGestureListener;


public class MainActivity extends Activity  implements HandyGestureListener{


	HandyGesturer gesturer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		gesturer = new HandyGesturer(this, this, 50);
	
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		gesturer.dealWithTouchEvent(event);
		return super.onTouchEvent(event);
	}

	@Override
	public void onSideUp() {
		System.out.println("onUp");
		
	}

	@Override
	public void onSideDown() {
		System.out.println("onDown");
	}

	@Override
	public void onSideLeft() {
		System.out.println("onLeft");
	}

	@Override
	public void onSideRight() {
		System.out.println("onRight");
	}

	

	



}




实例化手势处理类,并且设置一个监听器就行了。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:50769次
    • 积分:1005
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:11篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论