Android自動播放Banner圖片輪播效果
本文實例為大家分享了Android自動播放Banner圖片輪播的具體代碼,供大家參考,具體內(nèi)容如下
先看一下效果圖
支持本地圖片以及網(wǎng)絡(luò)圖片or本地網(wǎng)絡(luò)混合。
使用方式:
<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{//網(wǎng)絡(luò) 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://滑動結(jié)束 isAuto=true; break; } }
當(dāng)狀態(tài)為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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter?Ping檢查服務(wù)器通訊信號強度實現(xiàn)步驟
這篇文章主要為大家介紹了Flutter?Ping檢查服務(wù)器通訊信號強度實現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android使用TextView實現(xiàn)無下劃線超鏈接的方法
這篇文章主要介紹了Android使用TextView實現(xiàn)無下劃線超鏈接的方法,結(jié)合實例形式分析了Android中TextView超鏈接去除下劃線的相關(guān)實現(xiàn)技巧與注意事項,需要的朋友可以參考下2016-08-08Android Studio出現(xiàn)Failed to pull selection: open failed: Permi
本篇文章給大家分享了Android Studio中導(dǎo)出數(shù)據(jù)庫文件的方法以及出現(xiàn)Failed to pull selection: open failed: Permission denied的解決思路,有興趣的學(xué)習(xí)下。2018-05-05Android 個人理財工具一:項目概述與啟動界面的實現(xiàn)
本文主要介紹Android 開發(fā)個人理財工具項目概述與啟動界面的實現(xiàn),這里主要對實現(xiàn)項目的流程做了詳細(xì)概述,并對啟動界面簡單實現(xiàn),有需要的小伙伴可以參考下2016-08-08