Example analysis of Viewpager rebound effect of Android programming _android

Source: Internet
Author: User

The example of this article describes the Android programming Viewpager rebound effect. Share to everyone for your reference, specific as follows:

In fact, many of our applications are seen when the Viewpager slide to the first page or the last page, if you slide again, there will be a buffer process, that is, rebound effect. In the study of rebound effect, and also by the way to achieve the rebound effect of viewpager, in fact, is very simple, it is the implementation of code, the comments are relatively few:

Package com.freesonfish.viewpager_2; 
Import Android.content.Context; 
Import Android.graphics.Rect; 
Import Android.support.v4.view.ViewPager; 
Import Android.util.AttributeSet; 
Import android.view.KeyEvent; 
Import android.view.MotionEvent; 
Import android.view.animation.TranslateAnimation; 
  public class Myviewpager extends Viewpager {private Rect mrect = new Rect ();//To record the initial position private int pagercount = 3; 
  private int currentitem = 0; 
  Private Boolean handledefault = true; 
  private float PreX = 0f; 
  Private static final float RATIO = 0.5f;//friction coefficient private static final float scroll_width = 30f; 
  Public Myviewpager {Super (context); 
  Public Myviewpager (context, AttributeSet attrs) {Super (context, attrs); 
  //Set the total number of pages, please remember to call it public void Setpagercount (int pagercount) {this.pagercount = Pagercount; 
  //This is the current page, please call it in the Onpageselect method. public void SetCurrentIndex (int currentitem) {This.currentitem = cUrrentitem; 
  @Override public boolean dispatchkeyevent (KeyEvent event) {return super.dispatchkeyevent (event); @Override public boolean onintercepttouchevent (Motionevent arg0) {if (arg0.getaction () = = Motionevent.action 
  _down) {PreX = Arg0.getx ();//Record Start} return Super.onintercepttouchevent (arg0); @Override public boolean ontouchevent (motionevent arg0) {switch (arg0.getaction ()) {case MOTIONEVENT.A 
      Ction_up:ontouchactionup (); 
    Break 
      Case Motionevent.action_move://At the time of slide to the first or last item. 
        if ((CurrentItem = = 0 | | currentitem = = pagerCount-1)) {Float nowx = Arg0.getx (); 
        float offset = Nowx-prex; 
        PreX = nowx; if (CurrentItem = = 0) {if (offset > Scroll_width) {//The distance of the finger slip is greater than the set value whetherconditionisright (offs 
          ET); else if (!handledefault) {//This situation is where the buffer area is already present, the finger slowly recovers if (GetLeft () + (int) (offset * RATIO) >= mrect.left) {layout (GetLeft () + (int) (offset * RATIO), GetTop (), GetRight () + (int) (offset * RATIO), 
            Getbottom ());  
          }} else {if (offset <-scroll_width) {whetherconditionisright (offset); 
              else if (!handledefault) {if (GetRight () + (int) (offset * RATIO) <= mrect.right) { 
            Layout (GetLeft () + (int) (offset * RATIO), GetTop (), GetRight () + (int) (offset * RATIO), Getbottom ()); 
      }}} else {handledefault = true; 
      } if (!handledefault) {return true; 
    } break; 
    Default:break; 
  Return super.ontouchevent (arg0); private void Whetherconditionisright (float offset) {if (Mrect.isempty ()) {Mrect.set (GetLeft (), GetTop () 
    , GetRight (), Getbottom ()); 
    } Handledefault = false; Layout (GetLeft () + (int) (offset * RATIO), GetTop (), GetrigHT () + (int) (offset * RATIO), Getbottom ()); 
    private void Ontouchactionup () {if (!mrect.isempty ()) {recoveryposition ()); 
    }} private void Recoveryposition () {translateanimation ta = null; 
    Ta = new Translateanimation (GetLeft (), mrect.left, 0, 0); 
    Ta.setduration (300); 
    Startanimation (TA); 
    Layout (Mrect.left, Mrect.top, Mrect.right, Mrect.bottom); 
    Mrect.setempty (); 
  Handledefault = true;

 } 
}

See, very simple, if you have any way, you can communicate a lot.

I hope this article will help you with the Android program.

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.