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://用戶什么都沒有操作
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í) 說明用戶正在操作 ,看下源碼中的解釋:
/** * 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頁面正在被用戶拖動(dòng)。2代表成功切換至下一頁面。
源碼地址:https://github.com/yudehai0204/autoBanner
以上就是本文的全部內(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)輪播并且支持手勢左右無限滑動(dòng)
- Android實(shí)現(xiàn)輪播圖片展示效果
- Android實(shí)現(xiàn)圖片文字輪播特效
- Android實(shí)現(xiàn)視圖輪播效果
相關(guān)文章
Android Gradle Plug 4.1.0 升級(jí)后gradle獲取manifest位置失敗問題解決
這篇文章主要介紹了Android Gradle Plug 4.1.0 升級(jí)后gradle獲取manifest位置失敗問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
android RecyclerView添加footerview詳解
大家好,本篇文章主要講的是android RecyclerView添加footerview詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Android中butterknife的使用與自動(dòng)化查找組件插件詳解
這篇文章主要給大家介紹了關(guān)于Android中butterknife的使用與自動(dòng)化查找組件插件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Android開發(fā)之關(guān)于項(xiàng)目
本文是此系列文章的第二篇,給大家介紹的是項(xiàng)目相關(guān)的內(nèi)容,非常的細(xì)致全面,有需要的小伙伴可以參考下2016-02-02
利用Jetpack Compose實(shí)現(xiàn)主題切換功能
這篇文章主要介紹了如何利用Android中的Jetpack Compose實(shí)現(xiàn)主題切換功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-01-01

