iOS development slide right back to the previous page function (demo) _android

Source: Internet
Author: User

In iOS7, Apple's original eco-apps are almost always able to go back to the previous page by sliding to the right, which prevents users from using their thumb to click on the remote Return key (the return of the iphone5 is an anti-human design) in a single hand operation. But now Android's mobile phone market is almost impossible to find a phone less than 4 inch screen, almost all applications are by clicking the back of the upper left corner to the previous page, if one hand with a large screen mobile phone, I think it will go crazy. It took a little time to write a demo that slides right back.

The effect of the following figure:

This figure slides to the right and returns to the first activity after clicking the first activity to jump to the second activity.

First, design ideas

1, let the root layout of an activity implement the Ontouchlistener interface, and when the finger slides to the right on the screen, we write down the position of the x axis of the Action_down, when the finger slides (that is, the Action_move), and gets the position of the x-axis when the slide is made, When the sliding position is greater than a critical value and the speed in this direction is greater than a critical value, we assume that the user's intention to slide the finger is to return to the previous page.

2, by following this line of thinking, we can make this function by sliding to the right, but it will feel very stiff, without any excessive return to the previous page, for this reason, the activity plus the start and end of the excessive animation. This is the realization of the idea, very simple, look at the code below.

Second, the main code

In this demo, I wrote three activity,activity1 can jump to Activity2,activity2 can jump to Activity3,activity3 can slide back to Activity2, Activity2 can slide back to activity1. The main logical code is in the Activity2, and the code is as follows:

Package Org.sunday.slidingreturn; 
Import COM.EXAMPLE.SLIDINGRETURN.R; 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import android.view.MotionEvent; 
Import Android.view.VelocityTracker; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.view.View.OnTouchListener; 
Import Android.widget.Button; 
Import Android.widget.LinearLayout;  /** * * @author Sunday * 2014-1-4 * zhengchao1937@163.com/public class Secondactivity extends activity implements 
Ontouchlistener {//The minimum speed at which the finger slides to the right the private static final int xspeed_min = 200; 
The minimum distance when the finger slides to the right private static final int xdistance_min = 150; 
Record the horizontal axis when the finger is pressed. 
private float Xdown; 
Record the horizontal axis when the finger moves. 
private float Xmove; 
Used to calculate the speed at which a finger slides. 
Private Velocitytracker Mvelocitytracker; 
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_second); Button btn = (button) Findviewbyid (r.id.Btn_second); Btn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {startactivity (new Intent (Seconda 
Ctivity.this, Thirdactivity.class)); 
Set toggle animation, enter from right, left exit Overridependingtransition (R.anim.in_from_right, r.anim.out_to_left); 
} 
}); 
LinearLayout ll = (linearlayout) Findviewbyid (R.id.ll_second); 
Ll.setontouchlistener (this); //Reprint Please indicate the source: http://blog.csdn.net/ff20081528/article/details/17845753 @Override public boolean Ontouch (View V, motione 
Vent event) {Createvelocitytracker (event); 
Switch (event.getaction ()) {Case MotionEvent.ACTION_DOWN:xDown = event.getrawx (); 
Break 
Case MotionEvent.ACTION_MOVE:xMove = EVENT.GETRAWX (); 
Active distance int Distancex = (int) (xmove-xdown); 
Get speed int xspeed = getscrollvelocity (); When the sliding distance is greater than the minimum distance we set and the instantaneous speed of the slide is greater than the speed we set, return to the previous activity if (Distancex > Xdistance_min && xspeed > Xspeed_ 
MIN) {finish (); 
} break; 
Case MotionEvent.ACTION_UP:recycleVelocityTracker (); 
Break 
DefaultBreak 
return true; 
/** * Creates the Velocitytracker object and joins the sliding event of the touch content interface into the Velocitytracker. * * @param event */private void Createvelocitytracker (Motionevent event) {if (Mvelocitytracker = null) {Mveloc 
Itytracker = Velocitytracker.obtain (); 
} mvelocitytracker.addmovement (event); 
/** * Reclaims Velocitytracker objects. 
* * private void Recyclevelocitytracker () {mvelocitytracker.recycle (); 
Mvelocitytracker = null; 
/** * Gets the speed at which the finger slides at the content interface. 
* * @return sliding speed, the number of pixels moved per second in units. 
* * Private int getscrollvelocity () {mvelocitytracker.computecurrentvelocity (1000); 
int velocity = (int) mvelocitytracker.getxvelocity (); 
return math.abs (velocity);  } 
}

Note I write very clearly, you can download the demo directly to see very simple, this demo in the sliding return of the implementation and activity of the coupling is very high, today's main idea of a good process, in a few days will be this abstraction into a component to use.

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.