Android Banner Scrolling effect

Source: Internet
Author: User

One

Second, the main technical points

Use of 1.ViewPager

2. Background Selector

3. Auto Play

Three, sample code

1. layout file: Activity_main.xml

<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= "200DP" > <Android.support.v4.view.ViewPager Android:id= "@+id/vp_show"Android:layout_width= "Match_parent"Android:layout_height= "200DP"/> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"Android:background= "#33000000"android:gravity= "Center"android:orientation= "Vertical" > <TextView Android:id= "@+id/tv_description"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "title"android:textsize= "16SP"Android:textcolor= "#FFFFFFFF"/> <LinearLayout Android:id= "@+id/ll_viewgroup"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > </LinearLayout> </LinearLayout></RelativeLayout>

2. Picture below the indicator point background imageview_point_normal.xml,imageview_point_focused.xml,imageview_selector.xml

(1) Imageview_point_normal.xml

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"     Android:shape= "Oval" >    <size        android:height= "5DP"        android:width = "5DP"/>    <solid android:color= "#55000000"/></shape>

(2) Imageview_point_focused.xml

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"     Android:shape= "Oval" >    <size        android:height= "5DP"        android:width = "5DP"/>    <solid android:color= "#AAFFFFFF"/></shape>

(3) Imageview_selector.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:drawable=" @drawable/imageview_point_focused "android:state_enabled=" true "> </item>    <item android:drawable= "@drawable/imageview_point_normal" android:state_enabled= "false" > </item></selector>

3. Main Page Mainactivity.java

 PackageCom.gnnuit.banner;Importjava.util.ArrayList;Importjava.util.List;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.support.v4.view.ViewPager.OnPageChangeListener;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.LinearLayout.LayoutParams;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateViewpager vp_show; PrivateTextView tv_description; PrivateLinearLayout Ll_viewgroup; Private Final int[] IDs ={r.drawable.a, r.drawable.b, r.drawable.c, R.DRAWABLE.D, R.DRAWABLE.E}; Private FinalString[] Descriptions = {"Gong Li is not vulgar, I can not vulgar", "flutter the tree back!" Re-sing the classic old songs cited million people chorus "," secret Beijing movie How to Upgrade "," Le video network TV version of the big delivery "," blood cock silk anti-killing " }; PrivateList<imageview> imageviewlist =NewArraylist<imageview>(); Private intlastposition = 0;//record the location of the previous page    Private BooleanIsRunning =false;//record Viewpager is playing@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Vp_show=(Viewpager) Findviewbyid (r.id.vp_show); Tv_description=(TextView) Findviewbyid (r.id.tv_description); Ll_viewgroup=(LinearLayout) Findviewbyid (R.id.ll_viewgroup); //Initializing Resources         for(inti = 0; i < ids.length; i++) {            //initializing a picture resourceImageView IV =NewImageView ( This);            Iv.setbackgroundresource (Ids[i]);            Imageviewlist.add (iv); //Initialize indicator pointsImageView Iv_point =NewImageView ( This); Linearlayout.layoutparams params=Newlayoutparams (layoutparams.wrap_content, layoutparams.wrap_content); Params.rightmargin= Px2dip ( This, 10); Params.topmargin= Px2dip ( This, 5);            Iv_point.setlayoutparams (params);            Iv_point.setbackgroundresource (R.drawable.imageview_selector); Iv_point.setenabled (false);        Ll_viewgroup.addview (Iv_point); } tv_description.settext (descriptions[0]); Ll_viewgroup.getchildat (0). setenabled (true); //set the adapter of ViewpagerVp_show.setadapter (NewMyadapter ()); //set the Onpagechangelistener event for ViewpagerVp_show.setonpagechangelistener (NewOnpagechangelistener () {/*** Perform when a new page is displayed*/@Override Public voidOnpageselected (intposition)                {Tv_description.settext (descriptions[position]); Ll_viewgroup.getchildat (position). setenabled (true); Ll_viewgroup.getchildat (lastposition). setenabled (false); Lastposition=position; }            /*** Called when the page is sliding*/@Override Public voidOnpagescrolled (intPositionfloatPositionoffset,intpositionoffsetpixels) {            }            /*** Called when the page status has changed*/@Override Public voidOnpagescrollstatechanged (intState )        {            }        }); IsRunning=true; Myhandler.sendemptymessagedelayed (0, 2000); }    PrivateHandler MyHandler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {Vp_show.setcurrentitem (Vp_show.getcurrentitem ()+ 1)%imageviewlist.size ()); if(isrunning) {myhandler.sendemptymessagedelayed (0, 2000);    }        };    }; Private classMyadapterextendsPageradapter {/*** Total number of pages obtained*/@Override Public intGetCount () {returnimageviewlist.size (); }        /*** Determine the correspondence between view and object*/@Override Public Booleanisviewfromobject (View view, Object object) {returnView = =object; }        /*** Get the view * * in the appropriate location@paramcontainer * View of the container, is actually viewpager itself *@paramposition * Corresponding location*/@Override PublicObject Instantiateitem (ViewGroup container,intposition)            {Container.addview (Imageviewlist.get (position)); returnImageviewlist.get (position); }        /*** Destroy the object at the corresponding location*/@Override Public voidDestroyitem (ViewGroup container,intposition, Object object)            {Container.removeview (View) object); Object=NULL; }    }    /*** According to the resolution of the phone from PX (pixel) to the unit to become DP*/     Public Static intPx2dip (Context context,floatpxvalue) {        Final floatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Pxvalue/scale + 0.5f); }}

Android Banner Scrolling effect

Related Article

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.