Android Viewpager Multi-page swipe toggle and animation effects

Source: Internet
Author: User

What we can do with Viewpager:

1, Viewpager make the first use of the application of the navigation introduced, plus the effect of the bottom dot, such as a lot of navigation.

2, page card sliding, plus the effect of the menu, generally these support gestures also support the bottom icon click .

3, Viewpager+fragment can make multi-page sliding effect, let our application interface operation more flexible.

First look at a few:

1, the use of navigation, plus the effect of the bottom dot, such as Sina.



2 . Slide the page card, add the effect of the menu.


3,Viewpager+fragment can make multi-page sliding effect.



Two, we now mainly talk about the second way: page card sliding, plus the effect of the menu.

Viewpager. It is a class of an add-on package that comes with the Google SDK and can be used for switching between screens.

This add-on is Android-support-v4.jar, which is a software package that Google has provided to us that is compatible with the low-version Android device, which is encapsulated with APIs that are available only on Android 3.0. And Viewpager is one of them. Using it, we can do a lot of things, from the simplest navigation, to the page menu and so on. So how to use it, like Lisstview, we also need an adapter, he is pageradapter.

Viewpager's function is to make the view slide, just as the lanucher slides around.

1, we first do the interface,

Interface design is simple, the first line of three headers, the second action to draw pictures, the third row of page card content display.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Xmlns:umadsdk=" http://schemas.android.com/apk/res/com. Lovebus "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G    T <linearlayout android:id= "@+id/linearlayout1" android:layout_width= "Fill_parent" android:layout_he            ight= "100.0dip" android:background= "#FFFFFF" > <textview android:id= "@+id/text1"            Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:layout_weight= "1.0" android:gravity= "Center" android:text= "page card 1" android:textcolor= "#000000" Androi D:textsize= "22.0dip"/> <textview android:id= "@+id/text2" android:layout_width= "Fill_pa Rent "android:layout_height=" fill_parent "android:layout_weight= "1.0" android:gravity= "Center" android:text= "page Card 2" android:textcolor= "#000000" Android:textsize= "22.0dip"/> <textview android:id= "@+id/text3" android:layout_width= " Fill_parent "android:layout_height=" fill_parent "android:layout_weight=" 1.0 "Android:grav ity= "Center" android:text= "page Card 3" android:textcolor= "#000000" android:textsize= "22.0dip"/&    Gt </LinearLayout> <imageview android:id= "@+id/cursor" android:layout_width= "Fill_parent" an droid:layout_height= "wrap_content" android:scaletype= "Matrix" android:src= "@drawable/a"/> <android . Support.v4.view.ViewPager android:id= "@+id/vpager" android:layout_width= "Wrap_content" android:layou t_height= "wrap_content" android:layout_gravity= "Center" android:layout_weight= "1.0" Android:backgroun      D= "#000000"  Android:flipinterval= "" android:persistentdrawingcache= "animation"/></linearlayout> 

we want to show three page cards, so we also need three page card content interface design, here we only set the background color, can play a different role.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical"    android:background= "#158684" ></LinearLayout>

2, the Code implementation part

(1) Define the variable first

Private Viewpager mpager;//page card content    private list<view> listviews;//tab page List    private ImageView cursor;// Animated pictures    private TextView t1, T2, t3;//tab header    private int offset = 0;//animated picture offset    private int currindex = 0;//Current page Card number    private int bmpw;//animated picture width

(2) Initialization of the header

/**     * Init header */    private void Inittextview () {        T1 = (TextView) Findviewbyid (R.ID.TEXT1);        T2 = (TextView) Findviewbyid (R.ID.TEXT2);        T3 = (TextView) Findviewbyid (R.ID.TEXT3);        T1.setonclicklistener (new Myonclicklistener (0));        T2.setonclicklistener (New Myonclicklistener (1));        T3.setonclicklistener (New Myonclicklistener (2));    }


/**     * Head punctuation Listener */Public    class Myonclicklistener implements View.onclicklistener {        private int index = 0;        Public Myonclicklistener (int i) {            index = i;        }        @Override public        void OnClick (View v) {            Mpager.setcurrentitem (index);        }    };

(3) Initialize the content area of the page card
/**     * Initialize viewpager*/    private void Initviewpager () {        Mpager = (viewpager) Findviewbyid (R.id.vpager);        ListViews = new arraylist<view> ();        Layoutinflater minflater = Getlayoutinflater ();        Listviews.add (minflater.inflate (r.layout.lay1, null));        Listviews.add (minflater.inflate (r.layout.lay2, null));        Listviews.add (minflater.inflate (r.layout.lay3, null));        Mpager.setadapter (New Mypageradapter (ListViews));        Mpager.setcurrentitem (0);        Mpager.setonpagechangelistener (New Myonpagechangelistener ());    }

We loaded the three page card interface, the first page card is displayed by default. Here we also need to implement an adapter, where we implement the loading and unloading of each page card

< /span>

/** * Viewpager Adaptor */public class Mypageradapter extends Pageradapter {public list<view> mlistviews;        Public Mypageradapter (list<view> mlistviews) {this.mlistviews = Mlistviews; } @Override public void Destroyitem (View arg0, int arg1, Object arg2) {(Viewpager) arg0). Remove        View (Mlistviews.get (arg1));            } @Override public void Finishupdate (View arg0) {} @Override public int getcount () {        return Mlistviews.size (); } @Override Public Object instantiateitem (View arg0, int arg1) {(Viewpager) arg0). AddView (mlist            Views.get (Arg1), 0);        Return Mlistviews.get (ARG1);        } @Override public boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = (arg1);        } @Override public void Restorestate (parcelable arg0, ClassLoader arg1) {} @Override Public parceLable saveState () {return null; } @Override public void Startupdate (View arg0) {}}


(3) Initializing animations

/**     * Initialize animation */    private void Initimageview () {        cursor = (ImageView) Findviewbyid (r.id.cursor);        BMPW = Bitmapfactory.decoderesource (Getresources (), R.drawable.a)                . getwidth ();//Get Picture width        displaymetrics dm = New Displaymetrics ();        Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);        int screenw = dm.widthpixels;//Get resolution width        offset = (SCREENW/3-BMPW)/2;//calculate offset        Matrix matrix = new Matrix (); 
   matrix.posttranslate (offset, 0);        Cursor.setimagematrix (matrix);//Set animation initial position    }

implement page-card switching monitoring

< Span style= "FONT-FAMILY:VERDANA,ARIAL,HELVETICA,SANS-SERIF; Line-height:14.399999618530273px ">

/** * Page Card Toggle Monitor */public class Myonpagechangelistener implements Onpagechangelistener {int one = offset * 2 + bmpw;//page Card 1, page Card 2 offset int, one * 2;//page card 1, page Card 3 offset @Override public void onpageselected (int arg0)            {Animation Animation = null; Switch (arg0) {case 0:if (currindex = = 1) {animation = new Translateanimat                Ion (one, 0, 0, 0);                } else if (Currindex = = 2) {animation = new Translateanimation (0, 0, 0);            } break;                Case 1:if (Currindex = = 0) {animation = new Translateanimation (offset, one, 0, 0);                } else if (Currindex = = 2) {animation = new Translateanimation (both, one, 0, 0);            } break; Case 2:if (Currindex = = 0) {animation = new Translateanimation (offset, 0, 0);                } else if (Currindex = = 1) {animation = new Translateanimation (one, 0, 0);            } break;            } currindex = arg0;            Animation.setfillafter (TRUE);//true: The picture stops at the end of the animation animation.setduration (300);        Cursor.startanimation (animation); } @Override public void onpagescrolled (int arg0, float arg1, int arg2) {} @Override PU Blic void onpagescrollstatechanged (int arg0) {}}


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.