Transformpage method parsing in Pagetransformer interface of Android Viewpager switch

Source: Internet
Author: User

Today, let's look at an animation that Android3.0 after the Viewpager switch. Google showed us two examples of animations: Depthpagetransformer and Zoomoutpagetransformer, with the following code:

public class Depthpagetransformer implements Pagetransformer {private static float Min_scale = 0.75f; @SuppressLint ("Newapi") @Overridepublic void Transformpage (view view, float position) {int PageWidth = View.getwidth (); if (position <-1) {//[-infinity,-1]//this page is-a-off-screen to the LEFT.VIEW.SETALP Ha (0);} else if (position <= 0) {//[ -1,0]use//the default Slide transition when moving to the left pageview.setalpha (1); view . Settranslationx (0); View.setscalex (1); View.setscaley (1);} else if (position <= 1) {//(0,1]//Fade the page out.view.setAlpha (1-position);//counteract the default slide tran Sitionview.settranslationx (PageWidth *-position);//Scale the PAGE down (between Min_scale and 1) float scalefactor = Min_ Scale + (1-min_scale) * (1-math.abs (position)); View.setscalex (scalefactor); View.setscaley (scalefactor);} else {//(1,+infinity]//This page was the off-screen to the Right.view.setAlpha (0);}} 

public class Zoomoutpagetransformer implements Pagetransformer {private static float Min_scale = 0.85f;        private static float Min_alpha = 0.5f;          @Override public void Transformpage (view view, float position) {int pagewidth = View.getwidth ();            int pageheight = View.getheight ();              if (Position <-1) {//[-infinity,-1]//This page is the off-screen of the left.          View.setalpha (0);                                      } else if (position <= 1) {//[ -1,1]//Modify The default slide transition to Shrink the page as well float scalefactor = Math.max (Min_scale, 1-mat              H.abs (position));              float Vertmargin = PageHeight * (1-scalefactor)/2;              float Horzmargin = pagewidth * (1-scalefactor)/2;           if (Position < 0) {View.settranslationx (HORZMARGIN-VERTMARGIN/2);   } else {View.settranslationx (-horzmargin + VERTMARGIN/2);              }//Scale the page is down (between Min_scale and 1) view.setscalex (scalefactor);              View.setscaley (Scalefactor);              Fade the page relative to its size.          View.setalpha (Min_alpha + (Scalefactor-min_scale)/(1-min_scale) * (1-min_alpha));              } else {//(1,+infinity]//This page is the off-screen of the right.          View.setalpha (0);   }      }  }

We use only Mpager.setpagetransformer (true, New Depthpagetransformer ()) can, let us see how the Transformpage method to achieve animation!!!

The Transformpage method has two parameters, the first view is of course the one that applies the animation, the second is a value of type float, not the position position we normally see, but the representation of the current sliding state, Position relative to the current position. It has three thresholds-1 0 1, and 0 represents the position,1 of the view displayed on the current screen, which represents the position of the previous view of the current view. Position,-1 represents the current view. Don't you know I made it clear?

Then take a look at the following picture:

  

Let's take a look at the changes in the view Positon when the current view is left-sliding and right-sliding:

  

All right, the table above can be verified by playing log. Knowing the position of the view in each location, let's take a look at the animation. Here we do not do cool animation, only do the animation of transparency, easy to understand the Transformpage method.

If you don't know about the alpha attribute, the value range is between 0 and 1, 0 is completely transparent, and 1 is completely opaque. Don't say much nonsense about the animation:

public class Viewpagertransformanim implements Viewpager.pagetransformer {    @Override public    Void Transformpage (View page, float position) {        float alpha = 0.0f;        if (0.0f <= position && position <= 1.0f) {            alpha = 1.0f-position;        } else if ( -1.0f <= position && Position < 0.0f) {            alpha = position + 1.0f;        }        Page.setalpha (alpha);}    }

So let's simply explain the above code: whether the current view is left or right, when I swipe, according to the previous table we know that the above two judgments will go, because when you swipe, it must be two view animation switch (not counting two boundaries, The boundary will only go one), since alpha values between 0 and 1, the position value is between 1 and 1. So we have the code in the conditional statement. Let's look at the effect:

Very simple an animation, hope to help you!!!

Transformpage method parsing in Pagetransformer interface of Android Viewpager switch

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.