Android AD Carousel Diagram implementation

Source: Internet
Author: User

First look at the effect

The first step, the layout

<?xml version= "1.0" encoding= "Utf-8"?><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.example.xw.myviewpager.MainActivity" ><relativelayout android:layout_width= "Match_parent"Android:layout_height= "180DP" ><Android.support.v4.view.ViewPager Android:id= "@+id/vp"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" ></android.support.v4.view.ViewPager> <LinearLayout android:layout_width= "Match_parent"Android:layout_alignparentbottom= "true"android:orientation= "Horizontal"Android:background= "#66000000"Android:layout_height= "30DP" > <LinearLayout Android:id= "@+id/point_container"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"android:layout_gravity= "Center"android:gravity= "center" > </LinearLayout> &LT;/LINEARLAYOUT&GT;&LT;/RELATIVELAYOUT&GT;&LT;/RELATIVELAYOUT&G T

The point_container here are used to make small dots of containers

Two dots shape and state selector

Bg_shape.xml

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

Bg_shape2.xml

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

Selector.xml

"1.0" encoding= "Utf-8"? ><selector xmlns:android= "Http://schemas.android.com/apk/res/android" >    < Item android:state_enabled= "True" android:drawable= "@drawable/bg_shape"/>    <item android:state_enabled= " False "  android:drawable=" @drawable/bg_shape2 "/></selector>

Enable= "False" is a gray dot, when state_enabled= "true" is a white dot

Three mainactivity

ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;Importjava.util.ArrayList; Public classMainactivityextendsAppcompatactivityImplementsviewpager.onpagechangelistener{PrivateViewpager VP; Private  int[] imas; PrivateLinearLayout ll; PrivateArraylist<imageview>imalist; PrivateBoolean Flag; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Initialize Layoutinitviews (); //DataInitData (); //AdapterInitadapter (); Flag=true; //Implement automatic Carousel        NewThread (NewRunnable () {@Override Public voidrun () { while(flag) {Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } runonuithread (NewRunnable () {@Override Public voidrun () {Vp.setcurrentitem (Vp.getcurrentitem ()+ 1);                }                        });    }}). Start (); }    Private voidInitadapter () {Ll.getchildat (0). setenabled (true); Vp.setadapter (NewMyadapter ()); Vp.setcurrentitem (Integer.max_value/2+2);//This starts with a number of%5 0, so opening defaults will be displayed in the No. 0 item} @Overrideprotected voidOnDestroy () {Super. OnDestroy (); Flag=false; }    Private voidInitData () {}Private voidinitviews () {VP=(Viewpager) Findviewbyid (R.ID.VP); Vp.setonpagechangelistener ( This); ll=(LinearLayout) Findviewbyid (R.id.point_container); //Image ArrayIMAS =New int[]{r.drawable.a, r.drawable.b, r.drawable.c, R.DRAWABLE.D, R.DRAWABLE.E,};        ImageView IV; Imalist=NewArraylist<imageview>();        View Pointview;  for(inti = 0; i < imas.length; i++) {IV=NewImageView ( This);            Iv.setimageresource (Imas[i]);            Imalist.add (iv); //Add small white dotsPointview =NewView ( This); Pointview.setenabled (false);            Pointview.setbackgroundresource (R.drawable.selector); Linearlayout.layoutparams params=NewLinearlayout.layoutparams (15,15); //params.gravity= Gravity.center;            if(i!=0) Params.leftmargin=10;        Ll.addview (Pointview, params); }} @Override Public voidOnpagescrolled (intPositionfloatPositionoffset,intpositionoffsetpixels) {} @Override Public voidOnpageselected (intposition) {         for(intI=0;i<ll.getchildcount (); i++) {Ll.getchildat (i). setenabled (false); } ll.getchildat (Position%5). setenabled (true);//set Small white dots} @Override Public voidOnpagescrollstatechanged (intState ) {    }    classMyadapterextendsPageradapter {@Override PublicObject Instantiateitem (ViewGroup container,intposition) {           intNewposition =position%5;//get a new locationImageView imageview=Imalist.get (newposition);            Container.addview (ImageView); returnImageView;//To return the View object to the frame, you must override the} @Override Public voidDestroyitem (ViewGroup container,intposition, Object object)        {Container.removeview (View) object); } @Override Public intGetCount () {returnInteger.max_value; } @Override//Judgment Multiplexing         Public Booleanisviewfromobject (View view, Object object) {returnview==object; }    }}

Android AD Carousel Map Implementation

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.