Android Swipeback to achieve the right sliding return effect _android

Source: Internet
Author: User

Now there are a lot of app support right sliding back, such as know, the effect is better.

So he inherited the activity and fragment, derived swipebackactivity and swipebackfragment, and used it for the realization of this effect, that is, just inherit these two classes.

The effect is as follows

    • Activity

Fragment

The effect of frgament is slightly simpler than activity, because the activity takes into account the Dectorview.

Support Sliding control swipelayout, the core idea is to add the original control to support sliding control, swipelayout to pay attention to calculate the speed of gestures, the source code is as follows:

Package Com.ui.jerry.swipebackdemo;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import android.view.MotionEvent;
Import Android.view.VelocityTracker;
Import Android.view.View;
Import android.view.ViewConfiguration;
Import Android.view.ViewGroup;
Import Android.widget.LinearLayout;
Import Android.widget.Scroller;


Import Android.widget.Toast;

  public class Swipelayout extends LinearLayout {public static final String TAG = "Swipelayout";
  Private View Memptyview;

  Private View Mcontentview;
  private int Mleftedge;
  private int mwidth;

  private int mmaxscrollx;
  Private Scroller Mscroller;
  Private Velocitytracker mvelocitytracker = null;
  private int mmaxflingvelocity;

  private int mlastx; Viewgroup.layoutparams childparams = new Viewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT,

  ViewGroup.LayoutParams.MATCH_PARENT);
  Private context Mcontext; public static final int Duration = 1500;
  Full screen sliding time public static final int open_anim_duration = 1000; public static int snap_velocity = 600;

  The minimum sliding rate is private onfinishlistener monfinishlistener;
  Public Swipelayout {This (context, NULL);

    Public Swipelayout (context, AttributeSet attrs) {Super (context, attrs);
    Mcontext = context;
  Init (); 
  public void Setonfinishlistener (Onfinishlistener monfinishlistener) {this.monfinishlistener = Monfinishlistener;
    } void Init () {Mscroller = new scroller (mcontext);

    mmaxflingvelocity = Viewconfiguration.get (mcontext). Getscaledmaximumflingvelocity ();
    Mwidth = Displayutils.getscreenwidth (mcontext) * 2;
    MMAXSCROLLX = MWIDTH/2;

    Mleftedge = MMAXSCROLLX-MMAXSCROLLX/3;

    SetOrientation (linearlayout.horizontal);

    Childparams.width = Displayutils.getscreenwidth (Mcontext);

    Memptyview = Layoutinflater.from (Mcontext). Inflate (r.layout.view_translate, NULL); AddView (Memptyview, CHildparams);
    public void Setcontentview (View contentview) {if (Mcontentview!= null) {Removeview (Mcontentview);
    } Mcontentview = Contentview;

    AddView (Contentview, childparams);
      Postdelayed (New Runnable () {@Override public void run () {openactivityanimation ();
  }, 200);  /** * Get Speed Tracker * * @return/private Velocitytracker Getvelocitytracker () {if (Mvelocitytracker = =
    NULL) {Mvelocitytracker = Velocitytracker.obtain ();
  return mvelocitytracker; /** * Recovery Speed Tracker */private void Recyclevelocitytracker () {if (Mvelocitytracker!= null) {MVELOCITYT
      Racker.clear ();
      Mvelocitytracker.recycle ();
    Mvelocitytracker = null;
    @Override public boolean ontouchevent (motionevent ev) {//1. Gets the Speed tracker getvelocitytracker ();

    2. Incorporating current events into the tracker mvelocitytracker.addmovement (EV);

    int pointid =-1; Switch (ev.getaction ()) {caseMotionevent.action_down://If the screen animation is not finished, you press, we end the last animation, that is, to start the new Action_down animation//Clearscrollhis ();
        MLASTX = (int) ev.getx ();
        Pointid = Ev.getpointerid (0);
      Break

        Case MotionEvent.ACTION_MOVE:int NEXTSCROLLX = (int) (MLASTX-EV.GETX () + GETSCROLLX ());
        if (Scrollto (NEXTSCROLLX)) {mlastx = (int) ev.getx ();
      } break; Case MotionEvent.ACTION_CANCEL:case motionevent.action_up://3. Calculate Current Speed Mvelocitytracker.computecurre
        Ntvelocity (1000, mmaxflingvelocity);

        Gets the velocity on the x y direction of float VX = mvelocitytracker.getxvelocity (Pointid);

        LOG.I (TAG, "Mvelocityx:" + VX);
        Greater than a rate of direct sliding if (VX > Snap_velocity) {scrolltoleft ();
        else if (VX <-snap_velocity) {scrolltoright ();
        else {snaptodestation ();
        //4. Recovery Speed tracker Recyclevelocitytracker ();
    Break
 return true; private void Openactivityanimation () {clearscrollhis ();
    Mscroller.startscroll (GETSCROLLX (), 0, Mmaxscrollx-getscrollx (), 0, open_anim_duration); Invalidate ()///Must call invalidate () to ensure that computescroll () will be invoked, otherwise it will not necessarily refresh the interface, do not see the scrolling effect} public void Closeactivityanimation ()
    {clearscrollhis ();
    Mscroller.startscroll (GETSCROLLX (), 0,-getscrollx (), 0, open_anim_duration); Invalidate ()///Must call invalidate () to ensure that computescroll () will be invoked, otherwise it will not necessarily refresh the interface, do not see the scrolling effect} private void Clearscrollhis () {if (Mscroller!= null)
      {if (!mscroller.isfinished ()) {mscroller.abortanimation ();
    }}/** * Based on the current scrolling position to judge/private void Snaptodestation () {int scrollx = GETSCROLLX ();
    if (scrollx > 0 && scrollx <= mleftedge) {smoothscrollto (0);
    else if (Scrollx > Mleftedge) {smoothscrollto (MMAXSCROLLX); }/** * Direct scrolling * * @param x * @return/public boolean scrollto (int x) {if (x< 0) {scrollto (0, 0);
    else if (x > Mmaxscrollx) {scrollto (mmaxscrollx, 0);
    else {scrollto (x, 0);
  return true;
  public void Scrolltoright () {Smoothscrollto (MMAXSCROLLX);
  public void Scrolltoleft () {smoothscrollto (0); @Override protected void onscrollchanged (int l, int t, int oldl, int Oldt) {super.onscrollchanged (L, T, OLDL,

    Oldt);

    LOG.D (TAG, "Left:" + L);

      if (L = = 0) {log.d (TAG, "onfinish");

      Toast.maketext (Mcontext, "finished", Toast.length_short). Show ();
      if (monfinishlistener!=null) {monfinishlistener.onfinish ();
    }} public void Smoothscrollto (int fx) {if (FX < 0) {smoothscrollto (0, 0);
    else if (FX > Mmaxscrollx) {smoothscrollto (mmaxscrollx, 0);
    else {smoothscrollto (FX, 0);
    }///Call this method to scroll to the target location public void Smoothscrollto (int fx, int fy) {int dx = FX-GETSCROLLX (); int dy = Fy-geTscrolly ();
  Smoothscrollby (dx, dy); //Call this method to set the relative offset of scrolling public void Smoothscrollby (int dx, int dy) {//Set Mscroller's scrolling offset mscroller.startscroll (ge
    TSCROLLX (), 0, DX, dy, math.abs (DX * duration/mmaxscrollx));  Invalidate ()///Must call invalidate () to ensure that computescroll () will be invoked, otherwise it will not necessarily refresh the interface, do not see scrolling effect} @Override public void Computescroll () {///first determine if Mscroller scrolling completes if (Mscroller.computescrolloffset ()) {///here is the Scrollto () that calls view to complete the actual scrolling Scrollto

      (Mscroller.getcurrx (), Mscroller.getcurry ());
    The method must be called, otherwise the scrolling effect postinvalidate () is not necessarily visible;
  } super.computescroll ();
  }/** * Fragment or an activity-terminated interface/public interface onfinishlistener{void OnFinish ();
 }
}

The above is the entire content of this article, I hope to help you learn.

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.