Android Realizes Global Right Slide Return

  • 2021-12-04 11:07:50
  • OfStack

At present, the full screen of Android mobile phone is becoming more and more popular, and many applications have already supported the function of sliding right back to the upper level 1 page, so how to realize this function?

First of all, let's talk about the next train of thought, mainly through MotionEvent. Through different treatments of this event, what is the position of the point where PointF is used to monitor and press.

Next, explain 1 to you through the code

Shows a new Gesture this sample 1 class, used to handle, sliding logic.


public class GestureHandler {}

Next, define the 1 representation of the relevant 1 screen width and height and sliding interval


// Screen width and height 
int sWidth = 1280;
int sHeight = 720;
// Points pressed 
PointF down;
//Y Interval of axis sliding 
float minY, maxY;
// Time when pressed 
long downTime;
// Edge determination distance, 
double margin = sWidth * 0.035;
//Y The maximum interval range of the axis, that is, Y Sliding the shaft beyond this range does not trigger an event 
double height = sHeight * 0.2;
//X Shortest sliding distance of shaft  X Axis slip range below this value does not trigger an event 
double width = sWidth * 0.1;
// Is it in this sliding event 
boolean work = false;

Of course, my screen here is like this, and the width and height of the screen are according to my actual situation.

Then use PointF to monitor


public boolean point(PointF up) {
  long upTime = System.currentTimeMillis();
  float tWidth = Math.abs(down.x - up.x);
  if (maxY - minY < height && tWidth > width && (upTime - downTime) / tWidth < 2.5) {
   // The starting point is on the left 
   if (down.x < margin) {
    left();// Left sliding needs to be handled by logical methods 
    return true;
   }
   
  }
  return false;
 }

After it is defined, you can use MontionEvent to do 1 processing


public boolean doEventF(MotionEvent event) {
   switch (event.getActionMasked()) {
   case MotionEvent.ACTION_DOWN:
    // Record the pressed point 
    downTime = System.currentTimeMillis();
    down = new PointF(event.getX(), event.getY());
    minY = maxY = down.y;
    // Determine whether it is in edge sideslip 
    if (down.x < margin || (sWidth - down.x) < margin) work = true;
    break;
   case MotionEvent.ACTION_MOVE:
    // Record slip Y Axial interval 
    if (work)
     if (event.getY() > down.y) {
      maxY = event.getY();
     } else {
      minY = event.getY();
      }
    break;
   case MotionEvent.ACTION_UP:
    if (work) {
     handle(new PointF(event.getX(), event.getY()));
     work = false;
     return true;
    }
    work = false;
  }
  
  return work;
 }

Then here is a left slide to exit the current activity


public void left() {
 
  // Handle the left edge sliding event, here you can write it yourself 1 A ActivityUtil , used for finish Current activity (There are a lot of them on the Internet, so search them casually 1 There will be it next.) 
   
}

After dealing with this, we have to think about 1. How to achieve the global right slide back, then you need to let all activity inherit 1 BaseActivity, and then deal with whether you need to slide back right in this base class.


/**  Gesture monitoring  */
 GestureHandler mGestureHandler;
 /**  Do you need to listen for gesture shutdown function  */
 private boolean mNeedBackGesture = false;

Then one gesture is distributed through one dispatchTouchEvent


@Override
 public boolean dispatchTouchEvent(MotionEvent event){
  //TODO Auto-generated method stub
  if (mNeedBackGesture){
   return mGestureHandler.doEventF(event) || super.dispatchTouchEvent(event);
  }
  return super.dispatchTouchEvent(event);
 }

Set up a gesture monitor to facilitate a special activity setting to exit without sliding, such as the home page


public final void setNeedBackGesture(boolean mNeedBackGesture){
  this.mNeedBackGesture = mNeedBackGesture;
 }

Then when you use it, you only need one Gesture object in onCreate method

If you want to do not return on the home page, you can also set setNeedBackGesrure (false) in the onCreate method

In this way, you have achieved a global right slide back.


Related articles: