android 自动播放Banner

先看一下效果图

这里写图片描述

支持本地图片以及网络图片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代表成功切换至下一页面。

源码地址

点击跳转

有问题随时欢迎Issues;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫的于

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值