transformpage (view view, float position)
view is the sliding view, Position here is the float type, is a representation of the current sliding state, such as when sliding to the full screen, position is 0, if the previous page and the next page is almost half of the screen, The previous page of the position is-0.5, the next page of the Posiotn is 0.5 ( 0.5 or 0.5 to see whether the current view is the front page of the view or the back of the page ), So according to the value of position we can set the required alpha,x/y information, that is, you can use Setalpha (), Settranslationx () or Setsaley () to customize the transition animation.
Google official animation effect
Public classDepthpagetransformerImplementsPagetransformer {Private Static floatMin_scale = 0.75f; @SuppressLint ("Newapi") @Override Public voidTransformpage (View view,floatposition) { intPageWidth =view.getwidth (); if(Position <-1) {//[-infinity,-1]//This page is the same as the off-screen to the left .View.setalpha (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 transitionView.settranslationx (PageWidth *-position); //Scale the page down (between Min_scale and 1) floatScalefactor = Min_scale + (1-Min_scale)* (1-Math.Abs (position)); View.setscalex (Scalefactor); View.setscaley (Scalefactor); } Else{//(1,+infinity]//This page is the -off-screen to the right.View.setalpha (0); } }}
Public classZoomoutpagetransformerImplementsPagetransformer {Private Static floatMin_scale = 0.85f; Private Static floatMin_alpha = 0.5f; @Override Public voidTransformpage (View view,floatposition) { intPageWidth =view.getwidth (); intPageHeight =view.getheight (); if(Position <-1) {//[-infinity,-1]//This page is the same as the off-screen to the left .View.setalpha (0); } Else if(Position <= 1) {//[ -1,1]//Modify The default slide transition to//shrink the page as well floatScalefactor = Math.max (Min_scale, 1-Math.Abs (position)); floatVertmargin = PageHeight * (1-scalefactor)/2; floatHorzmargin = PageWidth * (1-scalefactor)/2; if(Position < 0) {View.settranslationx (Horzmargin-VERTMARGIN/2); } Else{View.settranslationx (-horzmargin + VERTMARGIN/2); } //Scale the page 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 to the right.View.setalpha (0); } }
Use
Viewpager Mpager = (Viewpager) Findviewbyid (R.id.pager); Mpager.setpagetransformer (truenew Depthpagetransformer ());
I'm the dividing line of the king of the land Tiger.
Viewpager Series:
Viewpager background Gradient effect: Android--viewpager sliding background gradient
Viewpager+fragment:android--Viewpager, fragment, state saving, communication
Android–viewpager Toggle Animation, Pagetransformer