Android onTouchEvent event, androidtouchevent
Determine the sliding direction based on the user's touch, and select pop-up popupWindow or dialog. You can also switch the interface to define the switching animation.
Interface Definition:
/*** According to the Angle of the sliding range sliding ction * @ author LanYan **/public interface OnEventListener {/*** optional */void onLeft (); /***** success */void onTop ();/***** → */void onRight ();/***** success */void onBottom (); /*****/void onNortheast ();/*****/void onNorthwest ();/*****/void onSouthwest (); /*****/void onSoutheast ();/***** touch screen */void onTouch ();/***** event distribution */EventType oneventdistriast ();
Method call tool class:
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 stubboolean 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 outer: [157.5, 202.5] Top Priority: [67.5, 112.5] Right * →: [337.5, 360] & [0, 22.5] Bottom weight: [247.5, 292.5] Northwest: * [112.5, 157.5] zookeeper was wrongly formed when there were too many zookeeper whose Southeast: [292.5, 337.5] was * Southwest: [202.5, 247.5] was Northeast: [22.5, 67.5] Why? * VelocityXY ------------- Why? */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) {// the x and y values of two points 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); // obtain the radian double angle = 180/(Math. PI/radian); // use radians to calculate the angle if (y <0) {angle = 180 + (180-angle);} else if (y = 0) & (x <0) {angle = 180;} return angle ;} /*** get the distance between two points ** @ 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,/*** events do not distribute down */normal, /*** completely distribute downward */complete }}
BaseActivity class:
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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setTheme();setContentView(getLayoutResID());configClass();initialization();}public abstract int getLayoutResID();public void setTheme(){}public void configClass(){}public void initialization(){}/***********************************onTouch*********************************************/@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubif(OnEventTouchFactory.getInstance(this).onTouch(v, event)){return true;}return false;}@Overridepublic void onLeft() {// TODO Auto-generated method stub}@Overridepublic void onTop() {// TODO Auto-generated method stub}@Overridepublic void onRight() {// TODO Auto-generated method stub}@Overridepublic void onBottom() {// TODO Auto-generated method stub}@Overridepublic void onNortheast() {// TODO Auto-generated method stub}@Overridepublic void onNorthwest() {// TODO Auto-generated method stub}@Overridepublic void onSouthwest() {// TODO Auto-generated method stub}@Overridepublic void onSoutheast() {// TODO Auto-generated method stub}@Overridepublic void onTouch() {// TODO Auto-generated method stub}@Overridepublic EventType onEventDistribute() {// TODO Auto-generated method stubreturn 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();}}
Test 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 {@Overridepublic int getLayoutResID() {// TODO Auto-generated method stubreturn R.layout.activity_main;}@Overridepublic void configClass() {// TODO Auto-generated method stubsuper.configClass();}@Overridepublic void initialization() {// TODO Auto-generated method stubsuper.initialization();RelativeLayout parent=(RelativeLayout)findViewById(R.id.parent);TextView child=(TextView)findViewById(R.id.child);child.setMovementMethod(ScrollingMovementMethod.getInstance()); parent.setOnTouchListener(this);}@Overridepublic void onRight() {// TODO Auto-generated method stubsuper.onRight();ShowSToast("right");}@Overridepublic void onTop() {// TODO Auto-generated method stubsuper.onTop();ShowSToast("top");}@Overridepublic void onLeft() {// TODO Auto-generated method stubsuper.onLeft();ShowSToast("left");}@Overridepublic void onBottom() {// TODO Auto-generated method stubsuper.onBottom();ShowSToast("bottom");}@Overridepublic void onNortheast() {// TODO Auto-generated method stubsuper.onNortheast();ShowSToast("northeast");}@Overridepublic void onNorthwest() {// TODO Auto-generated method stubsuper.onNorthwest();ShowSToast("northwest");}@Overridepublic void onSoutheast() {// TODO Auto-generated method stubsuper.onSoutheast();ShowSToast("southeast");}@Overridepublic void onSouthwest() {// TODO Auto-generated method stubsuper.onSouthwest();ShowSToast("southwest");}@Overridepublic void onTouch() {// TODO Auto-generated method stubsuper.onTouch();}@Overridepublic EventType onEventDistribute() {// TODO Auto-generated method stubreturn super.onEventDistribute();}}
The BaseActivity class implements OnEventListener and abstracts this class. In this case, you need to use a gesture to bind the Touch event in the subclass. You can use the OnEventListener callback function to process the gesture operation.
This is a simple gesture operation. complex operations (such as onTouch conflicts) Involve classes: View ViewGroup Activity, and the method involved: dispatchTouchEvent (event distribution) onInterceptTouchEvent (event interception) onTouch onTouchEvent .. the onInterceptTouchEvent method is not included in the Activity.