Android Swipe to destroy (finish) Activity with gestures

Source: Internet
Author: User
Tags gety

Today brings a right swipe to destroy the activity, activtiy with the movement of the finger to destroy, sliding destruction activity mainly Using Gesturedetector to achieve this effect, when the gesture on the screen on the slide, will fall off with the Onfling method, so, in this method to do the judgment and operation can achieve the effect we want.

First look at the final effect of the implementation:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/7C/wKiom1Wd5YaiPahBAAS6hzkZW1o097.gif "title=" Activity slide Destroy Effect "alt=" Wkiom1wd5yaipahbaas6hzkzw1o097.gif "/>

All right, just on the code, I'm sure you'll understand.

1. Main Page activity

package com.sunny.slidingfinish;import android.os.bundle;import android.view.gesturedetector; import android.view.motionevent;import android.view.view;import  Android.view.view.onclicklistener;import android.widget.button;import android.app.activity;import  android.content.Intent;public class MainActivity extends Activity {  private button mbutton;private gesturedetector mgesturedetector; @Overrideprotected   Void oncreate (bundle savedinstancestate)  {super.oncreate (savedinstancestate); SetContentView ( R.layout.activity_main);mbutton =  (Button)  findviewbyid (R.ID.BTN1); Mbutton.setonclicklistener ( New onclicklistener ()  {@Overridepublic  void onclick (view arg0)  {// todo   automatically generated method stub startactivity (New intent (Mainactivity.this,aty1.class));}); /1. Overriding the Onfling method of  gesturedetector Mgesturedetector = new gesturedetector (tHis, new gesturedetector.simpleongesturelistener ()  {              @Override              public boolean onfling (motionevent e1, motionevent e2, float  VELOCITYX,&NBSP;FLOAT&NBSP;VELOCITYY)  {                 /**                  *  when the gesture is sliding, the effect of closing the page, the specific needs of the specific treatment                   *  swipe down, swipe up, swipe right (used this way)                   */                 //  gesture Down  down                 if  ((E2.getrawy ()  - e1.getrawy ())  > 200 )  {                     //finish ();//Control the shutdown here                      return true;                 }                 //  gesture up  up                 if  ((E1.getrawy ()  - e2.getrawy ())  > &NBSP;200)  {                 //finish ();//Control the shutdown here                  return true;                }                 //  Control only Right slide                  if  (E2.getx ()  - e1.getx ()  > 0                         &&  (E1.getx ()  >= 0 && e1.getx ()  <= 100)  {                      if  ( Math.Abs (E2.getx ()  - e1.getx ())  > math.abs (E2.gety ()  - e1.gety ())                           &nBsp;    && math.abs (Velocityx)  > 1000)  {                           overridependingtransition (r.anim.base_slide_right_in,                                   r.anim.base_slide_right_out);                           finish ();                          onbackpressed ();                       }                 }                  return super.onfling (E1, e2, &NBSP;VELOCITYX,&NBSP;VELOCITYY);             }         });} 2. Let the gesture recognizer   work and invoke the method when the activity is touched. @Overridepublic  boolean ontouchevent (motionevent  Event)  {    mgesturedetector.ontouchevent (event);    return  Super.ontouchevent (event);} @Overridepublic  void onbackpressed ()  {// TODO  auto-generated method stub super.onbackpressed (); O Verridependingtransition (0, r.anim.base_slide_right_out);  }}

2. Realize right slide animation effect

animation effect, rewrite the right slide animation effect, you can use the Android comes with a method, before the finish () method is called:

Overridependingtransition (r.anim.slide_up_in, r.anim.slide_up_out);

Base_slide_right_in.xml

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <translate android:duration= "android:fromxdelta=" 100.0% "android:interpolator=" @andro Id:anim/decelerate_interpolator "Android:toxdelta=" 0.0% "/></set>

Base_slide_right_out.xml

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <translate android:duration= "android:fromxdelta=" 0.0% "android:interpolator=" @android : Anim/accelerate_interpolator "Android:toxdelta=" 100.0% "/></set>


The above is the realization of the activity gesture sliding effect of the specific implementation, the code has been detailed comments, do not do too much explanation, I believe you can read, welcome to a learning Exchange ~ ~

: http://down.51cto.com/data/2066383

This article is from the "Sunnygeek Technology blog" blog, please be sure to keep this source http://sunnygeek.blog.51cto.com/9485654/1672457

Android Swipe to destroy (finish) Activity with gestures

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.