Event mechanism distribution and mechanism distribution

Source: Internet
Author: User

Event mechanism distribution and mechanism distribution
Related API1. MotionEvent: Touch screen event

Int ACTION_DOWN = 0: Indicates down

Int ACTION_MOVE = 2; indicates move

Int ACTION_UP = 1: Indicates up

GetAction (): Get the event type value.

GetX (): Obtain the X axis coordinates of the event (relative to the current view)

GetRawX (): Obtain the X axis coordinates of the event (relative to the left vertex of the screen)

GetY (): Get the Y coordinate of the event (relative to the current view)

GetRawY (): Obtain the Y axis coordinates of the event (relative to the left vertex of the screen)

2. Activity

Boolean dispatchTouchEvent (MotionEvent event): distribution event

Boolean onTouchEvent (MotionEvent event): handles event callbacks.

3. View

Boolean dispatchTouchEvent (MotionEvent event): distribution event

Boolean onTouchEvent (MotionEvent event): callback method for processing events

Void setOnTouchListener (OnTouchListener l): sets the event listener.

Void setOnClickListener (OnClickListener l): Set click listening

Void setOnLongClickListener (OnLongClickListener l)

Void setOnCreateContextMenuListener
(OnCreateContextMenuListener l): used to create a menu.

4. ViewGroup

Boolean dispatchTouchEvent (MotionEvent ev): distribution event

Boolean onInterceptTouchEvent (MotionEvent ev): The callback method of the interception event.

Distribution and handling of touch events

Event generation sequence: down-> move... -> Up

After an event object is created by the system, it will first call the dispatchTouchEvent () of the corresponding Activity object for distribution.

In the process of distributing down to view objects, determine whether the consumer (onTouchEvent () returns true). If both return false, the consumer of the event can only be Activity.

After the move and up events, the events are distributed to the consumer (which may be a view object or Activity) for processing. If the view is not consumed, it is directly sent to the Activity for consumption.

Each event requires a consumer.

View-related methods used in case of image Movement

Int getLeft ()

Obtain the X axis coordinates of the Left vertex of the current view relative to the parent view.

int getTop()

Obtain the Y axis coordinate of the Left vertex of the current view relative to the parent view.

int getRight()

Obtain the x-axis coordinate of the point in the lower right corner of the current view relative to the parent view.

int getBottom()

Obtain the Y axis coordinate of the point in the lower right corner of the current view relative to the parent view.

layout(int left, int top, int right, int bottom) :

Dynamically specify the positioning of the current view in the parent view. The parameter is the coordinate of the parent view.

ViewParent getParent() :

Obtains the parent View object of the current View.
Easy Layout
   
Move the imageview and set the drag range
Private ImageView iv_main; private RelativeLayout parentView; private int maxRight; private int maxBottom; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); iv_main = (ImageView) findViewById (R. id. image); // get the parent layout parentView = (RelativeLayout) iv_main.getParent (); iv_main.setOnTouchListener (this);} private int lastX; private int lastY; @ Overridepublic boolean onTouch (View v, motionEvent) {// obtain the event coordinate int eventX = (int) event. getRawX (); int eventY = (int) event. getRawY (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: // lastX, lastY lastX = eventX; lastY = eventY; break; case MotionEvent. ACTION_MOVE: if (maxRight = 0) {maxRight = parentView. getRight (); maxBottom = parentView. getBottom () ;}// calculate the event offset int dx = eventX-lastX; int dy = eventY-lastY; // move the ImageView int left = iv_main.getLeft () according to the event offset () + dx; int right = iv_main.getRight () + dx; int top = iv_main.getTop () + dy; int bottom = iv_main.getBottom () + dy; // restrict left if (left <0) {right + =-left; left = 0;} // top if (top <0) {bottom + =-top; top = 0 ;} // restrict right if (right> maxRight) {left-= right-maxRight; right = maxRight;} // restrict bottom if (bottom> maxBottom) {top-= bottom-maxBottom; bottom = maxBottom;} iv_main.layout (left, top, right, bottom); lastY = eventY; lastX = eventX; break;} return true; // The event is consumed and processed by the subview }}
Button operations Understanding Basic operation type

Down: press your finger;
Up: move your finger away from the button

Key operation order: down? Down? Down ?... -> Up

The system will create a KeyEvent object for any operation on the key.

Button long-press listener: If the button is not up for a certain period of time after the button is down, the listener callback will be triggered.

Related APIs KeyEvent

Int ACTION_DOWN = 0: constant that identifies down

Int ACTION_UP = 1: constant that identifies up

Int getAction (): obtains the event type.

Int getKeyCode (): Obtain the key code (unique identifier) of the key)

StartTracking (): tracking events for Long-pressed listeners

Activity

Boolean dispatchKeyEvent (KeyEvent event): distribution event

Boolean onKeyDown (int keyCode, KeyEvent event): callback by pressing the button

Boolean onKeyUp (int keyCode, KeyEvent event): Release the button callback.

Boolean onKeyLongPress (int keyCode, KeyEvent event): Long-pressed callback

Case: Click the return key to exit the program two seconds later.
// Whether to exit private boolean isExit = false; Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {if (msg. what = 1) {isExit = false;} super. handleMessage (msg) ;}}; @ Overridepublic boolean onKeyUp (int keyCode, KeyEvent event) {if (! IsExit) {// do not leave isExit = true; Toast. makeText (this, "Click again to exit the program", Toast. LENGTH_SHORT ). show (); handler. sendEmptyMessageDelayed (1, 2000); // two seconds later, return true;} return super. onKeyUp (keyCode, event );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.