Android-loop AD Bit Component

Source: Internet
Author: User
<span id="Label3"></p><p><p>The circular ad bit is also a very common component, the network has a variety of implementations, that day saw the <span>Singwhatiwanna</span> of a realization, very simple, and then combined with a previously seen one, a little tidy up a bit.</p></p><p><p>Reprint please indicate source: http://blog.csdn.net/goldenfish1919/article/details/46811889<br></p></p><p><p>First look at how to use:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><com.xjs.demo.view.bannerviewandroid:id= "@+id/bannerview" android:layout_width= "match_parent" android:layout_ height= "150dp" android:paddingleft= "10dp" android:paddingright= "10dp" >< android.support.v4.view.viewpagerandroid:id= "@+id/banner_viewpager" android:layout_width= "match_parent" android: layout_height= "match_parent"/><com.xjs.demo.view.dotviewandroid:id= "@+id/banner_dotview" Android:layout_ Width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "bottom|center_horizontal" android:layout_marginbottom= "10dp" app:dot_number= "5" app:dot_radius= "4dp" app:dot_selected_color= "0xffffffff" app :d ot_span= "8dp" app:dot_unselected_color= "0x80ffffff"/></com.xjs.demo.view.bannerview></pre></pre><br>Custom bannerview, with two child elements inside, one viewpager and one custom dotview, when used:<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Bannerview banner = (bannerview) This.findviewbyid (r.id.bannerview); banner.setonbannerclicklistener (new Onbannerclicklistener () {@Overridepublic void onbannerclicked (int POS) {toast.maketext (mainactivity.this, " Onbannerclicklistener: "+ pos,toast.length_short)." Show ();}}); int[] imagessrc = new int[] {r.mipmap.img1, r.mipmap.img2,r.mipmap.img3, r.mipmap.img4, r.mipmap.img5};banner.update ( imagessrc);</pre></pre>You can add a click event to banner, then pass the image ID and call the banner update () METHOD.<p><p></p></p><p><p>Here's a look at the Bannerview:</p></p><p><p><br></p></p><p><p></p></p><pre name="code" class="java">public class Bannerview extends Framelayout{private dotview mbannerdotview;private Viewpager mbannerviewpager;private Banneradapter mbanneradapter;/** current position*/private int mbannerposition = 0;/**banner Post-click callback */private Onbannerclicklistener mbannerclicklistener;/** Auto Play related */private Handler Mhandler = new Handler ();p rivate Runnable task = New Runnable () {@Overridepublic void run () {mbannerposition = (mbannerposition + 1)% Mbanneradapter.getcoun T (); Mbannerviewpager.setcurrentitem (mbannerposition); LOG.D (TAG, "tname:" + thread.currentthread (). getName ()); mhandler.postdelayed (task, 3000);}}; private static final String TAG = BannerView.class.getSimpleName ();p ublic bannerview (context Context) {this (context, null);} Public Bannerview (context context, attributeset attrs) {this (context, attrs, 0);} Public Bannerview (context context, attributeset attrs, int defstyle) {super (context, attrs, defstyle); init (context);} private void Init (Context COntext) {} @Overrideprotected void onfinishinflate () {super.onfinishinflate ();//note The unspoken rules here Mbannerviewpager = (viewpager ) Getchildat (0); Mbannerdotview = (dotview) Getchildat (1); Mbannerdotview.setdotnumber (0); Mbanneradapter = new Banneradapter (getcontext (), new int[0]); Mbannerviewpager.setadapter (mbanneradapter); Mbannerviewpager.setonpagechangelistener (mbanneradapter);} public void update (int[] Imagessrc) {if (imagessrc = = NULL | | imagessrc.length <= 0) {return;} Mbannerdotview.setdotnumber (imagessrc.length); mbannerdotview.setselected (0); mbanneradapter.update (imagesSrc); mhandler.postdelayed (task, 3000);} private void Setindicator (int Position) {position%= mbanneradapter.getsize (); Mbannerdotview.setselected (position); }private int mdownx; private int mdowny; Private Long mdowntime; @Override public boolean dispatchtouchevent (motionevent event) {int action = Event.getac tion (); if (action = = Motionevent.actioN_down) {mhandler.removecallbacks (task); Mdownx = (int) Event.getx (); Mdowny = (int) event.gety (); Mdowntime = System.currenttimemillis (); } else if (action = = Motionevent.action_up) {if (system.currenttimemillis ()-mdowntime < && Ma Th.abs (mdownx-event.getx ()) < 5 && math.abs (mdowny-event.gety ()) < 5) {//interface callback If (mbannerclicklistener! = Null) {mbannerclicklistener.onbannerclicked (mbannerposition%mbanneradapt Er.getsize ()); }} mhandler.postdelayed (task, 3000); } else if (action = = Motionevent.action_cancel) {mhandler.postdelayed (task, 3000); } else if (action = = Motionevent.action_move) {//do nothing} return super.dispatchtouchevent (event); }private class Banneradapter extends Pageradapter implements Viewpager.onpagechangelistener {private Context mcontext; Private int[] mimagessrc; private int msize; private int mfakesize; Public banneradapter (context context, int[] imagessrc) {mcontext = context; Update (mimagessrc); public void update (int[] Imagessrc) {if (imagessrc = = NULL | | imagessrc.length <= 0) {return; } THIS.MIMAGESSRC = imagessrc; This.msize = imagessrc.length; This.mfakesize = msize * 10; Notifydatasetchanged ();} @Override public int GetCount () {return mfakesize; } public int GetSize () {return msize; } @Override public boolean isviewfromobject (view view, Object o) {return view = = o; } @Override public Object instantiateitem (viewgroup container, int Position) {position%= MS Ize ImageView ImageView = new ImageView (mcontext); Imageview.setlayoutparams (new Viewgroup.layoutparams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); Imageview.setscaletype (scaletype.center_crop); Imageview.setimageresource (mimagessrc[position]); Container.addview (imageView); Return imageView; } @Override public void Destroyitem (viewgroup container, int position, object Object) {container . Removeview ((View) object); } @Override public void finishupdate (viewgroup container) {int position = Mbannerviewpager.getcu Rrentitem (); LOG.D (TAG, "finish Update before, position=" + position); if (position = = 0) {position = msize; Mbannerviewpager.setcurrentitem (position, false); } else if (position = = GetCount ()-1) {position = mSize-1; Mbannerviewpager.setcurrentitem (position, false); } log.d (TAG, "finish update after, position=" + position); } @Override public void onpagescrolled (int position, float positionoffset, int Positionoffsetpixels) { } @Override public void onpageselected (int Position) {mbannerposition = position; Setindicator (position); } @Override public void onpagescrollstatechanged (int State) {}}public interface Onbannerclicklis tener{public void onbannerclicked (int pos);} public void Setonbannerclicklistener (onbannerclicklistener bannerclicklistener) {this.mbannerclicklistener = bannerclicklistener;} @Overridepublic void Ondetachedfromwindow () {mhandler.removecallbacksandmessages (null); this.removeallviews (); This.mbannerclicklistener = Null;super.ondetachedfromwindow ();}}</pre>Then look at the Dotview:<p><p></p></p><p><p></p></p><pre name="code" class="java">public class Dotview extends linearlayout {private int mlittledotwidth; private int mdotspan = 36; private float Mdotradius = 6f; private int mdotnumber = 5; private int mcurrent = 0; private int mselectedcolor = 0xff377bee; private int munselectedcolor = 0xffc5cedb; Public Dotview (context Context) {super (context); } public Dotview (context context, attributeset attrs) {super (context, attrs); Gets the custom properties of TypedArray arr = context.obtainstyledattributes (attrs, r.styleable.dotview, 0, 0); If (arr! = Null) {if (arr.hasvalue (r.styleable.dotview_dot_radius)) {mdotradius = arr.getdimen Sion (r.styleable.dotview_dot_radius, mdotradius); } if (arr.hasvalue (r.styleable.dotview_dot_span)) {mdotspan = (int) arr.getdimension (r.styleabl e.dotview_dot_span, mdotspan); } If (arr.hasvalue (r.styleable.dotview_dot_number)) {mdotnuMber = (int) Arr.getint (r.styleable.dotview_dot_number, mdotnumber); } Mselectedcolor = Arr.getcolor (r.styleable.dotview_dot_selected_color, mselectedcolor); Munselectedcolor = Arr.getcolor (r.styleable.dotview_dot_unselected_color, munselectedcolor); Arr.recycle (); } mlittledotwidth = (int) (MDOTSPAN/2 + Mdotradius * 2); Draw out the little dots adddotviews (); } public void Setdotnumber (int dotnumber) {this.mdotnumber = dotnumber; Adddotviews (); } private void Adddotviews () {setgravity (gravity.center_horizontal); SetOrientation (horizontal); Removeallviews (); for (int i = 0; i < mdotnumber; i++) {littledot dot = new Littledot (getcontext (), i); if (i = = 0) {dot.setcolor (mselectedcolor); } else {dot.setcolor (munselectedcolor); } dot.setlayoutparams (new Layoutparams (int) Mlittledotwidth, (int) Mdotradius * 2, 1)); AddView (dot); }} public final void setselected (int Index) {if (index >= getchildcount () | | | Index < 0 | | mcurren t = = Index) {return; } If (mcurrent < Getchildcount () && mcurrent >= 0) {((littledot) getchildat (mcurrent)). SE TColor (munselectedcolor); } ((littledot) getchildat (index)). setcolor (mselectedcolor); Mcurrent = index; } private class Littledot extends View {private int mcolor; Private Paint mpaint; Public Littledot (context context, int Index) {super (context); Mpaint = new Paint (); Mpaint.setantialias (true); public void SetColor (int color) {if (color = = Mcolor) {return; } Mcolor = color; Invalidate (); } @Override protected void OnDraw (canvas Canvas) {super.ondraw (canvas); Mpaint.setcolor (mcolor); Canvas.drawcircle (mlittledotwidth/2, mdotradius, mdotradius, mpaint); }} public void Setselectedcolor (int color) {if (mselectedcolor! = Color) {mselectedcolor = Colo R Invalidate (); }} public void Setunselectedcolor (int color) {if (munselectedcolor! = Color) {mselectedcolor = Color Invalidate (); }} public void SetColor (int selectedcolor, int unselectedcolor) {if (mselectedcolor! = Selectedcolor | | munselectedcolor! = Unselectedcolor) {mselectedcolor = selectedcolor; Munselectedcolor = unselectedcolor; Invalidate (); } }}</pre>finally, the Attrs_dotview.xml:<p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"?><resources> <declare-styleable name= "dotview" > <attr name= "dot_number" format= "integer"/> <attr name= "dot_radius" format= "dimension"/> < attr name= "dot_span" format= "dimension"/> <attr name= "dot_unselected_color" format= "integer"/> <attr name= "dot_selected_color" format= "integer"/> </declare-styleable></resources></pre></pre><p><p></p></p><p><p>The advantage of this control is: it is very simple to use, just to pass the picture to the Bannerview update method, and the source code is very simple to modify.</p></p><p><p>In addition, the implementation of Dotview is very ingenious, very worthy of reference!</p></p><p><p>We do not produce code, we are just code movers, thanks to these selfless dedication people:<br></p></p><p><p>Bannerview reference: http://blog.csdn.net/singwhatiwanna/article/details/46541225</p></p><p><p>Dotview reference: Https://github.com/etao-open-source/cube-sdk/tree/master/core/src/in/srain/cube/views/banner</p></p><p><p>Touch Blocker: http://blog.csdn.net/wuseyukui/article/details/46627961<br><br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android-loop AD Bit Component</p></p></span>

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.