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 Common Methods
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 the following six callback functions:

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 very fast (do not 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 is not triggered when sliding (compared to ondown,onlongpress) Ondown as long as 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
Trigger in the order of ondown->onshowpress->onlongpress.

4.onLongPress (motionevent E): Long press event, touch no move always touch down when triggered

5.onFling (motionevent E1, motionevent E2, float Velocityx, float velocityy): swipe gesture event; When you swipe a little distance, the parameter is triggered at ACTION_UP: E1 1th Action_down motionevent and only one; E2 the last action_move motionevent; movement speed on the x-axis, pixels/sec; movement speed on the Velocityx y-axis, pixels/ seconds. Trigger condition: Coordinate displacement of x-axis is greater than fling_min_distance, and movement speed is greater than fling_min_velocity pixels/second

6.onScroll (motionevent E1, motionevent E2, float Distancex, float Distancey): Drag events on the screen. Whether you drag a view by hand, or if you scroll with a throw, it triggers multiple times, and this method triggers when the Action_move action occurs.
Throw: After the finger touches the screen, slightly swipe and release immediately

Gesturedetector.ondoubletaplistener Interface: A double-click event that is used to notify the Doubletap event, similar to a mouse.

1.onDoubleTap (motionevent E): triggered when Touch down is second on double-click.
2.onDoubleTapEvent (motionevent E): Notifies events in the Doubletap gesture, including 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, In the doubletap gesture there will also be a down and up event, both of which are notified by the function, and the second touch down and up of the double-click are triggered and can be distinguished by e.getaction ().
3.onSingleTapConfirmed (motionevent E): Used to determine that the click is Singletap instead of DOUBLETAP, if the consecutive click is Doubletap gesture, if only 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 screen, that is, not "double-click" When the trigger

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

1: Inheritance Gesturedetector.ongesturelistener
2: Instantiation Detector Gesturedetector mdetector = new Gesturedetector (viewactivity.this, this);
3: Callback method in Ontouch

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

Reference Links:
User gesture Detection-gesturedetector use detailed
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.