Today, a sudden whim, want to add a right to the project inside the function of the interface. The idea is simple is to intercept the screen slide event, and then finish the current page, not much to say, on the code bar.
The first step: calculate the sliding speed using Velocitytracker and some accompanying code.
Private Velocitytracker Mvelocitytracker = null;//Speed Recorder
private int getscrollvelocity () {
Mvelocitytracker.computecurrentvelocity (1000);
int velocity = (int) mvelocitytracker.getxvelocity ();
return math.abs (velocity);
}
/**
* Reclaim Velocitytracker objects.
*/
private void Recyclevelocitytracker () {
Mvelocitytracker.recycle ();
Mvelocitytracker = null;
}
/**
* Create Velocitytracker objects and add sliding events that touch the content interface to the Velocitytracker.
*
* @param Event
*
*/
private void Createvelocitytracker (Motionevent event) {
if (Mvelocitytracker = = null) {
Mvelocitytracker = Velocitytracker.obtain ();
}
Mvelocitytracker.addmovement (event);
}
The second step: in the activity to intercept the sliding event, note that if the rewrite touchevent is first to the bounds of the face control before it is passed to this method, it may cause only a few parts of the interface to get to the sliding event.
Private float xdown, xmove;
The minimum speed when the finger is sliding to the right
private static final int xspeed_min = 200;
The minimum distance when the finger is sliding to the right
private static final int xdistance_min = 150;
@Override
public boolean dispatchtouchevent (Motionevent ev) {
TODO auto-generated Method Stub
Createvelocitytracker (EV);
Switch (ev.getaction ()) {
Case Motionevent.action_down:
Xdown = Ev.getrawx ();
Break
Case Motionevent.action_move:
Xmove = Ev.getrawx ();
Distance of activity
int Distancex = (int) (xmove-xdown);
Get Time speed
int xspeed = getscrollvelocity ();
if (Distancex > Xdistance_min && xspeed >xspeed_min) {
finish ();
}
Break
Case MOTIONEVENT.ACTION_UP:
Recyclevelocitytracker ();
Break
Default
Break
}
return super.dispatchtouchevent (EV);
}
This article is from the "Android Development record" blog, so be sure to keep this source http://dyxiaoyu.blog.51cto.com/5805904/1439473