Android自动播放Banner图片轮播效果
程序员文章站
2024-03-03 21:14:04
先看一下效果图
支持本地图片以及网络图片or本地网络混合。
使用方式:
先看一下效果图
支持本地图片以及网络图片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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。