Add some cool animations to Viewpager

Source: Internet
Author: User

Viewpagerwithanimations

Add animated effects to Viewpager and make them compatible with API11 versions below

Overriding the Viewpager class
 Public  class myviewpager extends viewgroup {... Public void Setpagetransformer(BooleanReversedrawingorder, Viewpager.pagetransformer transformer) {//if (Build.VERSION.SDK_INT >=) {            Final BooleanHastransformer = transformer! =NULL;Final BooleanNeedspopulate = hastransformer! = (Mpagetransformer! =NULL);            Mpagetransformer = transformer; Setchildrendrawingorderenabledcompat (Hastransformer);if(Hastransformer) {Mdrawingorder = Reversedrawingorder?            Draw_order_reverse:draw_order_forward; }Else{mdrawingorder = Draw_order_default; }if(needspopulate) populate ();//        }}...}
    1. Comment out where the SDK version is judgedif (Build.VERSION.SDK_INT >= 11) {
    2. PageTransformerSwitchViewPager.PageTransformer
XML Layout
<com.zhengsonglan.viewpagerwithanimations.UI.MyViewPager        android:id="@+id/main_viewpager"        android:layout_width="match_parent"        android:layout_height="match_parent"/>
Animating Animations 1
 Public  class Zoomoutpagetransformer implements Viewpager. Pagetransformer {    Private Static Final floatMin_scale =0.85FPrivate Static Final floatMin_alpha =0.5F Public void Transformpage(View view,floatPosition) {intwidth = View.getwidth ();intPageWidth = width;intPageHeight = View.getheight ();if(Position <-1) {//[-infinity,-1]            //This page was the off-screen to the left.Viewhelper.setalpha (View,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) {floatresult1=horzmargin-vertmargin/2;            Viewhelper.settranslationx (VIEW,RESULT1); }Else{floatResult2=-horzmargin + vertmargin/2;            Viewhelper.settranslationy (VIEW,RESULT2); }//Scale the PAGE down (between Min_scale and 1)Viewhelper.setscalex (View,scalefactor);            Viewhelper.setscaley (View,scalefactor); Viewhelper.setalpha (View,min_alpha + (Scalefactor-min_scale)/(1-Min_scale) * (1-Min_alpha)); }Else{//(1,+infinity]            //This page is a-off-screen to the right.Viewhelper.setalpha (View,0); }    }}
Effect

Animation 2
/** * Created by ZSL on 2015/2/25. * Animation 2 */ Public  class Depthpagetransformer implements Viewpager. Pagetransformer {    Private Static Final floatMin_scale =0.75F Public void Transformpage(View view,floatPosition) {intPageWidth = View.getwidth ();if(Position <-1) {//[-infinity,-1]            //This page was the off-screen to the left.Viewhelper.setalpha (View,0); }Else if(Position <=0) {//[ -1,0]            //Use of the default slide transition when moving to the left pageViewhelper.setalpha (View,1); Viewhelper.settranslationx (View,0); Viewhelper.setscalex (View,1); Viewhelper.setscaley (View,1); }Else if(Position <=1) {//(0,1]            //Fade the page out.Viewhelper.setalpha (View,1-position);//CounterAct the default slide transitionViewhelper.settranslationx (View,pagewidth *-position);//Scale the PAGE down (between Min_scale and 1)            floatScalefactor = Min_scale + (1-Min_scale) * (1-Math.Abs (position));            Viewhelper.setscalex (View,scalefactor);        Viewhelper.setscaley (View,scalefactor); }Else{//(1,+infinity]            //This page is a-off-screen to the right.Viewhelper.setalpha (View,0); }    }}
Effect

Animation 3
/** * Created by ZSL on 2015/2/25. * Animation 3 */ Public  class Animation3transformer implements Viewpager. Pagetransformer {    Private Static Final floatMax_roate = -F Public void Transformpage(View view,floatPosition) {intwidth = View.getwidth ();intPageWidth = width;intPageHeight = View.getheight ();if(Position <-1) {//[-infinity,-1]Viewhelper.setrotation (View,0); }Else if(Position <=1) {//[ -1,1]            floatResult=position*max_roate; Viewhelper.setpivotx (view,pagewidth*0.5f);            Viewhelper.setpivoty (View,pageheight);        Viewhelper.setrotation (View,result); }Else{//(1,+infinity]Viewhelper.setrotation (View,0); }    }}
Effect

Animation 4
/** * Created by ZSL on 2015/2/25. * Animation 4 */ Public  class Animation4transformer implements Viewpager. Pagetransformer {    Private Static Final floatMax_roate = theF Public void Transformpage(View view,floatPosition) {intwidth = View.getwidth ();intPageWidth = width;intPageHeight = View.getheight ();if(Position <-1) {//[-infinity,-1]Viewhelper.setrotation (View,0); Viewhelper.setalpha (View,1); }Else if(Position <=1) {//[ -1,1]            floatResult=position*max_roate;//RotateViewhelper.setpivotx (view,pagewidth*0.5f); Viewhelper.setpivoty (View, pageheight*0.5f); Viewhelper.setrotation (View,result);//TransparencyViewhelper.setalpha (View,1-math.abs (position));//ZoomViewhelper.setscaley (View,1-math.abs (position)); Viewhelper.setscalex (View,1-math.abs (position)); }Else{//(1,+infinity]Viewhelper.setrotation (View,0); Viewhelper.setalpha (View,1); }    }
Effect

Thanks for

1.jakewharton:nineoldandroids

2.google:using Viewpager for Screen Slides

Developed by
    • ZSL-<[email protected]>
Source Address

Viewpagerwithanimations
Welcome to Star

Add some cool animations to Viewpager

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.