安卓项目实战之仿魅族的酷炫Banner轮播效果

效果图

在这里插入图片描述
GitHub地址:https://github.com/pinguo-zhouwei/MZBannerView

添加依赖

1,project的build.gradle添加:

allprojects {
     repositories {
          ...
          maven { url 'https://jitpack.io' }
     }
}

2,app的build.gradle添加:

dependencies {
         compile 'com.github.pinguo-zhouwei:MZBannerView:v2.0.2' // banner
         compile 'com.makeramen:roundedimageview:2.3.0' // 圆角图片
}

使用方式

1,xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="txkj.xian.com.qiubantiyu.activity.TestViewPagerActivity">

    <com.zhouwei.mzbanner.MZBannerView
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="10dp"
        app:open_mz_mode="true"
        app:middle_page_cover="false"
        app:canLoop="true"
        app:indicatorAlign="center"
        />

</LinearLayout>

1,既可作为banner使用,也可作为普通的viewpager使用,canLoop为 true 表示轮播,false 则为普通ViewPager
2,MZBannerView的高度必须指定为具体值,否则为填充整个高度
3,上面的middle_page_cover设置中间的pager是否覆盖,建议设置为false,设置为false有动画效果,true没有动画效果。

Activity中:

public class TestViewPagerActivity extends BaseActivity{

    @BindView(R.id.banner)
    MZBannerView mMZBanner;

    @Override
    public int setLayout() {
        return R.layout.activity_test_view_pager;
    }

    @Override
    public void initView() {
        List<String> list = new ArrayList<>();
        list.add("http://statics.qiuban.vip/carousel/banner1.jpg");
        list.add("http://statics.qiuban.vip/carousel/banner2.jpg");
        list.add("http://statics.qiuban.vip/carousel/banner3.jpg");
        list.add("http://statics.qiuban.vip/carousel/banner4.jpg");

        mMZBanner.setIndicatorVisible(true);  // 设置是否显示指示器
//        mMZBanner.setDelayedTime(3);  // 设置切换时间间隔
//        mMZBanner.setIndicatorRes(R.drawable.unselect,R.drawable.selected); // 设置指示器样式未选中-选中
        // 点击监听,必须写在setPages前面调用否则没效果
        mMZBanner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int i) {
                Toast.makeText(TestViewPagerActivity.this, "点击了"+i, Toast.LENGTH_SHORT).show();
            }
        });

        // 设置数据
        mMZBanner.setPages(list, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });

    }

    public static class BannerViewHolder implements MZViewHolder<String> {
        private RoundedImageView mImageView;
        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item,null);
            mImageView = view.findViewById(R.id.imageView);
            return view;
        }

        @Override
        public void onBind(Context context, int position, String data) {
            // 数据绑定
            Glide.with(context).load(data).into(mImageView);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mMZBanner.pause();//暂停轮播
    }

    @Override
    public void onResume() {
        super.onResume();
        mMZBanner.start();//开始轮播
    }
}

如果是当Banner使用,注意在onResume 中调用start()方法,在onPause中调用 pause() 方法。如果当普通ViewPager使用,则不需要。

Banner的item布局:使用ConstraintLayout对图片进行了适配,此处原图尺寸为:672*234,修改为自己所用素材的宽高比即可

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <com.makeramen.roundedimageview.RoundedImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/imageView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="centerCrop"
            app:layout_constraintDimensionRatio="h,672:234"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:riv_corner_radius="10dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            />

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

关于设置指示器颜色用到的两个xml文件:
unselect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <solid android:color="#eeeeee" />
    <size android:height="5dp"
        android:width="5dp"
        />

</shape>

selected.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <solid android:color="#ff0000" />
    <size android:height="5dp"
        android:width="5dp"
        />

</shape>

实际项目中使用

在实际项目中,一般获得的都是一个对象ImageBean,里面包含了图片路径,标题等信息,而不像上面的例子直接加载一个本地图片,只需要将泛型改为ImageBean即可,相应的onBind方法中的参数2也需要改为ImageBean类型。
图片素材:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
现在的APP Banner大多数千篇一律,前几天看到魅族手机上所有魅族自家APP上的Banner效果不错,于是就想着来仿着做一个类似的效果。因此就有了这个库。但是为了使用方便,这个库不仅仅只有仿魅族效果BannerView 来使用,还可以当作普通的BannerView 来使用,还可以当作一个ViewPager 来使用。使用很方便,具体使用方法和API 请看后面的示例。 ---  左图为魅族APP上的Banner效果,右图是高仿效果。MZBannerView 有以下功能:1 . 仿魅族BannerView 效果。2 . 当普通Banner 使用3 . 当普通ViewPager 使用。4 . 当普通ViewPager使用(有魅族Banner效果)5 . 仿某视频网站Banner效果。Demo APKgif图片有点模糊,可以扫描下方二维码下载APK体验相关博客ViewPager系列之 仿魅族应用的广告BannerView更新日志v1.1.1 : 增加按住Banner 停止轮播,松开开始自动轮播的功能v1.1.0 : fix 在从网上获取数据后,banner 显示 造成 ANR 的bug(如果在onCreate()中设置资源显示则没问题)v1.1.2 : fix 更改数据之后,调用setPages重新刷新数据会crush的bugv2.0.0 :1,add: 添加仿魅族Banner效果,中间Page覆盖两边。 -- 2,fix 部分bug: 添加OnPageChangeListener 回调 pisition 不对的bug.DependencyAdd it in your root build.gradle at the end of repositories:allprojects {      repositories {           ...           maven { url 'https://jitpack.io' }      } }Step 2. Add the dependencydependencies {          compile 'com.github.pinguo-zhouwei:MZBannerView:v2.0.0' }自定义属性属性名属性意义取值open_mz_mode是否开启魅族模式true 为魅族Banner效果,false 则普通Banner效果canLoop是否轮播true 轮播,false 则为普通ViewPagerindicatorPaddingLeft设置指示器距离左侧的距离单位为 dp 的值indicatorPaddingRight设置指示器距离右侧的距离单位为 dp 的值indicatorAlign设置指示器的位置有三个取值:left 左边,center 剧中显示,right 右侧显示middle_page_cover设置中间Page是否覆盖(真正的魅族Banner效果)true 覆盖,false 无覆盖效果使用方法1 . xml 布局文件2 . activity中代码:mMZBanner = (MZBannerView) view.findViewById(R.id.banner);              // 设置数据         mMZBanner.setPages(list, new MZHolderCreator() {             @Override             public BannerViewHolder createViewHolder() {                 return new BannerViewHolder();             }         });  public static class BannerViewHolder implements MZViewHolder {         private ImageView mImageView;         @Override         public View createView(Context cont
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

智玲君

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

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

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

打赏作者

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

抵扣说明:

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

余额充值