Android Custom Viewpager--3d effect app

Source: Internet
Author: User
Tags acos object object sin

Today in GitHub see a 3D effect of the Viewpager, feel made out of the Viewpager slide when the effect is very dazzling, check out down to study how to achieve, and how to use, Once the entire viewpager has been slightly modified (mainly by dealing with events that conflict with other control sliding controls in the project), it is applied to your own project. Feel this effect is really very good, now write a demo of their own to share out.

Here is the Viewpager embedded in the project:

 

After the change, there will be a three-dimensional feeling when switching viewpager, which will add a lot to your application. Following the use of the demo sent out, in fact, with the use of ordinary viewpager exactly the same, just as if only with Pageradapter and its subclasses adapter to fill it with data.

1.viewpager3d Source:

public class Viewpager3d extends viewpager{/** * Maximum overscroll rotation of the children are divided by this value * /final static Float default_overscroll_rotation = 2f;/** * Maximum Z distance to translate child view */final static int D efault_overscroll_translation = 150;/** * Maximum z distanze during swipe */final static int default_swipe_translation = 1 00;/** * Maximum rotation during swipe is all divided by this value */final static float default_swipe_rotation = 3;/** * d  Uration of Overscroll Animation in MS */final private static int default_overscroll_animation_duration = 400;/** * If True Alpha of children gets animated during swipe and Overscroll */final private static Boolean Default_animate_alpha = true;@ Suppresswarnings ("unused") private final static String Debug_tag = ViewPager.class.getSimpleName ();p rivate final static int invalid_pointer_id = -1;private final static Double RADIANS = 180f/math.pi;/** * @author renard */private class over Scrolleffect{private floatMoverscroll;private Animator manimator;/** * @param deltadistance [0..1] 0->no overscroll, 1>full overscroll */publ IC void Setpull (final float deltadistance) {moverscroll = Deltadistance;invalidatevisiblechilds ();} /** * Called when Finger is released. Starts to animate back to default * position */private void Onrelease () {if (manimator! = null && Manimator.isrunni Ng ()) {Manimator.addlistener (new Animatorlistener () {@Overridepublic void Onanimationstart (Animator animation) {}@ overridepublic void Onanimationrepeat (Animator animation) {} @Overridepublic void Onanimationend (Animator animation) { Startanimation (0);} @Overridepublic void Onanimationcancel (Animator animation) {}}); Manimator.cancel ();} Else{startanimation (0);}} private void Startanimation (final float target) {manimator = Objectanimator.offloat (this, ' pull ', moverscroll, target); Manimator.setinterpolator (New Decelerateinterpolator ()); final float scale = Math.Abs (Target-moverscroll); Manimator.setduration ((Long) (MoverscrollanImationduration * scale)); Manimator.start ();} Private Boolean isoverscrolling () {if (mscrollposition = = 0 && moverscroll < 0) {return true;} if (getadapter () = null) {Final Boolean islast = (Getadapter (). GetCount ()-1) = = Mscrollposition;if (islast && MO Verscroll > 0) {return true;}} return false;}} Final private Overscrolleffect moverscrolleffect = new Overscrolleffect (); final private Camera Mcamera = new camera ();p Riv Ate onpagechangelistener mscrolllistener;private float mlastmotionx;private int mactivepointerid;private int mscrollposition;private float mscrollpositionoffset;private int mscrollpositionoffsetpixels;final private int mtouchslop;private float moverscrollrotation;private float mswiperotation;private int moverscrolltranslation;private int Mswipetranslation;private int Moverscrollanimationduration;private boolean manimatealpha;private Rect mTempTect = New Rect ();p ublic Viewpager3d (context context, AttributeSet Attrs) {Super (context, attrs); setstatictransformAtionsenabled (true); final viewconfiguration configuration = viewconfiguration.get (context); Mtouchslop = Viewconfigurationcompat.getscaledpagingtouchslop (configuration); Super.setonpagechangelistener (new Myonpagechangelistener ()); init (attrs);} private void init (AttributeSet attrs) {TypedArray a = GetContext (). Obtainstyledattributes (Attrs, R.styleable.viewpager3d); moverscrollrotation = A.getfloat (r.styleable.viewpager3d_overscroll_rotation, DEFAULT_ overscroll_rotation); mswiperotation = A.getfloat (r.styleable.viewpager3d_swipe_rotation, DEFAULT_SWIPE_ROTATION); Mswipetranslation = A.getint (r.styleable.viewpager3d_swipe_translation, default_swipe_translation); Moverscrolltranslation = A.getint (r.styleable.viewpager3d_overscroll_translation,default_overscroll_translation) ; moverscrollanimationduration = A.getint (r.styleable.viewpager3d_overscroll_animation_duration,default_ overscroll_animation_duration); Manimatealpha = A.getboolean (R.styleable.viewpager3d_animate_alpha, DEFAULT_ Animate_alpha); A.REcycle ();} public Boolean Isanimatealpha () {return manimatealpha;} public void Setanimatealpha (Boolean manimatealpha) {this.manimatealpha = Manimatealpha;} public int getoverscrollanimationduration () {return moverscrollanimationduration;} public void setoverscrollanimationduration (int moverscrollanimationduration) {this.moverscrollanimationduration = Moverscrollanimationduration;} public int getswipetranslation () {return mswipetranslation;} public void setswipetranslation (int mswipetranslation) {this.mswipetranslation = mswipetranslation;} public int getoverscrolltranslation () {return moverscrolltranslation;} public void setoverscrolltranslation (int moverscrolltranslation) {this.moverscrolltranslation = Moverscrolltranslation;} public float getswiperotation () {return mswiperotation;} public void setswiperotation (float mswiperotation) {this.mswiperotation = mswiperotation;} public float getoverscrollrotation () {return moverscrollrotation;} public void setoverscrollrotation (float moverscrollrotation) {This.moveRscrollrotation = moverscrollrotation;} @Overridepublic void Setonpagechangelistener (Onpagechangelistener listener) {Mscrolllistener = listener;}; private void Invalidatevisiblechilds () {for (int i = 0; i < Getchildcount (); i++) {final View Childat = Getchildat (i); chi Ldat.getlocalvisiblerect (mtemptect); final int area = Mtemptect.width () * Mtemptect.height (), if (area > 0) { Childat.invalidate ();}} Invalidate ();} Private class Myonpagechangelistener implements onpagechangelistener{@Overridepublic void onpagescrolled (int position , float positionoffset, int positionoffsetpixels) {if (Mscrolllistener! = null) {mscrolllistener.onpagescrolled ( Position, Positionoffset, positionoffsetpixels);} Mscrollposition = Position;mscrollpositionoffset = Positionoffset;mscrollpositionoffsetpixels = PositionOffsetPixels ;//LOG.I (Debug_tag, "mscrollposition =" + position + "offset =" + String.Format ("%f.2", Positionoffset));//LOG.I (Debu G_tag, "onpagescrolled"); Invalidatevisiblechilds ();} @Overridepublic void Onpageselected (int position) {if (Mscrolllistener! = null) {mscrolllistener.onpageselected (position);}} @Overridepublic void onpagescrollstatechanged (final int state) {if (Mscrolllistener! = null) { Mscrolllistener.onpagescrollstatechanged (state);} if (state = = Scroll_state_idle) {mscrollpositionoffset = 0;}}} @Overridepublic boolean onintercepttouchevent (motionevent ev) {final int action = Ev.getaction () & Motioneventcompat.action_mask;switch (ACTION) {Case Motionevent.action_down:{mlastmotionx = Ev.getx (); Mactivepointerid = Motioneventcompat.getpointerid (EV, 0); break;} Case motioneventcompat.action_pointer_down:{final int index = motioneventcompat.getactionindex (EV); final float x = MOTIONEVENTCOMPAT.GETX (EV, index); Mlastmotionx = X;mactivepointerid = Motioneventcompat.getpointerid (EV, index); Break;}} return super.onintercepttouchevent (EV);} @Overridepublic boolean ontouchevent (Motionevent ev) {boolean callsuper = false;final int action = ev.getaction (); switch ( Action) {Case Motionevent.action_dowN:{callsuper = True;mlastmotionx = Ev.getx (); Mactivepointerid = Motioneventcompat.getpointerid (EV, 0); break;} Case motioneventcompat.action_pointer_down:{callsuper = true;final int index = motioneventcompat.getactionindex (EV); Final float x = motioneventcompat.getx (ev, index); Mlastmotionx = X;mactivepointerid = Motioneventcompat.getpointerid (ev , index); Case Motionevent.action_move:{if (Mactivepointerid! = invalid_pointer_id) {//Scroll to follow the motion eventfinal int AC Tivepointerindex = Motioneventcompat.findpointerindex (EV, mactivepointerid); final float x = motioneventcompat.getx (ev  , Activepointerindex); final float deltax = mlastmotionx-x;final int width = getwidth (); final int widthwithmargin = width + Getpagemargin (); final int lastitemindex = Getadapter (). GetCount ()-1;final int currentitemindex = Getcurrentitem (); fin Al float leftbound = Math.max (0, (currentItemIndex-1) * widthwithmargin); final float rightbound = math.min (currentitemin Dex + 1, lastitemindex) * WidthwithMargin;if (Mscrollpositionoffset = = 0) {if (Currentitemindex = = 0) {if (Leftbound = = 0) {final Float over = DeltaX + MTOUCHSL Op;moverscrolleffect.setpull (Over/width);}}  else if (Lastitemindex = = Currentitemindex) {if (Rightbound = = Lastitemindex * widthwithmargin) {final Float over = deltax- Mtouchslop;moverscrolleffect.setpull (Over/width);}}} Else{mlastmotionx = x;}} Else{moverscrolleffect.onrelease ();} break;} Case MotionEvent.ACTION_UP:case Motionevent.action_cancel:{callsuper = True;mactivepointerid = invalid_pointer_id; Moverscrolleffect.onrelease (); break;} Case Motionevent.action_pointer_up:{final int pointerindex = (ev.getaction () & Motionevent.action_pointer_index_ MASK) >> motionevent.action_pointer_index_shift;final int pointerid = Motioneventcompat.getpointerid (EV, Pointerindex); if (Pointerid = = Mactivepointerid) {//This is our active pointer going up. Choose a new//active pointer and adjust accordingly.final int newpointerindex = Pointerindex = 0? 1:0;mlastmotionx = Ev.getx (newpointerindex); Mactivepointerid = Motioneventcompat.getpointerid (EV, newpointerindex); callSuper = true;} Break;}} if (moverscrolleffect.isoverscrolling () &&!callsuper) {return true;} Else{try{return super.ontouchevent (EV);} catch (IllegalArgumentException Ignore) {}catch (ArrayIndexOutOfBoundsException ignore) {}return false;}} @Overrideprotected Boolean getchildstatictransformation (View child, transformation t) {if (child.getwidth () = = 0) { return false;} Final Boolean isfirstorlast = Mscrollposition = = 0 | | (Mscrollposition = = (Getadapter (). GetCount ()-1)); if (moverscrolleffect.isoverscrolling () && isfirstorlast) { Final float dx = getwidth ()/2;final int dy = getheight ()/2;t.getmatrix (). Reset (); final float Translatez = (float) (mOv Erscrolltranslation * Math.sin (math.pi* math.abs (Moverscrolleffect.moverscroll))); final float degrees = 90/ moverscrollrotation-(float) ((RADIANS * Math.acos (Moverscrolleffect.moverscroll))/moverscrollrotation); Mcamera.save (); Mcamera.Rotatey (degrees); mcamera.translate (0, 0, Translatez); Mcamera.getmatrix (T.getmatrix ()); Mcamera.restore (); T.getmatrix (). Pretranslate (-DX,-dy), T.getmatrix (). posttranslate (dx, dy); if (Manimatealpha) { T.settransformationtype (Transformation.type_both); T.setalpha ((Floatmath.sin ((float) ((1-math.abs ( Moverscrolleffect.moverscroll)))) (MATH.PI/2)));} return true;} else if (Mscrollpositionoffset > 0) {final float dx = getwidth ()/2;final float dy = getheight ()/2;double degrees = 0 ; Child.getlocalvisiblerect (Mtemptect); if (Mtemptect.left >= mscrollpositionoffsetpixels) {if (MAnimateAlpha) { T.settransformationtype (Transformation.type_both); T.setalpha ((Floatmath.sin (float) (Mscrollpositionoffset * MATH.PI/2)));} Right sidedegrees = (90/mswiperotation)-(RADIANS * Math.acos (Mscrollpositionoffset))/mswiperotation;} else if (Mtemptect.left = = 0) {if (Manimatealpha) {t.settransformationtype (Transformation.type_both); T.setalpha (( Floatmath.sin (float) (Mscrollpositionoffset * MATH.PI/2 +MATH.PI/2)));} Left sidedegrees =-(90/mswiperotation) + (RADIANS * Math.acos (1-mscrollpositionoffset))/mswiperotation;} Final float Translatez = (mswipetranslation * Floatmath.sin ((float) ((math.pi) * mscrollpositionoffset));//LOG.I (DEBUG _tag, visiblerect.left+ "," + Mscrollpositionoffsetpixels + ", degress =" +//String.Format ("%F.2", degrees)); T.getMatrix (). Reset (); Mcamera.save (); Mcamera.rotatey ((float) degrees); mcamera.translate (0, 0, Translatez); Mcamera.getmatrix ( T.getmatrix ()); Mcamera.restore ();//Pivot point is center of Childt.getmatrix (). Pretranslate (-DX,-dy); T.getmatrix (). Posttranslate (dx, dy);//Child.invalidate (); return true;} return false;}}
Using Viewpager3d in 2.Activity
Package Com.example.pageradapterdemo;import Java.util.arraylist;import Android.os.bundle;import Android.support.v4.app.fragmentactivity;import Com.focustech.common.widget.scrollviewpager.viewpager3d;public Class Mainactivity extends Fragmentactivity{private viewpager3d mviewpager;private commonfragmentadapter mAdapter; Private arraylist<myfragment> mlist; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitData (); InitView ();} private void Initview () {Mviewpager = (Viewpager3d) Findviewbyid (r.id.viewpager); madapter = new Commonfragmentadapter ( Mainactivity.this, Getsupportfragmentmanager (), mlist); Mviewpager.setadapter (Madapter);} private void InitData () {mlist = new arraylist<myfragment> (); Myfragment layout1 = new Myfragment ("one Fragment"); Myfragment Layout2 = new Myfragment ("both Fragment"); Myfragment layout3 = new Myfragment ("Three Fragment"), Mlist.add (LAYOUT1); Mlist.add (Layout2); Mlist.add (LayoUT3);}} 
3.CommonFragemntAdapter for Viewpager3d filling fragment

public class Commonfragmentadapter extends Fragmentstatepageradapter{private arraylist<myfragment> Fragmentlist;public Commonfragmentadapter (activity activity, Fragmentmanager FM, arraylist<myfragment> Fragmentlist) {super (FM); this.fragmentlist = Fragmentlist;} @Overridepublic Fragment getItem (int arg0) {return fragmentlist.get (arg0);} @Overridepublic int GetCount () {return fragmentlist.size ();} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {Super.destroyitem (container, Position, object);}}
Summary: Use Viewpager3d as a normal viewpager.


Android Custom Viewpager--3d effect app

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.