Android Development Implementation Picture Auto scroll display label Viewpager

Source: Internet
Author: User

The effect of automatic scrolling of images in Android is very common, and we can animate them to achieve our functions. But there is a Viewpager class in Android that implements scrolling effects, Android-support-vx.jar in Android's Extras directory, x for version 4,7, and so on. We need to android.support.v4.view.ViewPager the Viewpager tag when we use it.

Blog Source : Http://blog.csdn.net/fengshizty

The code is very simple and does not have to be explained:


The XML layout file is as follows:

<relativelayout 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 "Tools:context        = "Com.andy.viewpagedemo.MainActivity" > <android.support.v4.view.viewpager android:id= "@+id/viewpager" Android:layout_width= "Wrap_content" android:layout_height= "200dip"/> <linearlayout android:lay        out_alignbottom= "@id/viewpager" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:background= "#33000000" android:orientation= "vertical" > <textview android:id= "@            +id/image_desc "android:layout_width=" match_parent "android:layout_height=" Wrap_content "            android:gravity= "center" android:text= "@string/app_name" android:textcolor= "@android: Color/white"       Android:textsize= "18sp"/> <linearlayout android:id= "@+id/point_group" android:layout_width= "Wrap_content" Andro             id:layout_height= "wrap_content" android:layout_gravity= "center" android:orientation= "Horizontal" > </LinearLayout> </LinearLayout></RelativeLayout>

The activity code is as follows:

Package Com.andy.viewpagedemo;import Java.util.arraylist;import Android.app.activity;import android.os.Bundle; Import Android.os.handler;import Android.support.v4.view.pageradapter;import Android.support.v4.view.ViewPager; Import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.view;import Android.view.viewgroup;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.textview;public class Mainactivity extends Activity {private Viewpager viewpager;private linearlayout Point_group;private TextView image_desc;//Picture Resources Idprivate Final int[] images = {r.drawable.a, r.drawable.b, r.drawable.c, R.DRAWABLE.D, r.drawable.e};//picture Title Collection private Final string[] imagedescriptions = {"Gong Li is not vulgar, I can't be vulgar", "flutter the tree and come back!" Re-sing the classic songs of the People Chorus "," secret Beijing movie How to Upgrade "," Le video network TV version of the big delivery "," blood cock silk anti-killing "};p rivate arraylist<imageview> imagelist;// Position of previous page protected int lastposition = 0;//Determines whether auto-scrolling viewpagerprivate boolean isrunning = true;private Handler Handler = new Handler () {public VOID handlemessage (android.os.Message msg) {//Perform swipe to next page Viewpager.setcurrentitem (Viewpager.getcurrentitem () + 1); isrunning) {//In a handler delay handler.sendemptymessagedelayed (0, 2000);}};}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) Viewpager = (Viewpager) Findviewbyid (r.id.viewpager);p Oint_group = (LinearLayout) Findviewbyid (r.id.point_group); Image_desc = (TextView) Findviewbyid (R.ID.IMAGE_DESC); Image_desc.settext ( Imagedescriptions[0]);//Initialize picture resource ImageList = new arraylist<imageview> (); for (int i:images) {//Initialize picture resource ImageView im Ageview = new ImageView (this), Imageview.setbackgroundresource (i); Imagelist.add (ImageView);//Add indication dot ImageView point = New ImageView (this); Linearlayout.layoutparams params = new Linearlayout.layoutparams (5,5);p arams.rightmargin = 20;point.setlayoutparams ( params);p Oint.setbackgroundresource (R.DRAWABLE.POINT_BG), if (i = = R.drawable.a) {point.setenabled (true);} else {poiNt.setenabled (false);} Point_group.addview (point);} Viewpager.setadapter (New MyPageAdapter ());//sets the position of the current Viewpager Viewpager.setcurrentitem (integer.max_value/2-( Integer.max_value/2% imagelist.size ()); Viewpager.setonpagechangelistener (new Onpagechangelistener () {@ overridepublic void onpageselected (int position) {//page switch after the call, position is the new page position//implement unrestricted loop playback position%= imagelist.size (); Image_desc.settext (Imagedescriptions[position]);//Set the current point to true to set the previous point to Falsepoint_group.getchildat (position). SetEnabled (True);p Oint_group.getchildat (lastposition). SetEnabled (false); lastposition = position;} @Overridepublic void onpagescrolled (int position, float positionoffset,int positionoffsetpixels) {//page sliding time callback}@ overridepublic void onpagescrollstatechanged (int state) {//when the PageView status changes, callback}});/** * Auto loop: 1. Timer: Timer 2. Open sub-thread: While True loop 3.ClockManger * 4. Use handler to send delay information, to realize the cycle, the simplest and most convenient * */handler.sendemptymessagedelayed (0, 2000);} @Overrideprotected void OnDestroy () {//stop scrolling isrunning = False;super.ondestroY ();} Private class MyPageAdapter extends Pageradapter {//need to implement the following four methods @overridepublic int GetCount () {//Get the total number of pages return INTEGER.M Ax_value;} @Overridepublic Boolean isviewfromobject (View view, Object object) {//To determine if the view and object correspond to an associated relationship if (view = = object) {return T Rue;} else {return false;}} @Overridepublic Object Instantiateitem (viewgroup container, int position) {//Get view at corresponding position, container view container, In fact, is viewpage itself,//Position:viewpager on the location//to container Add content Container.addview (imagelist.get (position% imagelist.size ()); return Imagelist.get (position% imagelist.size ());} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {//Destroy object//at the corresponding location Super.destroyitem (container, Position, object); Container.removeview ((View) object); object = null;}}}

The effect is as follows:




Android Development Implementation Picture Auto scroll display label Viewpager

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.