Android right slide back to previous interface

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.