Simple implementation of gesture Sliding Monitoring in Android, the source of PHP live platform

Posted by shadownet on Mon, 29 Jun 2020 09:45:43 +0200

realization
Declaration and creation of gesture monitor
The statement is as follows:

private GestureDetector detector = null;// Declare a gesture monitor

Create the following:

// Create a GestureDetector object and override the relevant methods
        detector = new GestureDetector(this, new GestureDetector.OnGestureListener() {

            @Override
            public boolean onDown(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                  Log.e("TAG", "onScroll");
                  Log.e("TAG", "onScroll distanceX = " + distanceX);
                  Log.e("TAG", "onScroll distanceY = " + distanceY);

                //Distancey > 0 means it's slipping up
                if (distanceY > 0){
                    
                }
                
                // Distancey > 0 means a decline
                if (distanceY < 0){
                    
                }
                
				// It means left slip
                if(distanceX > 0){
                    Log.e("TAG", "It means it slipped to the left");
                }

                // It means it's slipping to the right
                if(distanceX < 0){
                    Log.e("TAG", "It means it's sliding to the right");
                }

                return true;// Events are consumed and will not be passed on
            }

            @Override
            public void onLongPress(MotionEvent e) {

            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

                // The method needs to be recalled only after the user's fingers touch the screen for a little fast sliding and the fingers are raised
                // If it's slow, the method doesn't call back (which may explain the meaning of flying)
                Log.e("TAG", "onFling");

                return true;// The event has been consumed and will not continue to be delivered
            }
        });

In the code created above, I added some key comments to the rewriting method. It is suggested that students should understand it, which is helpful to deepen their understanding.

Description of the rewritten method onScroll()
First, look at the source code of this method

/**
         * Notified when a scroll occurs with the initial on down {@link MotionEvent} and the
         * current move {@link MotionEvent}. The distance in x and y is also supplied for
         * convenience.
         *
         * @param e1 The first down motion event that started the scrolling.
         * @param e2 The move motion event that triggered the current onScroll.
         * @param distanceX The distance along the X axis that has been scrolled since the last
         *              call to onScroll. This is NOT the distance between {@code e1}
         *              and {@code e2}.
         * @param distanceY The distance along the Y axis that has been scrolled since the last
         *              call to onScroll. This is NOT the distance between {@code e1}
         *              and {@code e2}.
         * @return true if the event is consumed, else false
         */
        boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);

The source code says: when you touch the screen and start to slide, this method will be triggered, and the interpretation of relevant parameters is relatively simple and easy to understand

Here, a specific direction of gesture sliding is determined according to the value of distanceX and distanceY

The distanceX parameter is used to judge a slide in the left and right directions. When the value is greater than 0, the gesture moves left, and when the value is less than 0, the gesture slides to the right
The distanceY parameter is used to judge a slide in the up and down direction. When the value is greater than 0, it means that the gesture moves up, and when the value is less than 0, it means the gesture slides down
However, my judgment in the onScroll() method is only rough (only one of the parameters is used for judgment). If students want to accurately represent left-right sliding or up-down sliding, they need to use two parameters (to achieve accurate judgment by constraining the value of parameters)

Gesture monitor takes over the touch screen event of View
Because some of the top-level parent classes of layouts or controls are actually views, I'll take View as an example.

Since View has touch screen events, you must set touch events for View, as follows:

// Display settings the following controls can be clicked
linear.setClickable(true);
// Linear layout settings touch listening event
linear.setOnTouchListener(new View.OnTouchListener() {
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // Gesture listeners analyze touch screen events for a given view (in this case, the linearLayout layout)
        // It is equivalent to the gesture monitor taking over the touch screen event of this view
        return detector.onTouchEvent(event);
    }
});

Return in the code of touch event monitoring detector.onTouchEvent(event); indicates that the gesture listener is allowed to consume the touch screen event, which is equivalent to that the gesture listener takes over the event.

Note: remember to set the clickable property of View to true, so that the gesture listener object can successfully consume the touch-screen event of this View

Topics: less