<span id="Label3"></p><p><p>About the advertisement carousel, everybody certainly will not be unfamiliar, it is in the mobile phone market each big app appears the frequency extremely high, its advantage lies in "does not occupy the screen", may use only the small fixed vacancy to display several even dozens of advertising strips, Moreover the dynamic effect is very good, has the very good user "the friendliness", below looks several example diagram:</p></p><p><p></p></p><p><p>And then look at the effect of my copy writing:</p></p><p><p></p></p><p><p></p></p><p><p>About the Advertising Carousel banner this thing, GitHub above should have ready-made open source components, but I did not look, feel that the implementation will not be too difficult, I go to imitate write, below I say the realization of the Idea:</p></p><p><p>1, first see this can slide to switch the image of the interface, we will naturally think of Viewpager Control.</p></p><p><p>2, need to consider its pseudo-loop (actually just slide to the end of the picture and then switch to the beginning of the picture, give a "infinite cycle" illusion), did Galleyview gallery effect of friends should be very familiar with, when we slide to the end of the gallery, if you want to see the first picture need to Re-slide back, So the user experience is not good, so we will be in the adapter adapter GetCount () method, return a very large value, so that it can "infinite loop." Unclear friends also matter, the following code will be mentioned in Detail.</p></p><p><p>3, is to consider its automatic sliding effect, then very simple will think of the timer, every few seconds to let it automatically slide once, and then by matching the Viewpager settings of the current page Setcurrentitem can achieve the effect we want.</p></p><p><p>4, the last is to take into account the details of things, how to make the screen slide with the bottom of the small round punctuate, we do the timer operation, the infinite loop is definitely a while forever true state, when we switch out of the current interface, this timer loop how to deal with.</p></p><p><p></p></p><p><p>well, consider the implementation principles and processes, and we'll be able to write the handwritten code.</p></p><p><p></p></p><p><p><strong>1, first to analyze the Layout:</strong></p></p><p><p></p></p><p><p>The above is very detailed, here directly on the code:</p></p><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">Layout File</span></span><p><p>Then there is the style of the small circle, there are 2 xml, one is the selection state, and the other is the blank state</p></p><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">Small Circle Normal state</span></span><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">Small Circle selected state</span></span><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">Small Circle Background effect</span></span><p><p></p></p><p><p><strong>2, Viewpager Adapter</strong></p></p><p><p>Now that we're using vierpager, we have to set up an adapter to load the ad map we want to show, and Here's the GetCount () method, which normally lets us return the length of the data source, but here we need to achieve an "infinite loop" Effect. , so we can return a relatively large is such as: integer.max_value, this value is 2 billion, the user how to slide to not slip to the next hundred billion sub-level. Then avoid the null pointer exception, we can no longer directly use the position to index resources in the following AddView and removeview, we should take the total number of the remaining item, so that the index location will not exceed the amount of resource data, such as 1%777=1,1%999 = 1.</p></p><p><p>For Viewpager unfamiliar friends can look at my previous written article "android development Note--viewpager components (imitation boot interface)"</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"> </span> </span><pre> 1 package com.lcw.rabbit.banner; 2 3 Import java.util.List; 4 5 Import android.support.v4.view.PagerAdapter; 6 Import android.view.View; 7 Import android.view.ViewGroup; 8 Import android.widget.ImageView; 9/**10 * Viewpager Adapter One * @author rabbit_lee12 *13 */14 public class Banneradapter extends Pageradapter {15 16// Data source list<imageview> mlist;18 public banneradapter (list<imageview> List) {. Mlist = list;21}22 @Override24 public int getcount () {25//take Extra Large number to achieve the wireless loop effect, return Integer . Max_value;27}28 @Override30 Public boolean isviewfromobject (View arg0, Object arg1) {return arg 0 = = arg1;32}33 @Override35 public Object instantiateitem (viewgroup container, int Position) {c Ontainer.addview (mlist.get (position%mlist.size ())); notoginseng return mlist.get (position%mlist.size ());}39 verride41 public void Destroyitem (viewgroup container,int position, Object Object) {container.removeview (mlist.get (position%mlist.size ())); 43}44 45} </pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p><strong>3. Main code</strong></p></p><p><p>First of all, the realization of small circle point, there are 2 ways, one is directly in the advertising map "painting dead", This method is time-consuming and maintenance is not flexible, so we use the second method of dynamic generation, according to the length of the ad map resources in a given linearlayout to add a circle point view. Then we need to give it a PointIndex flag bit, use it to record the current page position, the initial 0, and then each time the slide is based on this flag bit to switch the state of the small Circle.</p></p><p><p></p></p><p><p>In the beginning, we can not set the page position to 0 (first page), if we set to 0, then we can not swipe to the left. Since we returned the Integer.max_value in the Adapter's getcount, we can take its midpoint as the starting point, and use the Setcurrentitem to start the Onpageselected method in the Vierpager listener, Then the left and right can all Slide.</p></p><p><p></p></p><p><p>Finally is the timer systemclock, we here opened a new thread through while forever true to achieve this effect, so that it executes every 2 seconds to set the current page action, each time only need to set a simple page +1, The last note is that we need to set a switch for this timer, when we switch out of the page to stop the operation of the timer, that is, to rewrite our OnDestroy method, here to switch off.</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1 package com.lcw.rabbit.banner; 2 3 Import java.util.ArrayList; 4 Import java.util.List; 5 6 Import android.app.Activity; 7 Import android.os.Bundle; 8 Import android.os.SystemClock; 9 Import android.support.v4.view.ViewPager; Ten Import android.support.v4.view.ViewPager.OnPageChangeListener; Import android.view.View; Import android.widget.ImageView; Import android.widget.LinearLayout; Import android.widget.LinearLayout.LayoutParams; Android.widget.TextView import; public class Mainactivity extends Activity {18 19//declaration control-private viewpager mviewpager; List<imageview> mlist; The private TextView mtextview; LinearLayout mlinearlayout; 24 25//ad Map Footage of the private int[] bannerimages = {r.drawable.image1, r.drawable.image2, r.drawable.image3, R.drawa ble.image4}; 27//advertising language string[] bannertexts = {"because of professional so excellent", "adhere to the innovation industry leader", "integrity and professional win", "fine and harmonious atmosphere open"}; //viewpager Adapter and SupervisorThe listener to the private banneradapter madapter; + Private Bannerlistener bannerlistener; 33 34//circle Flag bit + private int pointIndex = 0; 36//thread Flag PNs Private Boolean isstop = false; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancest ate); Setcontentview (r.layout.activity_main); Initview (); InitData (); Initaction (); 46 47//open New thread, 2 SEC update banner new thread (new Runnable () {@Override PU Blic void Run () {!isstop) {systemclock.sleep (2000); 54 Runonuithread (new Runnable () {@Override-in-the-public void run () { Mviewpager.setcurrentitem (mviewpager.getcurrentitem () + 1); 59} 60}); 61} 62} 63}). Start (); 64} 65 66/** 67 * Initialization Event * * * initaction private void () {bannerlistener = new Bann Erlistener (); Mviewpager.setonpagechangelistener (bannerlistener); 72//take the middle number as the starting position of the INTEGER.MAX_VALUE/2 int index = (integer.max_value/2% mlist.size ()); 74//for Departure Listener Mviewpager.setcurrentitem (index); Mlinearlayout.getchildat (pointIndex). setenabled (true); 77} 78 79/** 80 * Initialize data bayi */initdata private void () {mlist = new Arraylist<im Ageview> (); View view; Layoutparams params; (int i = 0; i < bannerimages.length; i++) {87//set ad map ImageView ImageView = New ImageView (mainactivity.this); Imageview.setlayoutparams (new layoutparams (layoutparams.match_parent, layoutparams.match_parent)); Imageview.setbackgroundresource (bannerimages[i]); 91 Mlist.add (imageView); 92//set the circle point to view = new View (mainactivity.this); 94 params = new Layoutparams (5, 5); Params.leftmargin = 10; View.setbackgroundresource (r.drawable.point_background); View.setlayoutparams (params); 98 view.setenabled (false); Mlinearlayout.addview (view), 101}102 madapter = new Banneradapter (mlist); 103 MV Iewpager.setadapter (madapter); 104}105 106/**107 * Initialize view operation 108 */109 private void Initview () {110 Mviewpager = (viewpager) Findviewbyid (r.id.viewpager); 111 Mtextview = (TextView) Findviewbyid (r.id.tv_bann ertext); mlinearlayout = (linearlayout) Findviewbyid (r.id.points); 113}114 115//implement Vierpager Listener Interface 116 Class Bannerlistener implements Onpagechangelistener {117 118 @Override119 public void Onpagescrollstate Changed (int arg0) {120}121 122 @Override123 public void onpagescrolled (int arg0, float arg1, int arg2) {124}125 126 @Over ride127 public void onpageselected (int position) {$ int newposition = position% bannerimages.length ; 129 Mtextview.settext (bannertexts[newposition]); Mlinearlayout.getchildat (newposition). SetEnab LED (true); 131 Mlinearlayout.getchildat (pointIndex). setenabled (false); 132//update flag bit 133 p Ointindex = newposition;134 135}136 137}138 139 @Override140 protected void OnDestroy () {141 Off timer 142 isstop = true;143 Super.ondestroy (); 144}145 146}</pre><p><p> Android scrolling Pictures </p> </p></span>
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