Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android onTouchEvent事件

Android onTouchEvent事件

編輯:關於Android編程

根據用戶的觸摸判斷滑動方向,選擇彈出popupWindow 或者dialog ,也可以切換界面,定義切換動畫

 

定義接口:

 

/**
 * According to the Angle of the sliding range sliding direction
 * @author LanYan
 *
 */
public interface OnEventListener {

	/**
	 * ←
	 */
	void onLeft();
	/**
	 * ↑
	 */
	void onTop();
	/**
	 * →
	 */
	void onRight();
	/**
	 * ↓
	 */
	void onBottom();
	/**
	 * ↗
	 */
	void onNortheast();
	/**
	 * ↖
	 */
	void onNorthwest();
	/**
	 * ↙
	 */
	void onSouthwest();
	/**
	 * ↘
	 */
	void onSoutheast();
	/**
	 * 觸摸屏幕
	 */
	void onTouch();
	/**
	 * 事件分發
	 */
	EventType onEventDistribute();
	

方法調用工具類:

 

 

import android.view.MotionEvent;
import android.view.View;

public class OnEventTouchFactory {

	private static OnEventTouchFactory mCurrent;
	private static OnEventListener mListener;
	private int mLastEventX;
	private int mLastEventY;
	private EventType mType;
	private int mDistance = 50;
	private double cos;
	private double result;
	private EventType mCurrentType = null;

	public OnEventTouchFactory() {

	}

	public static OnEventTouchFactory getInstance(OnEventListener listener) {
		mListener = listener;
		if (mCurrent == null) {
			mCurrent = new OnEventTouchFactory();
		}
		return mCurrent;
	}

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		boolean  status=false;
		mListener.onTouch();
		int eventX = (int) event.getX();
		int eventY = (int) event.getY();
		int action = event.getAction();
		if (action == MotionEvent.ACTION_DOWN) {
			mLastEventX = eventX;
			mLastEventY = eventY;
			mCurrentType = mListener.onEventDistribute();
			status=true;
		} else if (action == MotionEvent.ACTION_MOVE) {
			if (mCurrentType == EventType.normal) {
				cos = getAngle(eventX, eventY, mLastEventX, mLastEventY);
				result = getDistance(mLastEventX, mLastEventY, eventX, eventY);
				/**
				 * EeventType: Left ←: [157.5,202.5] Top ↑: [67.5,112.5] Right
				 * →: [337.5,360]&&[0,22.5] Bottom ↓: [247.5,292.5] Northwest ↖:
				 * [112.5,157.5] ┅┅┅┅┅ ┅┅┅┅┅ ┅┅┅┅┅ Southeast ↘: [292.5,337.5] ┇
				 * Southwest ↙: [202.5,247.5] ┇ Northeast ↗: [22.5,67.5] ┇ ┇
				 * VelocityXY ↖↗ --------------- ↙ ↘ ┅┅┅┅┅ ┅┅┅┅┅ ┅┅┅┅┅┅┅┅┅┅
				 * ┅┅┅┅┅┇
				 */

				if ((cos >= 0 && cos <= 22.5) || (cos >= 337.5 && cos <= 360)) {
					mType = EventType.left;
				}

				else if (cos > 22.5 && cos <= 67.5) {
					mType = EventType.nortwest;
				}

				else if (cos >= 67.5 && cos <= 112.5) {
					mType = EventType.top;
				}
				else if (cos > 112.5 && cos < 157.5) {
					mType = EventType.northeast;
				}

				else if (cos >= 157.5 && cos <= 202.5) {
					mType = EventType.right;
				}

				else if (cos > 202.5 && cos < 247.5) {
					mType = EventType.southeast;
				}

				else if (cos >= 247.5 && cos <= 292.5) {
					mType = EventType.bottom;
				}

				else if (cos > 292.5 && cos < 337.5) {
					mType = EventType.southwest;
				}
				status=true;
			}  else if (mCurrentType == EventType.complete) {
				status= false;
			}
		} else if (action == MotionEvent.ACTION_UP) {
			if (mType == EventType.right && result > mDistance) {
				mListener.onRight();
			} else if (mType == EventType.top && result > mDistance) {
				mListener.onTop();
			} else if (mType == EventType.northeast && result > mDistance) {
				mListener.onNortheast();
			} else if (mType == EventType.nortwest && result > mDistance) {
				mListener.onNorthwest();
			} else if (mType == EventType.left && result > mDistance) {
				mListener.onLeft();
			} else if (mType == EventType.southeast && result > mDistance) {
				mListener.onSoutheast();
			} else if (mType == EventType.southwest && result > mDistance) {
				mListener.onSouthwest();
			} else if (mType == EventType.bottom && result > mDistance) {
				mListener.onBottom();
			}
			status=  true;
		}
		return status;
	}

	public double getAngle(int px1, int py1, int px2, int py2) {
		// 兩點的x、y值
		int x = px2 - px1;
		int y = py2 - py1;
		double hypotenuse = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
		// 斜邊長度
		double cos = x / hypotenuse;
		double radian = Math.acos(cos);
		// 求出弧度
		double angle = 180 / (Math.PI / radian);
		// 用弧度算出角度
		if (y < 0) {
			angle = 180 + (180 - angle);
		} else if ((y == 0) && (x < 0)) {
			angle = 180;
		}
		return angle;
	}

	/**
	 * 獲取兩點的距離
	 * 
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @return
	 */
	public double getDistance(int x1, int y1, int x2, int y2) {
		double d = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
		return Math.sqrt(d);
	}


	public enum EventType {
		left, top, right, bottom, nortwest, southeast, southwest, northeast,
		/**
		 * event不向下分發
		 */
		normal,
		/**
		 * 完全向下分發
		 */
		complete
		
	}
}

BaseActivity類:

 

 

import com.example.base.OnEventTouchFactory.EventType;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

public abstract class BaseActivity extends Activity implements OnTouchListener, OnEventListener{
	
	protected Class mLeftClass=null;
	protected Class mRightClass=null;
	protected Class mTopClass=null;
	protected Class mBottomClass=null;
	
	protected Class mNortheastClass=null;
	protected Class mNorthwestClass=null;
	protected Class mSoutheastClass=null;
	protected Class mSouthwestClass=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTheme();
		setContentView(getLayoutResID());
		configClass();
		initialization();
	}

	public abstract int getLayoutResID();
	public void setTheme(){

	}
	public void configClass(){
		
	}
	public void initialization(){

	}
	
	/***********************************onTouch*********************************************/

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(OnEventTouchFactory.getInstance(this).onTouch(v, event)){
			return true;
		}
		
		return false;
	}
	
	@Override
	public void onLeft() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTop() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRight() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBottom() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNortheast() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNorthwest() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSouthwest() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSoutheast() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTouch() {
		// TODO Auto-generated method stub

	}
	
	
	
	@Override
	public EventType onEventDistribute() {
		// TODO Auto-generated method stub
		return EventType.normal;
	}
	/***********************************Method*********************************************/
	public void startActivity(Class cls){
		Intent intent=new Intent();
		intent.setClass(this, cls);
		startActivity(intent);
	}
	public void startActivity(Class cls,boolean isFinish){
		Intent intent=new Intent();
		intent.setClass(this, cls);
		startActivity(intent);
		if(isFinish){
			finish();
		}
	}
	
	public void startActivityWithAnimation(Class cls,int enterAnim,int exitAnim){
		Intent intent=new Intent();
		intent.setClass(this, cls);
		startActivity(intent);
		overridePendingTransition(enterAnim, exitAnim);
	}
	
	public void startActivity(Class cls,boolean isFinish,int enterAnim,int exitAnim){
		Intent intent=new Intent();
		intent.setClass(this, cls);
		startActivity(intent);
		if(isFinish){
			finish();
		}
		overridePendingTransition(enterAnim, exitAnim);
	}
	public void ShowSToast(String message){
		Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
	}
	public void ShowLToast(String message){
		Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
	}
}

測試Activity類:

 

 

import android.text.method.ScrollingMovementMethod;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.base.BaseActivity;
import com.example.base.OnEventTouchFactory.EventType;

public class MainActivity extends BaseActivity {

	@Override
	public int getLayoutResID() {
		// TODO Auto-generated method stub
		return R.layout.activity_main;
	}
	
	@Override
	public void configClass() {
		// TODO Auto-generated method stub
		super.configClass();
	}
	
	@Override
	public void initialization() {
		// TODO Auto-generated method stub
		super.initialization();
		RelativeLayout parent=(RelativeLayout)findViewById(R.id.parent);
		TextView child=(TextView)findViewById(R.id.child);
		
		child.setMovementMethod(ScrollingMovementMethod.getInstance());
	    parent.setOnTouchListener(this);
	}
	@Override
	public void onRight() {
		// TODO Auto-generated method stub
		super.onRight();
		ShowSToast("right");
	}
	@Override
	public void onTop() {
		// TODO Auto-generated method stub
		super.onTop();
		ShowSToast("top");
	}
	@Override
	public void onLeft() {
		// TODO Auto-generated method stub
		super.onLeft();
		ShowSToast("left");
	}
	@Override
	public void onBottom() {
		// TODO Auto-generated method stub
		super.onBottom();
		ShowSToast("bottom");
	}
	@Override
	public void onNortheast() {
		// TODO Auto-generated method stub
		super.onNortheast();
		ShowSToast("northeast");
	}
	@Override
	public void onNorthwest() {
		// TODO Auto-generated method stub
		super.onNorthwest();
		ShowSToast("northwest");
	}
	@Override
	public void onSoutheast() {
		// TODO Auto-generated method stub
		super.onSoutheast();
		ShowSToast("southeast");
	}
	@Override
	public void onSouthwest() {
		// TODO Auto-generated method stub
		super.onSouthwest();
		ShowSToast("southwest");
	}
	@Override
	public void onTouch() {
		// TODO Auto-generated method stub
		super.onTouch();
	}

	@Override
	public EventType onEventDistribute() {
		// TODO Auto-generated method stub
		return super.onEventDistribute();
	}
	
	
}

BaseActivity 類實現了OnEventListener,並將該類抽象,那麼在子類裡面需要手勢操作 綁定Touch事件即可,通過OnEventListener的回調函數處理手勢操作

 

這是簡單的手勢操作,復雜點的(比如onTouch的沖突)需要涉及到 類:View ViewGroup Activity ,涉及到的方法:dispatchTouchEvent(事件分發) onInterceptTouchEvent(事件攔截) onTouch onTouchEvent..Activity 裡面不含onInterceptTouchEvent方法

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved