One has already said, how to realize the switch of the picture. Here I say how to add animation to the Switch animation, right! You heard wrong, we're going to animate viewpager.
First put the source code code
1 PackageCom.example.android_viewpager;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Importandroid.app.Activity;7 ImportAndroid.os.Bundle;8 ImportAndroid.support.v4.view.PagerAdapter;9 ImportAndroid.support.v4.view.ViewPager;Ten ImportAndroid.view.View; One ImportAndroid.view.ViewGroup; A ImportAndroid.widget.ImageView; - ImportAndroid.widget.ImageView.ScaleType; - the Public classMainactivityextendsActivity { - PrivateViewpager Viewpager =NULL; - Private int[] res =New int[] {r.drawable.guide_image1, r.drawable.guide_image2, r.drawable.guide_image3}; - PrivateList<imageview>list =NewArraylist<imageview>(); + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); AViewpager =(Viewpager) Findviewbyid (R.id.viewpager); at Viewpager.setpagetransformer (True, New Depthpagetransformer ()); -Viewpager.setadapter (NewPageradapter () { - @Override - PublicObject Instantiateitem (ViewGroup container,intposition) { -ImageView ImageView =NewImageView (mainactivity. This); - Imageview.setimageresource (res[position]); in Imageview.setscaletype (scaletype.center_crop); - Container.addview (ImageView); to List.add (ImageView); + returnImageView; - } the @Override * Public voidDestroyitem (ViewGroup container,intposition, Object object) { $ //TODO auto-generated Method StubPanax Notoginseng Container.removeview (list.get (position)); - } the Public BooleanIsviewfromobject (View arg0, Object arg1) { + //TODO auto-generated Method Stub A returnarg0 = =arg1; the } + - @Override $ Public intGetCount () { $ //TODO auto-generated Method Stub - returnres.length; - } the }); - }Wuyi}
Looking at the code above, we will find a line more than the original code, which is Viewpager set the animation, but where the inexplicable class from?
As you can see from the API, the Setpagetransformer method prototype is: Setpagetransformer (boolean reversedrawingorder, Viewpager.pagetransformer transformer); The first argument is a Boolean type, so needless to say, what is the second argument? Pagetransformer type parameter?
Let me explain the following Pagetransformer class, which is an interface specifically designed to animate Viewpager. It contains only an abstract method public void Transformpage (view view, float position). We usually manipulate this method to add animations to Viewpager.
Then, let's see how to use the Pagetransformer class. First we use its subclass, and it is a custom subclass, why? Because it has no sub-class of the system. So how do you create a subclass of it? I copied two codes from the official Android API for your reference.
1.DepthPageTransformer class
1 Private Static Final floatMin_scale = 0.75f;2 3 Public voidTransformpage (View view,floatposition) {4 intPageWidth =view.getwidth ();5 6 if(Position <-1) {//[-infinity,-1]7 //This page is the same as the off-screen to the left .8View.setalpha (0);9 Ten}Else if(Position <= 0) {//[ -1,0] One //Use the default slide transition If moving to the left page AView.setalpha (1); -View.settranslationx (0); -View.setscalex (1); theView.setscaley (1); - -}Else if(Position <= 1) {//(0,1] - //Fade the page out. +View.setalpha (1-position); - + //CounterAct the default slide transition AView.settranslationx (PageWidth *-position); at - //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); in -}Else{//(1,+infinity] to //This page is the -off-screen to the right. +View.setalpha (0); - } the}
2.ZoomOutPageTransformer class
1 Private Static Final floatMin_scale = 0.85f;2 Private Static Final floatMin_alpha = 0.5f;3 4 Public voidTransformpage (View view,floatposition) {5 intPageWidth =view.getwidth ();6 intPageHeight =view.getheight ();7 8 if(Position <-1) {//[-infinity,-1]9 //This page is the same as the off-screen to the left .TenView.setalpha (0); One A}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)); the floatVertmargin = PageHeight * (1-scalefactor)/2; - floatHorzmargin = PageWidth * (1-scalefactor)/2; - if(Position < 0) { -View.settranslationx (HORZMARGIN-VERTMARGIN/2); +}Else { -View.settranslationx (-horzmargin + VERTMARGIN/2); + } A at //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 + in(Scalefactor-min_scale)/ -(1-min_scale) * (1-min_alpha)); to +}Else{//(1,+infinity] - //This page is the -off-screen to the right. theView.setalpha (0); * } $}
We can use these two codes to animate Viewpager.
Android's ever-changing Viewpager II