| I. Overview of the problem |
Using the Viewpager control to achieve horizontal paging, horizontal switch pictures and other effects, but viewpager need to manually swipe to switch the page, the effect of the picture wheel is essentially in the Viewpager control on the basis of allowing it to automatically switch, so the implementation of the picture is the following steps:
1. Extended Viewpager Custom One Myscrollimageview class
2. Define adapter for Myscrollimageview, load picture information
3, define the picture sliding animation Time Control class
The next step is to implement the case:
| Second, the realization of routines |
1. Writing adapters for custom Viewpager controls
We'll start by writing an adapter for our custom Viewpager control that loads the picture that you want to turn on, and this is the same as using the Viewpager control to define the adapter for the adapter control.
Adapter public class Mypageradapter extends Pageradapter {private Activity mactivity;//Context Privat e list<view> mlistviews; Picture Group Public Mypageradapter () {} public mypageradapter (Activity mactivity,list<view> Mlistview s) {this.mactivity=mactivity; This.mlistviews=mlistviews; } public int GetCount () {if (mlistviews.size () = = 1) {//a picture without flow return Mlistviews.siz E (); } return integer.max_value; /** returns a picture element in the list loaded into the control */public Object Instantiateitem (View v, int i) {if (((VIEWPA GER) v). Getchildcount () = = Mlistviews.size ()) {((Viewpager) v). Removeview (Mlistvie Ws.get (i% mlistviews.size ())); } ((Viewpager) v). AddView (Mlistviews.get (i% mlistviews.size ()), 0); Return Mlistviews.get (i% mlistviews.size ()); } public Boolean Isviewfromobject (View arg0, Object arg1) {return arg0 = = (arg1); } public void Destroyitem (viewgroup view, int I, Object object) {View.removeview (Mlistviews.get (i%mlist Views.size ())); } }
2. Customizing a Myscrollimageview Class
Customizing a Myscrollimageview class, mainly extending a start (...) ) method, which allows the image to be switched on and off by time interval
public class Myimgscroll extends Viewpager {Activity mactivity;//context list<view> mlistviews;//Picture Group int m Scrolltime = 0; Timer timer; int oldindex = 0; int curindex = 0; Public Myimgscroll (context context, AttributeSet Attrs) {Super (context, attrs); }/** * Start advertising scrolling * * @param mainactivity * Display the main interface of the advertisement * @param imglist * Picture column Table, cannot be null, at least one * @param scrolltime * scrolling interval, 0 for no scrolling * @param ovallayout * dot container, can be empty, Li Nearlayout type * @param ovallayoutid * ovallayout is empty when writing 0, dot layout XMl * @param ovallayoutitemid * Ovallayout is empty when writing 0, dot layout xml dot xml under View ID * @param focusedid * ovallayout is empty when writing 0, Dot layout Animation when XML is selected * @param normalid * ovallayout is empty when writing 0, dot layout xml normal background */public void start (activi Ty Mainactivity, list<view> imglist, int scrolltime, linearlayout ovallayout, int oVallayoutid, int ovallayoutitemid, int focusedid, int normalid) {mactivity = mainactivity; Mlistviews = imglist; Mscrolltime = Scrolltime; Set dot Setovallayout (Ovallayout, Ovallayoutid, Ovallayoutitemid, Focusedid, normalid); This.setadapter (New Mypageradapter (mactivity,mlistviews));//Set adapter if (Scrolltime! = 0 && imglist.size () &G T 1) {//Set the sliding animation time, if the default animation time can not be used, the reflection technology implementation of new Fixedspeedscroller (mactivity). Setduration (this, 700); Starttimer (); Stop scrolling when Touch This.setontouchlistener (new Ontouchlistener () {public boolean OnTouch (View V, Motion Event event) {if (event.getaction () = = motionevent.action_up) {Starttimer (); } else {Stoptimer (); } return false; } }); } if (MlistvieWs.size () > 1) {this.setcurrentitem ((INTEGER.MAX_VALUE/2)-(INTEGER.MAX_VALUE/2)% Mlistviews.size ());//Set selected as middle/Picture as and No. 0}}//Set dot private void Setovallayout (final linearlayout Ovallayo UT, int ovallayoutid, final int ovallayoutitemid, final int focusedid, final int normalid) {if (ovallay Out! = null) {Layoutinflater inflater=layoutinflater.from (mactivity); for (int i = 0; i < mlistviews.size (); i++) {Ovallayout.addview (inflater.inflate (Ovallayoutid, null)); }//Select the first ovallayout.getchildat (0). Findviewbyid (Ovallayoutitemid) . Setbackgroundresource (Focusedid); This.setonpagechangelistener (New Onpagechangelistener () {public void onpageselected (int i) { Curindex = i% mlistviews.size (); Cancels the dot check Ovallayout.getchildat (oldindex). FindviEwbyid (Ovallayoutitemid). Setbackgroundresource (Normalid); Dot check Ovallayout.getchildat (curindex). Findviewbyid (Ovallayoutitemid). SetBackground Resource (Focusedid); Oldindex = Curindex; } public void onpagescrolled (int arg0, float arg1, int. arg2) {} public void onpagescrollstatechanged (int arg0) {}}); }}/** * obtained when the subscript is selected * @return */public int getcurindex () {return curindex; }/** * Stops scrolling */public void Stoptimer () {if (timer! = null) {Timer.cancel (); timer = null; }}/** * starts scrolling */public void Starttimer () {timer = new timer (); Timer.schedule (New TimerTask () {public void run () {Mactivity.runonuithread (new Runnable () { public voidRun () {MyImgScroll.this.setCurrentItem (myimgscroll.this. GetCurrent Item () + 1);//Set Control current item (change picture)}); }}, Mscrolltime, mscrolltime); }}
3, define the picture sliding animation Time Control class
Package Com.tianshicoffeeom.app.imgscroll;import Java.lang.reflect.field;import Android.content.context;import Android.support.v4.view.viewpager;import Android.view.animation.interpolator;import Android.widget.Scroller; /** * Picture Sliding animation Time Control class, if the default time can not use this class, through the reflection technology to change the Viewpager sliding time * */public class Fixedspeedscroller extends Scroller { Private context context; private int mduration = 500; Public Fixedspeedscroller (Context context) {super (context); This.context=context; } public Fixedspeedscroller (context context, Interpolator Interpolator) {Super (context, interpolator); This.context=context; }/** * Set the sliding time to change the Viewpager * @param VP Viewpager Object * @param time * */public void Setduratio N (Viewpager vp,int time) {try {Field field = ViewPager.class.getDeclaredField ("Mscroller"); Field.setaccessible (TRUE); This.setmduration (time);//Set the Turn Field.sET (VP, this); } catch (Exception e) {}} @Override public void startscroll (int startX, int starty, int dx, int dy, int duration) {//system.out.println ("startScroll1"); Super.startscroll (StartX, starty, dx, DY, mduration); } @Override public void startscroll (int startX, int starty, int dx, int dy) {//system.out.println ("sta RtScroll2 "); Super.startscroll (StartX, starty, dx, DY, mduration); } public void setmduration (int time) {mduration = time; } public int getmduration () {return mduration; } }
4, write mainactivity, test components
public class Mainactivity extends Activity {private myimgscroll Mypager;//Picture container private LinearLayout ovallayou T Dot container private list<view> listviews; Picture group @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mypager = (myimgscroll) Findviewbyid (R.ID.MYVP); Ovallayout = (linearlayout) Findviewbyid (R.id.vb); Initviewpager ();//Initialize picture//start scrolling Mypager.start (this, listviews, 4000, Ovallayout, r.layout.ad_b Ottom_item, R.id.ad_item_v, r.drawable.dot_focused, r.drawable.dot_normal); } @Override protected void Onrestart () {Mypager.starttimer (); Super.onrestart (); } @Override protected void OnStop () {Mypager.stoptimer (); Super.onstop (); }/** * Initialize picture */private void Initviewpager () {listviews = new arraylist<view> (); int[] Imageresid = new int[] {r.drawable.banner1, r.drawable. Banner2, r.drawable. Banner3, R.DRAWABLE.D, r.dr Awable. BANNER4}; for (int i = 0; i < imageresid.length; i++) {ImageView ImageView = new ImageView (this); Imageview.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {//Set Picture Click event Toast.maketext (Mainactivity.this, "clicked:" + Mypager.getcurindex (), Toast.length_short) . Show (); } }); Imageview.setimageresource (Imageresid[i]); Imageview.setscaletype (Scaletype.center_crop); Listviews.add (ImageView); } }}
5. mainactivity Layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" > <com.jereh.view. Myscrollimageview android:id= "@+id/myvp" android:layout_width= "fill_parent" android:layout_height= "120DP"/> <linearlayout android:id= "@+id/vb" android:layout_width= "Match_parent" android:layout_height= "10DP" android:layout_margintop= "3dip" android:gravity= "center" Android:o rientation= "Horizontal" > </LinearLayout></LinearLayout>
Finish!
Android for picture wheel effect--Custom Viewpager controls