Viewpager achieves carousel image effects

Source: Internet
Author: User

In many apps, we have the carousel effect, especially for some e-commerce apps. Most of them have this function. Today we simply imitate it and there is a small automatic beating function, this can be implemented by using a timer. If you don't need to talk about it, directly use the code:

Import Java. util. arraylist; import Java. util. concurrent. executors; import Java. util. concurrent. scheduledexecutorservice; import Java. util. concurrent. timeunit; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. support. v4.view. pageradapter; import android. support. v4.view. viewpager; import android. support. v4.view. viewpager. onpagechangelis Tener; import android. view. view; import android. view. viewgroup; import android. widget. imageview; import android. widget. textview; public class mainactivity extends activity {private int imageids []; private string [] titles; private arraylist <imageview> images; private arraylist <View> dots; private textview title; private viewpager mviewpager; private viewpageradapter adapter; private int oldposition = 0; // record the previous Location of the next point: Private int currentitem; // Private scheduledexecutorservice on the current page; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); // image ID imageids = new int [] {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e}; // image title titles = new string [] {"Gong Li is not vulgar, so I can't be vulgar", "I'm back! Sing a classic old song to attract a chorus of thousands of people "," reveal how Beijing movies are upgraded "," letv TV edition big delivery "," hot-blooded anti-Kill "}; // The displayed image images = new arraylist <imageview> (); For (INT I = 0; I <imageids. length; I ++) {imageview = new imageview (this); imageview. setbackgroundresource (imageids [I]); images. add (imageview);} // The displayed vertex DOTS = new arraylist <View> (); dots. add (findviewbyid (R. id. dot_0); dots. add (findviewbyid (R. id. dot_1); dots. add (findviewbyid (R. id. dot_2); dots. add (findviewbyid (R. id. dot_3); dots. add (findviewbyid (R. id. dot_4); Title = (textview) findviewbyid (R. id. title); title. settext (Titles [0]); mviewpager = (viewpager) findviewbyid (R. id. vp); adapter = new viewpageradapter (); mviewpager. setadapter (adapter); mviewpager. setonpagechangelistener (New onpagechangelistener () {@ overridepublic void onpageselected (INT position) {Title. settext (Titles [position]); dots. get (oldposition ). setbackgroundresource (R. drawable. dot_normal); dots. get (position ). setbackgroundresource (R. drawable. dot_focused); oldposition = position; currentitem = position ;}@ overridepublic void onpagescrolled (INT arg0, float arg1, int arg2) {}@ overridepublic void merge (INT arg0) {}});} private class viewpageradapter extends pageradapter {@ overridepublic int getcount () {return images. size () ;}// whether the same image is @ overridepublic Boolean isviewfromobject (view arg0, object arg1) {return arg0 = arg1 ;}@ overridepublic void destroyitem (viewgroup view, int position, object) {view. removeview (images. get (position) ;}@ overridepublic object instantiateitem (viewgroup view, int position) {view. addview (images. get (position); Return images. get (position) ;}@ overrideprotected void onstart () {// todo auto-generated method stubsuper. onstart (); scheduledexecutorservice = executors. newsinglethreadscheduledexecutor (); // switches an image scheduledexecutorservice every two seconds. schedulewithfixeddelay (New viewpagertask (), 2, 2, timeunit. seconds);} // switch the image private class viewpagertask implements runnable {@ overridepublic void run () {currentitem = (currentitem + 1) % imageids. length; // update the handler interface. obtainmessage (). sendtotarget () ;}} private handler = new handler () {@ overridepublic void handlemessage (Message MSG) {// set mviewpager on the current page. setcurrentitem (1 );}};}

:


Viewpager achieves carousel image effects

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.