java制作广告图片自动轮播控件
程序员文章站
2024-03-06 15:25:50
首页图片的轮播
/**
* 广告图片自动轮播控件
*
*/
public class imagecycleview exte...
首页图片的轮播
/** * 广告图片自动轮播控件</br> * */ public class imagecycleview extends linearlayout { /** * 上下文 */ private context mcontext; /** * 图片轮播视图 */ private viewpager madvpager = null; /** * 滚动图片视图适配 */ private imagecycleadapter madvadapter; /** * 图片轮播指示器控件 */ private viewgroup mgroup; /** * 图片轮播指示个图 */ private imageview mimageview = null; /** * 滚动图片指示视图列表 */ private imageview[] mimageviews = null; /** * 图片滚动当前图片下标 */ private boolean isstop; /** * 游标是圆形还是长条,要是设置为0是长条,要是1就是圆形 默认是圆形 */ public int stype=1; /** * @param context */ public imagecycleview(context context) { super(context); } /** * @param context * @param attrs */ @suppresslint("recycle") public imagecycleview(context context, attributeset attrs) { super(context, attrs); mcontext = context; layoutinflater.from(context).inflate(r.layout.ad_cycle_view, this); madvpager = (viewpager) findviewbyid(r.id.adv_pager); madvpager.setonpagechangelistener(new guidepagechangelistener()); // 滚动图片右下指示器视 mgroup = (viewgroup) findviewbyid(r.id.viewgroup); } /** * 触摸停止计时器,抬起启动计时器 */ @override public boolean dispatchtouchevent(motionevent event) { if(event.getaction()==motionevent.action_up){ // 开始图片滚动 startimagetimertask(); }else{ // 停止图片滚动 stopimagetimertask(); } return super.dispatchtouchevent(event); } /** * 装填图片数据 * * @param imageurllist * @param imagecycleviewlistener */ public void setimageresources(arraylist<integer> imageurllist ,imagecycleviewlistener imagecycleviewlistener,int stype){ this.stype=stype; // 清除 mgroup.removeallviews(); // 图片广告数量 final int imagecount = imageurllist.size(); mimageviews = new imageview[imagecount]; for (int i = 0; i < imagecount; i++) { mimageview = new imageview(mcontext); layoutparams params=new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); params.leftmargin=30; mimageview.setscaletype(scaletype.center_crop); mimageview.setlayoutparams(params); mimageviews[i] = mimageview; if (i == 0) { if(this.stype==1) // mimageviews[i].setbackgroundresource(r.drawable.banner_dian_focus); mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_focus);//换点 else mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_focus); } else { if(this.stype==1) // mimageviews[i].setbackgroundresource(r.drawable.banner_dian_blur); mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_blur); else mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_blur); } mgroup.addview(mimageviews[i]); } madvadapter = new imagecycleadapter(mcontext, imageurllist ,imagecycleviewlistener); madvpager.setadapter(madvadapter); madvpager.setcurrentitem(integer.max_value/2); startimagetimertask(); } public void setimageresources2(arraylist<integer> imageurllist ,imagecycleviewlistener imagecycleviewlistener,int stype){ this.stype=stype; // 清除 mgroup.removeallviews(); // 图片广告数量 final int imagecount = imageurllist.size(); mimageviews = new imageview[imagecount]; for (int i = 0; i < imagecount; i++) { mimageview = new imageview(mcontext); layoutparams params=new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); params.leftmargin=30; mimageview.setscaletype(scaletype.center_crop); mimageview.setlayoutparams(params); mimageviews[i] = mimageview; if (i == 0) { if(this.stype==1) // mimageviews[i].setbackgroundresource(r.drawable.banner_dian_focus); mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_focus);//换点 else mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_focus); } else { if(this.stype==1) // mimageviews[i].setbackgroundresource(r.drawable.banner_dian_blur); mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_blur); else mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_blur); } mgroup.addview(mimageviews[i]); } madvadapter = new imagecycleadapter(mcontext, imageurllist ,imagecycleviewlistener); madvpager.setadapter(madvadapter); madvpager.setcurrentitem(integer.max_value/2); startimagetimertask(); } /** * 图片轮播(手动控制自动轮播与否,便于资源控件) */ public void startimagecycle() { startimagetimertask(); } /** * 暂停轮播—用于节省资源 */ public void pushimagecycle() { stopimagetimertask(); } /** * 图片滚动任务 */ private void startimagetimertask() { stopimagetimertask(); // 图片滚动 mhandler.postdelayed(mimagetimertask, 5000); } /** * 停止图片滚动任务 */ private void stopimagetimertask() { isstop=true; mhandler.removecallbacks(mimagetimertask); } private handler mhandler = new handler(); /** * 图片自动轮播task */ private runnable mimagetimertask = new runnable() { @override public void run() { if (mimageviews != null) { madvpager.setcurrentitem(madvpager.getcurrentitem()+1); if(!isstop){ //if isstop=true //当你退出后 要把这个给停下来 不然 这个一直存在 就一直在后台循环 mhandler.postdelayed(mimagetimertask, 5000); } } } }; /** * 轮播图片监听 * * @author minking */ private final class guidepagechangelistener implements onpagechangelistener { @override public void onpagescrollstatechanged(int state) { if (state == viewpager.scroll_state_idle) startimagetimertask(); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpageselected(int index) { index=index%mimageviews.length; // 设置当前显示的图片 // 设置图片滚动指示器背 if(stype!=1) mimageviews[index].setbackgroundresource(r.drawable.cicle_banner_dian_focus); else mimageviews[index].setbackgroundresource(r.drawable.cicle_banner_dian_focus); for (int i = 0; i < mimageviews.length; i++) { if (index != i) { if(stype!=1) mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_blur); else mimageviews[i].setbackgroundresource(r.drawable.cicle_banner_dian_blur); } } } } private class imagecycleadapter extends pageradapter { /** * 图片视图缓存列表 */ private arraylist<smartimageview> mimageviewcachelist; /** * 图片资源列表 */ //private arraylist<string> madlist = new arraylist<string>(); private arraylist<integer> madlist = new arraylist<integer>(); /** * 广告图片点击监听 */ private imagecycleviewlistener mimagecycleviewlistener; private context mcontext; // public imagecycleadapter(context context, arraylist<string> adlist , imagecycleviewlistener imagecycleviewlistener) { // this.mcontext = context; // this.madlist = adlist; // mimagecycleviewlistener = imagecycleviewlistener; // mimageviewcachelist = new arraylist<smartimageview>(); // } public imagecycleadapter(context context, arraylist<integer> adlist , imagecycleviewlistener imagecycleviewlistener) { this.mcontext = context; this.madlist = adlist; mimagecycleviewlistener = imagecycleviewlistener; mimageviewcachelist = new arraylist<smartimageview>(); } @override public int getcount() { return integer.max_value; } @override public boolean isviewfromobject(view view, object obj) { return view == obj; } @override public object instantiateitem(viewgroup container, final int position) { int imageurl = madlist.get(position%madlist.size()); log.e("imageurl:",position%madlist.size()+"--"+imageurl+""); smartimageview imageview = null; if (mimageviewcachelist.isempty()) { imageview = new smartimageview(mcontext); imageview.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content)); //test imageview.setscaletype(imageview.scaletype.center_crop); } else { imageview = mimageviewcachelist.remove(0); } imageview.settag(imageurl); container.addview(imageview); //imageview.setimageurl(imageurl); imageview.setbackgroundresource(imageurl); // 设置图片点击监听 imageview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { mimagecycleviewlistener.onimageclick(position%madlist.size(), v); } }); return imageview; } @override public void destroyitem(viewgroup container, int position, object object) { smartimageview view = (smartimageview) object; madvpager.removeview(view); mimageviewcachelist.add(view); } } /** * 轮播控件的监听事件 * * @author minking */ public static interface imagecycleviewlistener { /** * 单击图片事件 * * @param position * @param imageview */ public void onimageclick(int position, view imageview); } }
我们再来看个基于安卓viewpager的图片轮播控件
package me.lanfog.myandroid.widget; import java.util.arraylist; import java.util.list; import android.content.context; import android.os.handler; import android.os.message; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.util.attributeset; import android.util.log; import android.view.view; import android.view.viewgroup; import android.widget.imageview; public class pageflipper extends viewpager { private string tag = pageflipper.class.getsimplename(); private list<view> views; private pageradapter adapter = new pageradapter() { @override public object instantiateitem(viewgroup container, int position) { view v = views.get(position); container.addview(v); return v; } @override public boolean isviewfromobject(view arg0, object arg1) { return arg0 == arg1; } @override public int getitemposition(object object) { return views.indexof(object); } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((view)object); } @override public int getcount() { return views == null ? 0 : views.size(); } }; private onpagechangelistener listener = new onpagechangelistener() { /** * 将控件位置转化为数据集中的位置 */ public int convert(int position){ return position == 0 ? views.size()-1 : ( position > views.size() ? 0 : position-1 ); } @override public void onpageselected(int position) { if(listener2 != null){ listener2.onpageselected(convert(position)); } } @override public void onpagescrolled(int position, float percent, int offset) { if(listener2 != null){ listener2.onpagescrolled(convert(position), percent, offset); } if(percent == 0){ if(position == 0) // 切换到倒数第二页 setcurrentitem(( views.size() - 2 ) % views.size(), false); else if(position == views.size() - 1) // 切换到正数第二页 setcurrentitem(1, false); } } @override public void onpagescrollstatechanged(int state) { if(listener2 != null){ listener2.onpagescrollstatechanged(state); } switch (state) { case scroll_state_idle: // 闲置 if(!handler.hasmessages(start_flipping)) handler.sendemptymessagedelayed(start_flipping, 3000); // 延时滚动 break; case scroll_state_dragging: // 拖动中 handler.sendemptymessage(stop_flipping); // 取消滚动 break; case scroll_state_settling: // 拖动结束 break; } } }, listener2; private final int start_flipping = 0; private final int stop_flipping = 1; private handler handler = new handler(){ public void handlemessage(message msg) { switch (msg.what) { case start_flipping: if(views.size() > 3) // 因为前后页是辅助页,所以此处3也就是只有1页 setcurrentitem((getcurrentitem() + 1) % views.size()); handler.sendemptymessagedelayed(start_flipping, 3000); // 延时滚动 break; case stop_flipping: handler.removemessages(start_flipping); break; } } }; public pageflipper(context context, attributeset attrs) { super(context, attrs); init(); } public pageflipper(context context) { super(context); init(); } private void init(){ setoffscreenpagelimit(1); // 最大页面缓存数量 setadapter(adapter); // 适配器 super.setonpagechangelistener(listener); // 监听器 handler.sendemptymessagedelayed(start_flipping, 3000); // 延时滚动 } public void setviews(int[] ids){ this.views = new arraylist<view>(); for(int i=0;i<ids.length+2;i++){ // 头部新增一个尾页,尾部新增一个首页 imageview iv = new imageview(getcontext()); iv.setimageresource(ids[i == 0 ? ids.length-1 : ( i > ids.length ? 0 : i-1 )]); iv.setscaletype(imageview.scaletype.fit_xy); this.views.add(iv); } setcurrentitem(1); // 首页 this.adapter.notifydatasetchanged(); } @override public void setonpagechangelistener(onpagechangelistener listener) { this.listener2 = listener; } }