Android Project---Touchlistener

Source: Internet
Author: User

public static interface

View.ontouchlistener
Android.view.View.OnTouchListener
Known Indirect subclasses Autoscrollhelper, Listviewautoscrollhelper, Zoombuttonscontroller
Class Overview

Interface definition for a callback to being invoked when a touch event is the dispatched to this view. The callback'll be invoked before the touch event was given to the view.

Summary
Public Methods
Abstract Boolean OnTouch (View V, motionevent event) Called when a touch event was dispatched to a view.

Using this class, it is generally in the Android project that gets the focus of the hand tap screen.

If the actual control on the operation, the Touchlistener event is generally presented separately, written as a tool class.

I. Define TOUCHLISTENER Tool class

 Public classTouchlistener implements Ontouchlistener {context context; floatolddist; floatnewdist; floatOldY; floatNewy; List<Float> list =NewArraylist<float>();    Boolean On_off;  PublicTouchlistener (Context context) { This. Context =context; } @Override PublicBoolean OnTouch (View V, motioneventEvent) {        floatF2 =Event. GETRAWX ();        List.add (F2); Olddist= list.Get(0);//In the collection, and because the Ontouch method will continue to fall, this value will continue to be added to the collection.        Switch(Event. Getaction () & Motionevent.action_mask) { CaseMotionEvent.ACTION_DOWN:System. out. println (" Down"); OldY=Event. GetY (); Break;  CaseMotionEvent.ACTION_UP:System. out. println (" up"); Newdist=Event. GETRAWX ();if(Olddist < -&& newdist-olddist > $) {Intent Intent =NewIntent (); Intent.setaction ("Open");                Context.sendbroadcast (Intent); Homeactivity.isopen=true; }            if(Newdist-olddist <- $|| (Newdist-olddist >0&& Newdist-olddist < $&& Olddist < -)) {Intent Intent =NewIntent (); Intent.setaction (" Back");            Context.sendbroadcast (Intent); }            if(Newdist-olddist <0&& newdist-olddist >- $&&homeactivity.isopen) {Intent Intent =NewIntent (); Intent.setaction ("Open");                Context.sendbroadcast (Intent); Homeactivity.isopen=true; } Else{List=NewArraylist<float>();Newy =Event. GetY ();                if(Math.Abs (Newy-oldy) < +&& Math.Abs (newdist-olddist) > +) {return true; } Else{return false; }            }             Break;  CaseMotionEvent.ACTION_MOVE:System. out. println ("MOVE"); Newdist=Event. GETRAWX (); floatDistance = newdist-olddist; Intent Intent=NewIntent (); Intent.putextra ("Distance", distance); Intent.putextra (" Old", olddist); Intent.setaction ("Move");            Context.sendbroadcast (Intent); LOG.I ("Info","MOVE");  Break; }        return true; }}

Note: Be sure to write a break for each case ...

Action_mask is applied to multi-touch operations in Android, literally meaning the action mask.

In the Ontouchevent (Motionevent event)

Use switch (event.getaction ()) to handle Action_down and ACTION_UP events

Use switch (event.getaction () & Motionevent.action_mask) to handle Action_pointer_down and ACTION_POINTER_UP events that handle multi-touch.

Action_down and Action_up is a single touch screen, press down and release the operation;
Action_pointer_down and ACTION_POINTER_UP are multi-touch screens, and when one finger presses down, the other finger presses and releases the action to capture;
Action_move is the operation of the finger moving on the screen;

There is a problem in Touchlistener that the return value of false and true differs when overriding the Ontouch method.

If False, the button control displays the Down,up,move value correctly, and the other controls can only display the down event

If true, the value is constantly being taken.

Event.getx ();//refers to the x-coordinate of the control

EVENT.GETRAWX ();//refers to the x-coordinate of the screen

II. Reception of Broadcasts

Broadcastreceiver receiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {if(Intent.getaction (). Equals ("Open")) {                NewAsyncMove2 (). Execute (); }            if(Intent.getaction (). Equals (" Back") ) {IsOpen=false; NewAsyncMove2 (). Execute (); }            if(Intent.getaction (). Equals ("Move")) {                floatf = Intent.getfloatextra ("Distance",0); floatOld = Intent.getfloatextra (" Old",0); Linearlayout.layoutparams LP=(Layoutparams) left_menu. Getlayoutparams (); if(IsOpen && F <0) {Lp.leftmargin= (int) F;                } Else if(!isopen && F >0&& Old < -) {Lp.leftmargin= (int) (-left_menu.getwidth () +f);} left_menu.setlayoutparams (LP); }        }    };

III. Registration of broadcasting

New Intentfilter ();        Inf.addaction ("open");        Inf.addaction ("back");        Inf.addaction ("move");        Registerreceiver (receiver, INF);

Algorithmic thinking about the Ontouch event:
Get action Multi-touch
If it is pressed
to get the y-axis value of the control if it is pressed
if it is lifted
gets the value of the new X on the screen obtained when the finger is lifted
If your finger is pressed close to the left of the screen in 100 pixels, and the value of the new position minus the old position is greater than 200 (swipe from left to right)
Open the menu bar (send open broadcast to activity)
If the new position value minus the old position value is less than 200 (swipe from right to left) or < old and new position value difference is greater than 0 and new subtraction is less than 200 (sliding from left to right is too short) and is within 100 pixels from the screen;
Close the menu bar (send close broadcast to activity)
If the menu is now open and is sliding from right to left And the sliding distance is very short (less than 200) the menu is re-ejected
Open menu bar
Otherwise if the control y-axis changes greater than 50 it is assumed that he is going up and down the ListView does not go to the left and right switch operation
If the gesture is curved and the arc is smooth, it is understood to be left and right
Open Menu
If the gesture is curved and the arc is steep, it is understood to be sliding up and down without executing the open close menu operation that says the user is sliding the ListView
Close Menu
If it is moving
get the x value of the screen
get moved distance
Send broadcast

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.