Some uses of Android Viewpager and animation (i)

Source: Internet
Author: User

Android Viewpager is a control that can support gestures to switch view, which is good for user-guided pages:

If there are 4 graphs, let's write layout like this:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/whats_new_main_view "android:layout_width=" match_parent "android:layout_height=" match_p Arent "android:orientation=" vertical "android:background=" #FFFFFF "> <linearlayout android:layout_wi            Dth= "Fill_parent" android:layout_height= "60DP" android:orientation= "Horizontal" > <textview            Android:id= "@+id/tab_name" android:layout_width= "0DP" android:layout_height= "Wrap_content"            android:layout_gravity= "Center" android:layout_weight= "1" android:gravity= "center"             android:text= "First page" android:textcolor= "#000000" android:textsize= "14sp"/> <textview Android:id= "@+id/tab_name1" android:layout_width= "0DP" android:layout_height= "Wrap_conte NT "Android:layout_gravity= "Center" android:layout_weight= "1" android:gravity= "center" android:text= "second page" Android:textcolor= "#000000" android:textsize= "14sp"/> <textview android:id= "@+id/t Ab_name2 "android:layout_width=" 0DP "android:layout_height=" Wrap_content "Android:layout_            gravity= "Center" android:layout_weight= "1" android:gravity= "center" android:text= "third page" Android:textcolor= "#000000" android:textsize= "14sp"/> <textview androi D:id= "@+id/tab_name3" android:layout_width= "0DP" android:layout_height= "Wrap_content" and roid:layout_gravity= "Center" android:layout_weight= "1" android:gravity= "center" android:t ext= "The fourth page" android:textcolor= "#000000" android:textsize= "14sp"/> </LinearLayout> < !--img is a blue slider--      <imageview android:id= "@+id/img" android:layout_width= "@dimen/image_width" Android:layout_h eight= "4DP" android:background= "#0A84BD"/> <android.support.v4.view.viewpager android:id= "@ +id/vpager "android:layout_width=" wrap_content "android:layout_height=" 0DP "android:layout_gravity=" C Enter "android:layout_weight=" 1.0 "android:background=" #000000 "android:flipinterval=" "Android" Oid:persistentdrawingcache= "Animation"/></linearlayout>

Do the init job:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.pager_layout); mImageWidth = This.getresources (). Getdimensionpixelsize (r.dimen.image_width);D isplaymetrics dm = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); int screenw = Dm.widthpixels;//offset is the initial position of the img offset = ( SCREENW/4-mimagewidth)/2;img = (ImageView) Findviewbyid (r.id.img); Layoutparams layout = (layoutparams) img.getlayoutparams (); layout.leftmargin = Offset;initviewpager (); Initanimationforeachview (0);} private void Initviewpager () {viewpager= (Viewpager) Findviewbyid (R.id.vpager); views=new arraylist<view> (); Layoutinflater Inflater=getlayoutinflater (); View1=inflater.inflate (r.layout.layout_tutorial_1, null); view2= Inflater.inflate (r.layout.layout_tutorial_2, null); View3=inflater.inflate (r.layout.layout_tutorial_3, NULL); View4=inflater.inflate (r.layout.layout_tutorial_4, NULL);Add (View1); Views.add (VIEW2); Views.add (VIEW3); Views.add (VIEW4); Viewpager.setadapter (new Myviewpageradapter (views ); Viewpager.setcurrentitem (0); Viewpager.setonpagechangelistener (new Myonpagechangelistener ()); InitOther (); private void Initother () {textView1 = (TextView) Findviewbyid (r.id.tab_name); textView2 = (TextView) Findviewbyid ( R.ID.TAB_NAME1); textView3 = (TextView) Findviewbyid (r.id.tab_name2); textView4 = (TextView) Findviewbyid (r.id.tab_ NAME3); Textview1.setonclicklistener (new Myonclicklistener (0)); Textview2.setonclicklistener (New Myonclicklistener (1)); Textview3.setonclicklistener (New Myonclicklistener (2)); Textview4.setonclicklistener (New Myonclicklistener (3)); T1    _fixed = (ImageView) View1.findviewbyid (r.id.t1_fixed);    T1_icon1 = (ImageView) View1.findviewbyid (R.id.t1_icon1);    T1_icon2 = (ImageView) View1.findviewbyid (R.id.t1_icon2);      T1_next = (ImageView) View1.findviewbyid (R.id.t1_next);    t2_fixed = (ImageView) View2.findviewbyid (r.id.t2_fixed); T2_icon1 = (ImageView) view2. Findviewbyid (R.id.t2_icon1);        T2_next = (ImageView) View2.findviewbyid (R.id.t2_next);    t3_fixed = (ImageView) View3.findviewbyid (r.id.t3_fixed);    T3_next = (ImageView) View3.findviewbyid (R.id.t3_next);    Center_layout_3 = (relativelayout) View3.findviewbyid (r.id.center_layout_3);        T3_icon6 = (ImageView) View3.findviewbyid (R.ID.T3_ICON6);    t4_fixed = (ImageView) View4.findviewbyid (r.id.t4_fixed); T4_icon1 = (ImageView) View4.findviewbyid (r.id.t4_icon1);}

Then write the adapter and the corresponding listener:

Private class Myonclicklistener implements onclicklistener{private int index=0;        Public Myonclicklistener (int i) {index=i; }public void OnClick (View v) {viewpager.setcurrentitem (index);}} public class Myviewpageradapter extends Pageradapter{private list<view> mlistviews;public myviewpageradapter ( List<view> mlistviews) {this.mlistviews = mlistviews;} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {Container.removeview ( Mlistviews.get (position));} @Overridepublic Object Instantiateitem (viewgroup container, int position) {Container.addview (Mlistviews.get (position ), 0); return Mlistviews.get (position);} @Overridepublic int GetCount () {return mlistviews.size ();}    @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0==arg1;}}  public class Myonpagechangelistener implements onpagechangelistener{int one = offset * 2 + mimagewidth;int-one * 2;public void onpagescrollstatechanged (int arg0){}public void onpagescrolled (int arg0, float arg1, int arg2) {}public void onpageselected (final int arg0) {Animation anima tion = new Translateanimation (single * currindex, one * arg0, 0, 0);//Obviously this is relatively concise, with only a single line of code. Currindex = Arg0;animation.setfillafter (TRUE);//true: The picture stops at the end of the animation animation.setduration; Img.startanimation (        animation); Initanimationforeachview (arg0);} }

In the second chapter of the animation section, the following:


Code can be downloaded in http://download.csdn.net/detail/baidu_nod/7636351

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.