Android自動播放Banner圖片輪播效果_Android

來源:互聯網
上載者:User

先看一下效果圖

支援本地圖片以及網狀圖片or本網混合。

使用方式:

<com.jalen.autobanner.BannerView android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="230dip"></com.jalen.autobanner.BannerView>

核心代碼:

 int length = mList.size(); View view = LayoutInflater.from(mContext).inflate(R.layout.banner_view,this,true); LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_points); vp= (ViewPager) view.findViewById(R.id.vp); ll.removeAllViews(); LinearLayout.LayoutParams ll_parmas = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); ll_parmas.leftMargin=5; ll_parmas.rightMargin=5; for(int i=0;i<length;i++){  ImageView img = new ImageView(mContext);  img.setLayoutParams(ll_parmas);  if(i==0){  img.setImageResource(R.mipmap.dot_focus);  }else{  img.setImageResource(R.mipmap.dot_blur);  }  ll.addView(img);  mImgs.add(img);  final ImageView imgforview = new ImageView(mContext);  imgforview.setOnClickListener(this);  imgforview.setScaleType(ImageView.ScaleType.FIT_XY);  if(mList.get(i).getType()==0){//本地圖片  imgforview.setImageResource(mList.get(i).getDrawableforint());  }else{//網路  Glide.with(mContext).load(mList.get(i).getDrawableforurl()).diskCacheStrategy(DiskCacheStrategy.ALL).into(imgforview);//  Glide.with(mContext).load(mList.get(i).getDrawableforurl()).listener(new RequestListener<String, GlideDrawable>() {//   @Override//   public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {//   Log.d("yu","Faile:"+e.toString());//   return false;//   }////   @Override//   public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {//    imgforview.setImageDrawable(resource);//   return false;//   }//  }).into(imgforview);//  Log.d("yu","url: "+mList.get(i).getDrawableforurl());  }  mViews.add(imgforview); } vp.setAdapter(new MyAdapter()); vp.addOnPageChangeListener(onPageChange);

自動輪播利用的是handler的postdelay方法。

private Runnable task = new Runnable() { @Override public void run() {  if(isAuto){  currentItem = currentItem%(mViews.size());//  Log.d("yu","runalbe "+currentItem);  if(currentItem==0){   vp.setCurrentItem(currentItem,false);  }else{   vp.setCurrentItem(currentItem);  }  currentItem++;  mHandle.postDelayed(task,delaytime);  }else{  mHandle.postDelayed(task,delaytime);  } } };

利用isAuto判斷是否正在自動輪播 如果為false 不自動切換item。isAuto賦值操作位於OnPageChangeListener的onPageScrollStateChanged方法中:

 public void onPageScrollStateChanged(int state) {  switch (state){  case ViewPager.SCROLL_STATE_IDLE://使用者什麼都沒有操作   isAuto=true;   currentItem = vp.getCurrentItem();//   Log.d("yu","IDLE"+currentItem);//   if(vp.getCurrentItem()==mViews.size()){//   vp.setCurrentItem(0,false);//   }   break;  case ViewPager.SCROLL_STATE_DRAGGING://正在滑動   isAuto =false;   break;  case ViewPager.SCROLL_STATE_SETTLING://滑動結束   isAuto=true;   break;  } }

當狀態為SCROLL_STATE_DRAGGING時 說明使用者正在操作 ,看下源碼中的解釋:

 /** * Indicates that the pager is in an idle, settled state. The current page * is fully in view and no animation is in progress. */ public static final int SCROLL_STATE_IDLE = 0; /** * Indicates that the pager is currently being dragged by the user. */ public static final int SCROLL_STATE_DRAGGING = 1; /** * Indicates that the pager is in the process of settling to a final position. */ public static final int SCROLL_STATE_SETTLING = 2;

大致意思呢就是 0代碼沒有任何操作。1頁面正在被使用者拖動。2代表成功切換至下一頁面。

源碼地址:https://github.com/yudehai0204/autoBanner

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.