Android自動(dòng)播放Banner圖片輪播效果
本文實(shí)例為大家分享了Android自動(dòng)播放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);
自動(dòng)輪播利用的是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判斷是否正在自動(dòng)輪播 如果為false 不自動(dòng)切換item。isAuto賦值操作位于OnPageChangeListener的onPageScrollStateChanged方法中:
public void onPageScrollStateChanged(int state) { switch (state){ case ViewPager.SCROLL_STATE_IDLE://用戶什么都沒(méi)有操作 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://正在滑動(dòng) isAuto =false; break; case ViewPager.SCROLL_STATE_SETTLING://滑動(dòng)結(jié)束 isAuto=true; break; } }
當(dāng)狀態(tài)為SCROLL_STATE_DRAGGING時(shí) 說(shuō)明用戶正在操作 ,看下源碼中的解釋?zhuān)?/p>
/** * 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代碼沒(méi)有任何操作。1頁(yè)面正在被用戶拖動(dòng)。2代表成功切換至下一頁(yè)面。
源碼地址:https://github.com/yudehai0204/autoBanner
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)圖片輪播效果的兩種方法
- Android實(shí)現(xiàn)圖片輪播效果
- Android實(shí)現(xiàn)Banner界面廣告圖片循環(huán)輪播(包括實(shí)現(xiàn)手動(dòng)滑動(dòng)循環(huán))
- Android使用ViewPager加載圖片和輪播視頻
- 詳解android 視頻圖片混合輪播實(shí)現(xiàn)
- Android實(shí)現(xiàn)炫酷輪播圖效果
- Android實(shí)現(xiàn)圖片自動(dòng)輪播并且支持手勢(shì)左右無(wú)限滑動(dòng)
- Android實(shí)現(xiàn)輪播圖片展示效果
- Android實(shí)現(xiàn)圖片文字輪播特效
- Android實(shí)現(xiàn)視圖輪播效果
相關(guān)文章
Android實(shí)現(xiàn)簡(jiǎn)單的banner輪播圖
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的banner輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Flutter?Ping檢查服務(wù)器通訊信號(hào)強(qiáng)度實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了Flutter?Ping檢查服務(wù)器通訊信號(hào)強(qiáng)度實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android使用TextView實(shí)現(xiàn)無(wú)下劃線超鏈接的方法
這篇文章主要介紹了Android使用TextView實(shí)現(xiàn)無(wú)下劃線超鏈接的方法,結(jié)合實(shí)例形式分析了Android中TextView超鏈接去除下劃線的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2016-08-08Android Studio出現(xiàn)Failed to pull selection: open failed: Permi
本篇文章給大家分享了Android Studio中導(dǎo)出數(shù)據(jù)庫(kù)文件的方法以及出現(xiàn)Failed to pull selection: open failed: Permission denied的解決思路,有興趣的學(xué)習(xí)下。2018-05-05基于Android的英文詞典的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了基于Android的英文詞典的實(shí)現(xiàn)方法2016-05-05Android列表控件Spinner簡(jiǎn)單用法示例
這篇文章主要介紹了Android列表控件Spinner簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了Android列表控件Spinner的布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12Android 個(gè)人理財(cái)工具一:項(xiàng)目概述與啟動(dòng)界面的實(shí)現(xiàn)
本文主要介紹Android 開(kāi)發(fā)個(gè)人理財(cái)工具項(xiàng)目概述與啟動(dòng)界面的實(shí)現(xiàn),這里主要對(duì)實(shí)現(xiàn)項(xiàng)目的流程做了詳細(xì)概述,并對(duì)啟動(dòng)界面簡(jiǎn)單實(shí)現(xiàn),有需要的小伙伴可以參考下2016-08-08