Android Touch Event

Source: Internet
Author: User

Ontouchlistener Use
 Public  class viewactivity extends Activity implements View. Ontouchlistener {    @Override    protected void onCreate(Bundle savedinstancestate) {requestwindowfeature (window.feature_no_title);Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.view_gestrue);        LinearLayout Ll_context = (linearlayout) Findviewbyid (R.id.ll_context); Ll_context.setontouchlistener ( This); }@Override     Public Boolean OnTouch(View V, motionevent event) {Switch(Event.getactionmasked ()) { CaseMOTIONEVENT.ACTION_DOWN:LOG.V (" --","fingers touch the screen."); Break; CaseMOTIONEVENT.ACTION_MOVE:LOG.V (" --","The fingers are moving."); Break; CaseMOTIONEVENT.ACTION_OUTSIDE:LOG.V (" --","finger out of view boundary"); Break; CaseMOTIONEVENT.ACTION_UP:LOG.V (" --","finger off the screen"); Break; CaseMOTIONEVENT.ACTION_SCROLL:LOG.V (" --","fingers are sliding."); Break; CaseMOTIONEVENT.ACTION_POINTER_DOWN:LOG.V (" --","The non-master finger touches the screen"); Break; CaseMOTIONEVENT.ACTION_POINTER_UP:LOG.V (" --","non-master finger left screen"); Break; }return true;//Intercept Events}}
tip:motionevent Frequent usage
event.getY();//触碰点获取距离v顶部的距离event.getRawY();//触碰点获取距离屏幕顶部的距离event.getPointerCount();//获取触碰手指的个数
Gesturedetector using:
//GestureDetector这个类对外提供了两个接口:OnGestureListener,OnDoubleTapListener,另一个内部类SimpleOnGestureListener。

Gesturedetector.ongesturelistener interface: Used to notify ordinary gesture events, the interface has six callback functions such as the following:

1.onDown (motionevent E): down event;

2.onSingleTapUp (motionevent E): One click Up event, no swipe after touch
(onscroll), without long pressing (onlongpress), and then triggering when touchup.
Click on the quick (don't swipe) Touchup:
Ondown->onsingletapup->onsingletapconfirmed
Click on a slightly slower (not sliding) Touchup:
Ondown->onshowpress->onsingletapup->onsingletapconfirmed

3.onShowPress (motionevent E): When the Down event occurs and move or up does not occur before the event is triggered. Touch has not yet been triggered when sliding (with Ondown. onlongpress) Ondown only touch down must be triggered immediately.

and touchdown after a while not sliding first triggered onshowpress and then onlongpress.

So the touchdown has not slipped after
triggered in accordance with the ondown->onshowpress->onlongpress order.

4.onLongPress (motionevent E): Long press event. Touch does not move always touch down when triggered

5.onFling (motionevent E1, motionevent E2, float Velocityx, float velocityy): Swipe gesture event. Touch after sliding a little distance, the action_up will only trigger the number of references: E1 1th Action_down motionevent and only one; E2 last Action_move motionevent; Velocityx The speed of movement on the x-axis, in pixels per second. Velocityy movement speed on the y-axis, pixels per second. Trigger condition: The x-axis coordinate displacement is greater than fling_min_distance and moves faster than fling_min_velocity pixels per second

6.onScroll (motionevent E1, motionevent E2, float Distancex, float Distancey): Drag events on the screen.

Whether you drag the view with your hand, or if you scroll with a throw, it will trigger multiple times, which will trigger when the Action_move action occurs.
Throw: After the finger touches the screen, slightly swipe and release immediately

Gesturedetector.ondoubletaplistener interface: Used to notify the Doubletap event, similar to the mouse double-click event.

1.onDoubleTap (motionevent E): Second under double-click. Triggered when Touch is down.


2.onDoubleTapEvent (motionevent E): Notifies the event in the Doubletap gesture. Includes down, up, and move events (this refers to events that occur between double-clicks, such as double-clicking in the same place generates DOUBLETAP gestures, and the down and up events in the Doubletap gesture, which are notified by the function). Double-click the second touch down and up are triggered and can be differentiated by e.getaction ().
3.onSingleTapConfirmed (motionevent E): Used to determine that the click is Singletap instead of DOUBLETAP, assuming that two consecutive clicks are doubletap gestures. Let's say just one click. The system waits for a period of time and does not receive a second click to determine that the click is Singletap instead of DOUBLETAP and then triggers the Singletapconfirmed event.

This method is different from Onsingletapup, he is in Gesturedetector convinced that the user after the first touch of the screen, not immediately following the second touch of the screen. That is, not a "double click" trigger.

Tip:
//distanceX。是前后两次call的X距离,不是e2与e1的水平距离; 是前后两次call的Y距离,不是e2与e1的垂直距离
Usage:

1: Inheritance Gesturedetector.ongesturelistener
2: Instantiate the detector gesturedetector mdetector = new Gesturedetector (viewactivity.this, this);
3: Callback method in Ontouch

@OverridepubliconTouchevent) {  mDetector.onTouchEvent(event);  returntrue;}

References Link:
User gesture detection-gesturedetector use specific explanations
Android Touch, first understand motionevent

Android Touch Event

Related Article

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.