The minimum speed when the finger slides up and down
private static final int yspeed_min = 1000;
The minimum distance when the finger is sliding to the right
private static final int xdistance_min = 50;
The minimum distance when the finger slides upward or falls
private static final int ydistance_min = 100;
Record the horizontal axis of the finger when it is pressed.
private float Xdown;
Records the ordinate when the finger is pressed.
private float Ydown;
Record the horizontal axis of the finger when moving.
private float Xmove;
Records the ordinate when the finger moves.
private float Ymove;
Used to calculate the speed of finger slippage.
Private Velocitytracker Mvelocitytracker;
@Override
public boolean dispatchtouchevent (Motionevent event) {
Createvelocitytracker (event);
Switch (event.getaction ()) {
Case Motionevent.action_down:
Xdown = Event.getrawx ();
Ydown = Event.getrawy ();
Break
Case Motionevent.action_move:
Xmove = Event.getrawx ();
Ymove = Event.getrawy ();
Distance to Slide
int Distancex = (int) (xmove-xdown);
int distancey = (int) (ymove-ydown);
Get Time speed
int yspeed = getscrollvelocity ();
The following conditions must be met to close activity:
Distance >xdistance_min of 1.x axis sliding
2.y axis sliding distance within the Ydistance_min range
On the 3.Y axis (that is, the speed up and down) <xspeed_min, if it is greater than the user intent is to swipe up and down rather than left to end activity
if (Distancex > Xdistance_min
&& (Distancey < ydistance_min && Distancey >-ydistance_min)
&& Yspeed < Yspeed_min) {
Overridependingtransition (r.anim.push_right_in,
R.anim.push_right_out);
Finish ();
}
Break
Case MOTIONEVENT.ACTION_UP:
Recyclevelocitytracker ();
Break
Default
Break
}
Return Super.dispatchtouchevent (event);
}
/**
* Create a Velocitytracker object and add the swipe events of the touch interface to the Velocitytracker.
*
* @param Event
*
*/
private void Createvelocitytracker (Motionevent event) {
if (Mvelocitytracker = = null) {
Mvelocitytracker = Velocitytracker.obtain ();
}
Mvelocitytracker.addmovement (event);
}
/**
* Reclaim Velocitytracker objects.
*/
private void Recyclevelocitytracker () {
Mvelocitytracker.recycle ();
Mvelocitytracker = null;
}
/**
*
* @return sliding speed, in units of how many pixels are moved per second.
*/
private int getscrollvelocity () {
Mvelocitytracker.computecurrentvelocity (1000);
int velocity = (int) mvelocitytracker.getyvelocity ();
return math.abs (velocity);
}
Swipe to close activity