Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Android Touch message passing mechanism analysis     - FastDFS installation and deployment (Server)

- Install RAID 6 (Striping double distributed parity) (Linux)

- Analysis JavaBean (Programming)

- Try to use Lets Encrypt (Linux)

- Tomcat configuration memory and remote debug port (Server)

- How to Install Android Studio on Ubuntu 15.04 / CentOS7 (Linux)

- Different versions of MongoDB achieve master-slave replication (Database)

- How to manage KVM virtual environments with command-line tools in Linux (Server)

- Using PHP MySQL library (Programming)

- How to upgrade to Ubuntu 14.04 Ubuntu 14.10 (Linux)

- Use Markdown editor for document work under Linux (Linux)

- Spring WebSocket Comments (Programming)

- Four safety delete files under Linux tools (Linux)

- Android basics summary article (Programming)

- EXP-00091: Exporting questionable statistics Processing Method (Database)

- To install the iNode client on UbuntuKylin 13.10 (Linux)

- Adjustment expand VMDK format VirtualBox disk space (Linux)

- Top command: the Task Manager under linux (Linux)

- MySQL Tutorial: Building MySQL Cluster under Linux (Database)

- Linux process scheduling opportunity (Programming)

 
         
  Android Touch message passing mechanism analysis
     
  Add Date : 2017-07-22      
         
         
         
  In Android, the transfer of control messages mainly through two ways to distribute together with the use of the user's touch message, take a look at the following two methods;

onInterceptTouchEvent: This method is defined in ViewGroup the name suggests, this method is a touch message for ViewGroup intercept (intercept) of;
onTouchEvent: This method is defined in the View, and touch for handling user events;
Let's look at the definition of prototypes of these two methods;

public boolean onInterceptTouchEvent (MotionEvent ev);
public boolean onTouchEvent (MotionEvent event);
Specific information has in common is a parameter of these two methods for obtaining MotionEvent touch events (such as pressing, mobile messaging, etc. morphology) and function return values ​​(for controlling the process touch on different occasions);

Briefly about MotionEvent, it contains the type of the user touches the message, several commonly used touch message type: ACTION_DOWN, ACTION_MOVE, ACTION_UP, ACTION_CANCEL, respectively touch pressed, moving to end state; these types of message type is not concurrent generated, but the same as the order of the touch occurrence ordered produced, DOWN-> MOVE-> UP / CANCEL;

Below an example to study the custom of a simple ViewGroup and View, and onInterceptTouchEvent onTouchEvent for tracking implementation;

LLinearLayout

public class LLinearLayout extends LinearLayout {

    public LLinearLayout (Context context, AttributeSet attrs) {
        super (context, attrs);
    }

    public LLinearLayout (Context context) {
        super (context);
    }

    private float lastY;

    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev) {
        String tag = "onInterceptTouchEvent";
        Log.w (tag, "" + super.onInterceptTouchEvent (ev));
        switch (ev.getAction ()) {
        case MotionEvent.ACTION_DOWN:
            Log.w (tag, "ACTION_DOWN");
            lastY = ev.getX ();
            break;
        case MotionEvent.ACTION_MOVE:
            Log.w (tag, "ACTION_MOVE");
            if (ev.getX () - lastY> 20) {
                Log.w (tag, "ACTION_MOVE> 20");
                return true;
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            Log.w (tag, "ACTION_UP");
            break;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent (MotionEvent event) {
        Log.w ( "onTouchEvent", "" + super.onTouchEvent (event));
        return false;
    }
}
LView

public class LView extends ImageView {

    public LView (Context context, AttributeSet attrs) {
        super (context, attrs);
    }

    public LView (Context context) {
        super (context);
    }

    @Override
    public boolean onTouchEvent (MotionEvent event) {
        String tag = "LView.onTouchEvent";
        Log.e (tag, "" + super.onTouchEvent (event));
        switch (event.getAction ()) {
        case MotionEvent.ACTION_DOWN:
            Log.e (tag, "ACTION_DOWN");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e (tag, "ACTION_MOVE");
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            Log.e (tag, "ACTION_UP");
            break;
        }
        return false;
    }
}
xml file will be added in LView LLinearLayout, change to a series of control exactly;

step1:
Log by the above information, we can see, touch event is first LLinearLayout intercepted (onInterceptTouchEvent), in this Demo, the method returns a value of false, then it will be passed to the event Touch LView, LView of onTouchEvent responded to the touch events, and also returns false; then Touch event before being passed to the onTouchEvent LLinearLayout processing;

We can easily be seen from the above, Touch event was first LLinearLayout intercepted and passed to LView, LView perform onTouchEvent processing logic; then LLinearLayout then perform their onTouchEvent processing logic; @ 1

step2: the onInterceptTouchEvent above method LLinearLayout return value to true, run the program again;

Log compared to the above information, we can see that the message was not delivered to LView; here, you should be able to draw a conclusion a small bar;

ViewGroup inside onInterceptTouchEvent return value Returns true if the event interception Touch, Touch event will no longer be passed to the child inside ViewGroup View;

step3: Back step1, will LView in onTouchEvent return value to true, run the program again, slide your finger from the screen left and right;

From the Log information can be seen, LView of onTouchEvent returns true when, LView touch events passed from DOWN to MOVE, and then passed to UP; of course, the whole process is the first by LLinearLayout onInterceptTouchEvent first received Touch event here and no interceptions Touch event, but the event is passed to the child Touch View; careful friends may find, here, did not perform to the onTouchEvent LLinearLayout method, why in fact, because of onTouchEvent LView event returns true? represents processing consumption of this event, no longer continue to pass, and not to perform the onTouchEvent LLinearLayout method;

Conclusion: View of onTouchEvent return value indicates whether or not to pass on Touch events, such as If true, the form will be transmitted from the touch to MOVE DOWN then UP (where this argument is not really rigorous, here refers to the entire onTouchEvent method return value It is true, and no segment is returned, such as when MOVE forms returned false);

step4: Continue step3, run the program, slide your finger from the screen left and right;

In LLinearLayout of onInterceptTouchEvent if MOVE message to the right sliding distance is greater than 20, then the interception Touch events, so after the event is intercepted, it will not be like step3, the MOVE message will continue to be passed LView of onTouchEvent, whereas is interrupted, touching a finger that is morphological changes to ACTION_UP lift (in fact, this is wrong to say, that is accurate touch morphological changes ACTION_CANCEL, because I will CANCEL uP and treated as a class, as shown above, print it out the Log information ACTION_UP, in fact it is essentially ACTION_CANCEL); then due ACTION_MOVE was blocked, so LLinearLayout of onTouchEevent continue to be invoked when the finger MOVE;

The end of the summary:

ViewGroup Lane onInterceptTouchEvent default value is false, only if the return value is false when Touch event was passed to son View, and then call the child's View onTouchEvent; returns true when the user will intercept Touch event, the sub-View not catch touch events;

View of onTouchEvent method, when the return value is true, the event will continue to be passed down, and then delivered by ACTION_DOWN to ACTION_MOVE ACTION_UP, whereas if the return value is false, only the capture of MotionEvent ACTION_DOWN form;
     
         
         
         
  More:      
 
- SME Linux network security policy server security (Linux)
- Deepin Tutorial: Depth Description Installer expert mode (Linux)
- Linux shell string interception and stitching (Linux)
- MySQL server after an unexpected power outage can not start (Database)
- RedHat Linux 6.4 install Oracle 10g error (Database)
- After reloading the cinder-volume OpenStack not start properly (Server)
- Java regular expression syntax (Programming)
- C ++ two second pointer memory model (two-dimensional array) (Programming)
- The istgt PSD on ported to Mac OS X (Linux)
- Deploy Apache Spark cluster environment in Ubuntu (Server)
- printf PHP string operations () built-in function usage (Programming)
- Use CutyCapt to convert HTML pages to png images on Linux (Linux)
- Those functions under Linux you do not know the df command (Linux)
- Open remote MySQL database connection managed under CentOS (Database)
- How to install Linux on a MacBook Pro Retina (Linux)
- How Datadog monitor Nginx (Server)
- RPM package management tools under Linux (Linux)
- A deep understanding of Java enum (Programming)
- Samba public folder permissions (Server)
- How apt-get limited use IPv4 or IPv6 protocol to download (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.