Android uses GestureDetector to implement a gesture slide to close the page.

Source: Internet
Author: User

 

This feature allows you to register a logon page on a mobile phone similar to Android. When a gesture slides down, the page is closed.

Use GestureDetector to achieve this effect. When the gesture slides on the screen, the onFling method is used. Therefore, you can make a judgment and operation in this method.

Well, as an old programmer, I will not talk about the principle. I believe you will understand it. If you have any questions, please leave a message or contact me. I will reply immediately.

The effect is as follows:

Private GestureDetector mGestureDetector; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. layout_login); mGestureDetector = new GestureDetector (this, new GestureDetector. simpleOnGestureListener () {@ Override public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {// if (Math. abs (e1.getRawX ()-e2.getRawX ()> 250) {// System. out. println ("the horizontal movement distance is too large"); // return true; //} if (Math. abs (velocityY) <100) {// System. out. println ("fingers moving too slowly"); return true;} // gesture down if (e2.getRawY ()-e1.getRawY ()> 200) {finish (); // disable return true here;} // gesture up if (e1.getRawY ()-e2.getRawY ()> 200) {return true;} return super. onFling (e1, e2, velocityX, velocityY );}});}

2. Similarly, if you want to control the Left and Right slides, you can use the getRawX () method directly.

3. Implement the animation effect. Rewrite the animation effects of pumping and pulling down. You can use a method that comes with Android to call the finish () method:

OverridePendingTransition (R. anim. slide_up_in, R. anim. slide_up_out );

R. anim. slide_up_out

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="200"        android:fromYDelta="0%"        android:interpolator="@android:anim/accelerate_interpolator"        android:toYDelta="100%" /></set>
R. anim. slide_up_in:
<set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="300"        android:fromYDelta="-100%"        android:interpolator="@android:anim/accelerate_interpolator"        android:toYDelta="0%" /></set>

 

Try it now!

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.