Android 1 minutes teach you to create a cool boot page (for Viewpager fade-in and fade-out transitions)

Source: Internet
Author: User

Pure hand-made Android boot page, achieve Viewpager switching fade in (page does not move!) ) Toggle and text animation.

Here is the effect demo:


Realization of ideas + mental process ... :

In fact, everything else is quite simple, is this viewpager fade switch animation is tricky, has not done before, and then went online for a long time.

Which encountered a variety of pits, about 90% of the people are cited Jazzyviewpager bag and then Balabala said that he realized a variety of functions, really drunk ....

The conclusion is that the realization of this effect can not be found at home ....

And then, in GitHub's Jazzyviewpager bag, I didn't see the effect I needed to achieve: fade in and out of the page! No swipe! Don't move!

But finally found out the way ...

Yes, that's right. Pagetransformer, in which the right view does not move when you swipe right by controlling the position of the view in the left side. (explained in detail below).

Code implementation:

Guideactivity.java

Package Com.whale.nangua.toquan;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmentpageradapter;import Android.support.v4.view.viewpager;import Android.view.layoutinflater;import Android.view.view;import Android.view.windowmanager;import Android.widget.imagebutton;import Android.widget.textview;import Com.whale.nangua.toquan.frag.guide1fragment;import Com.whale.nangua.toquan.frag.guide2fragment;import Com.whale.nangua.toquan.frag.guide3fragment;import Com.whale.nangua.toquan.frag.guide4fragment;import Com.whale.nangua.toquan.view.ngguidepagetransformer;import Java.util.arraylist;import java.util.List;/** * Created by Nangua on 2016/8/1. */public class Guideactivity extends Fragmentactivity implements Viewpager.onpagechangelistener,view.onclicklistener    {//main interface Adapter private Fragmentpageradapter Guidepageradapter;    All tab private list<view> views; Fragment each fragment as a layout PrivaTe arraylist<fragment> fragments;    Navigation tab Private Viewpager VP; Four choice button ID Private int[] choicebtnids = {R.id.imgbtn_guide_choice1, r.id.imgbtn_guide_choice2, r.id.img    Btn_guide_choice3, R.id.imgbtn_guide_choice4};    Four choice buttons private imagebutton[] choicebtns;    Move Right button private ImageButton btn_guide_next;    Skip button private TextView btn_guide_skip;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_guide);        Create a Fragment collection fragments = new arraylist<fragment> ();    Initview ();        } private void Initview () {WindowManager wm = This.getwindowmanager ();    Screenwith = Wm.getdefaultdisplay (). GetWidth ();        Screen width//Right shift button Btn_guide_next = (ImageButton) Findviewbyid (R.id.btn_guide_next);        Btn_guide_next.setonclicklistener (this); Skip Btn_guide_skip = (TextView) Findviewbyid (r.id.btn_GUIDE_SKIP);        Btn_guide_skip.setonclicklistener (this);        Bind button Component Choicebtns = new Imagebutton[4];            for (int i = 0; i < 4; i++) {final int j = i;            Choicebtns[i] = (ImageButton) Findviewbyid (Choicebtnids[i]); Choicebtns[i].setonclicklistener (New View.onclicklistener () {@Override public void OnClick (                View v) {Changetagview (j);        }            });        } layoutinflater Inflater = Layoutinflater.from (this);        Add sliding views = new arraylist<> ();        Views.add (inflater.inflate (r.layout.fragment_guide1, null));        Views.add (inflater.inflate (R.layout.fragment_guide2, null));        Views.add (inflater.inflate (r.layout.fragment_guide3, null));        Views.add (inflater.inflate (R.LAYOUT.FRAGMENT_GUIDE4, null));        VP = (Viewpager) Findviewbyid (r.id.vp_guide); Guidepageradapter = new Fragmentpageradapter (Getsupportfragmentmanager ()) {            @Override public int GetCount () {return fragments.size ();            } @Override public Fragment getItem (int arg0) {return fragments.get (arg0);        }        };        Declare an instance of each tab guide1fragment guide1fragment = new Guide1fragment ();        Guide2fragment guide2fragment = new Guide2fragment ();        Guide3fragment guide3fragment = new Guide3fragment ();        Guide4fragment guide4fragment = new Guide4fragment ();        Fragments.add (guide1fragment);        Fragments.add (guide2fragment);        Fragments.add (guide3fragment);        Fragments.add (guide4fragment);        Ngguidepagetransformer = new Ngguidepagetransformer ();        Ngguidepagetransformer.setcurrentitem (this, 0, fragments);        Vp.setpagetransformer (True, Ngguidepagetransformer);        Vp.setadapter (Guidepageradapter);        Vp.setonpagechangelistener (this); Note that setting the page is the number of cached pages, and the number of hours will occur fragment repeated loading problems VP.SETOFFSCReenpagelimit (4);    } Ngguidepagetransformer Ngguidepagetransformer;    Translationinterface Tempfrag; @Override public void OnClick (View v) {switch (V.getid ()) {///Next case R.id.btn_guide_nex                T:int NextPage;                if (nowpage = = 3) {nextPage = 0;                }else {nextPage = nowpage+1;                } onpageselected (NextPage);                Changetagview (NextPage);            Break        Skip case R.ID.BTN_GUIDE_SKIP://todo skip Operation break;    }} public interface Translationinterface {void translation (float x); } int screenwith;//Screen width/** * TODO Animation modified * @param position * @param positionoffset * @param position Offsetpixels */@Override public void onpagescrolled (int position, float positionoffset, int positionoffsetpixel s) {//If right switch (positiOn) {Case 0:tempfrag = (guide1fragment) fragments.get (0);                    Tempfrag.translation (Positionoffsetpixels);                Break                    Case 1:tempfrag = (guide2fragment) fragments.get (1);                    Tempfrag.translation (Positionoffsetpixels);                Break                    Case 2:tempfrag = (guide3fragment) fragments.get (2);                    Tempfrag.translation (Positionoffsetpixels);                Break                    Case 3:tempfrag = (guide4fragment) fragments.get (3);                    Tempfrag.translation (Positionoffsetpixels);            Break    }} private int nowpage = 0; /** * Custom Button selection method * @param position */private void Pagecheck (int position) {choicebtns[position].se        Tbackgroundresource (R.drawable.shape_guide_choice); for (int i = 0;i<4;i++) {if (i!= position) {Choicebtns[i].setbackgroundresource (r.drawable.shape_guide_unchoice);        }}} @Override public void onpageselected (int position) {nowpage = position;        Ngguidepagetransformer.setcurrentitem (position);    Pagecheck (position); } @Override public void onpagescrollstatechanged (int state) {}//replace label private void Changetagview (int chan    GE) {Vp.setcurrentitem (change, false); }}

The layout of the overall page, which compares the important points when setting the Viewpager Pagetransformer, and the effect of setting the text animation.

The following are the most important classes: Pagetransformer.java

Package Com.whale.nangua.toquan.view;import Android.content.context;import Android.support.v4.app.fragment;import Android.support.v4.view.viewpager;import Android.view.view;import Java.util.arraylist;public Class    Ngguidepagetransformer implements Viewpager.pagetransformer {private static final float Min_alpha = 0.0f;    Minimum transparency public void transformpage (view view, float position) {int pagewidth = View.getwidth ();            Get view wide if (position <-1) {//[-infinity,-1]//This page is the off-screen of the left screen        View.setalpha (0); } else if (position <= 1) {//[ -1,1] if (position < 0) {//Disappeared page View.sett  Ranslationx (-pagewidth * position);        Block disappear page Slide} else {//page View.settranslationx (pagewidth) appears;  Directly set the page that appears in the end View.settranslationx (-pagewidth * position); Prevent page Sliding}//Fade the page relative to Its size.            float Alphafactor = Math.max (Min_alpha, 1-math.abs (position));        Transparency Change Log view.setalpha (alphafactor);    } else {//(1,+infinity]//This page is the off-screen of the right.        Out of the right screen View.setalpha (0); }} int nowpostion = 0;    Current page context context;    Arraylist<fragment> fragments; public void Setcurrentitem (context context, int nowpostion, arraylist<fragment> fragments) {this.nowpostion        = Nowpostion;        This.context = context;    This.fragments = fragments;    } public void Setcurrentitem (int nowpostion) {this.nowpostion = nowpostion; }}

The implementation of the idea of the note is also more detailed, but I still explain:

is to first determine the position of the slide, if the right swipe, the first to block the current page (disappear ing page) with the characteristics of the Viewpager to the left to slide, and the right to appear on the left side of the interface to slide (before first set the right side of the screen directly to the left). It is important to note that the location is differentiated and the left and right sides are judged by position.





Android 1 minutes teach you to create a cool boot page (for Viewpager fade-in and fade-out transitions)

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.