Viewpager animation effect (learning from mu-net)

Source: Internet
Author: User

First on effect one:


First on effect two:

First on the effect three:


First, the Viewpager control is defined, and the second is to invoke a line of code to animate the effect.

Pager. Setpagetransformer (true,Depthpagetransformer ());

The prerequisite is to do some preparation operations, and only api3.0 above can be effective.

Note: Because the property animation is 3.0 later, if you want to implement animation on a phone below 3.0, you need to make some changes, using the nineoldandroids frame instead of the property animation. It's not backwards compatible here first. If you want to backward-compatible friends, you can visit the macro-yang great God's blog, address http://blog.csdn.net/lmj623565791/article/details/40411921/

Public classMainactivityextendsappcompatactivity {PrivateViewpagerPager;private int[]Mimgids=New int[]{r.drawable.Ic_guaid1, R.drawable.Ic_guaid2, R.drawable.IC_GUAID3, R.drawable.Ic_guaid4};PrivateList<imageview>mimages=NewArraylist<> ();@Override    protected voidOnCreate (Bundle savedinstancestate) {requestwindowfeature (Window.Feature_no_title);Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.Activity_main);Pager= ((Viewpager) Findviewbyid (r.id.Pager));//Add Toggle animation effect (api3.0 or more) for Viewpager
//Effect three  //        Pager.setpagetransformer (true,new zoomoutpagetransformer ());
Effect Two
   /span>
 //  Pager  . Setpagetransformer (  true  ,   new   Rotatedownpagetransformer ()); 
< em>//effect one   
Pager . Setpagetransformer ( true , new Depthpagetransformer ());
Pager. Setoffscreenpagelimit (3);Pager. Setadapter (NewPageradapter () {@Override public intGetCount () {returnMimgids.length; }@Override Public BooleanIsviewfromobject (View view, Object object) {returnView==object; }@Override PublicObject Instantiateitem (ViewGroup container,intPosition) {ImageView imageview=NewImageView (mainactivity. This); Imageview.setimageresource (Mimgids[position]); Container.addview (ImageView);returnImageView; }@Override Public voidDestroyitem (ViewGroup container,intPosition, Object object) {Container.removeview ((ImageView) object); } }); }}
Public classDepthpagetransformerImplementsViewpager.pagetransformer {private static final floatMin_scale=0.75f;Public voidTransformpage (View view,floatPosition) {intPageWidth = View.getwidth ();if(Position <-1) {//[-infinity,-1]//This page was the off-screen to the left.            View.setalpha (0); }else if(Position <=0) {//[ -1,0]//Use of the default slide transition when moving to the left page            View.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 transition            View.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 a-off-screen to the right.            View.setalpha (0); }    }}

Public classZoomoutpagetransformerImplementsViewpager.pagetransformer {private static final floatMin_scale=0.85f;private static final floatMin_alpha=0.5f;Public voidTransformpage (View view,floatPosition) {intPageWidth = View.getwidth ();intPageHeight = View.getheight ();if(Position <-1) {//[-infinity,-1]//This page was 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 a-off-screen to the right.            View.setalpha (0); }    }}
Public classRotatedownpagetransformerImplementsviewpager.pagetransformer{private static final floatRot_max=20.0f;Private FloatMrot;Public voidTransformpage (View view,floatPosition) {Log.e("TAG", view +" , " + Position +"");if(Position <-1)          {//[-infinity,-1]//This page was the off-screen to the left .             View.setrotation (0); }else if(Position <=1)//A page slide to page B, page a from 0.0 ~ 1; page b from 1 ~ 0.0        {//[ -1,1]//Modify The default slide transition to shrink the page as well            if(Position <0)              {Mrot= (Rot_max* position); View.setpivotx (View.getmeasuredwidth () *0.5f);                View.setpivoty (View.getmeasuredheight ()); View.setrotation (Mrot); }Else            {Mrot= (Rot_max* position); View.setpivotx (View.getmeasuredwidth () *0.5f);                View.setpivoty (View.getmeasuredheight ()); View.setrotation (Mrot); }//Scale the PAGE down (between Min_scale and 1)  //Fade the page relative to its size.           }Else        {//(1,+infinity]//This page is a-off-screen to the right.             View.setrotation (0); }      }  }



Viewpager animation effect (learning from mu-net)

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.