效果图


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' // bannercompile '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.MZBannerViewandroid: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;@Overridepublic int setLayout() {return R.layout.activity_test_view_pager;}@Overridepublic 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() {@Overridepublic void onPageClick(View view, int i) {Toast.makeText(TestViewPagerActivity.this, "点击了"+i, Toast.LENGTH_SHORT).show();}});// 设置数据mMZBanner.setPages(list, new MZHolderCreator<BannerViewHolder>() {@Overridepublic BannerViewHolder createViewHolder() {return new BannerViewHolder();}});}public static class BannerViewHolder implements MZViewHolder<String> {private RoundedImageView mImageView;@Overridepublic View createView(Context context) {// 返回页面布局View view = LayoutInflater.from(context).inflate(R.layout.banner_item,null);mImageView = view.findViewById(R.id.imageView);return view;}@Overridepublic void onBind(Context context, int position, String data) {// 数据绑定Glide.with(context).load(data).into(mImageView);}}@Overridepublic void onPause() {super.onPause();mMZBanner.pause();//暂停轮播}@Overridepublic 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.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><com.makeramen.roundedimageview.RoundedImageViewxmlns: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类型。
图片素材:




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

  1. 仿优酷视频焦点轮播图布局html页面前端源码

    大家好,今天给大家介绍一款,仿优酷视频焦点轮播图布局html页面前端源码(图1).送给大家哦,获取方式在本文末尾. 图1 可以用菜单控制切换(图2) 图2 整体布局简洁明了,干净简单(图3) 图3 代 ...

  2. 纯CSS3写的10个不同的酷炫图片遮罩层效果

    这个是纯CSS3实现的的10个不同的酷炫图片遮罩层效果,可以欣赏一下  在线预览 下载地址 实例代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  3. 纯CSS3写的10个不同的酷炫图片遮罩层效果【转】

    这个是纯CSS3实现的的10个不同的酷炫图片遮罩层效果,可以欣赏一下 在线预览 下载地址 实例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

  4. 24小时轮播怎么实现的_优酷24小时轮播台如何使用

    优酷24小时轮播台如何使用相信有很多小伙伴对此存在疑惑,接下来就跟着IEfans小编一起了解一下使用优酷24小时轮播台方法分享! 工具/原料 手机(设备名称:华为荣耀V8.系统版本:EMUI 8.0. ...

  5. web前端入门到实战:以轮播效果为案例谈如何写优质代码

    作为程序员大家在写代码时谈的最多的就是代码的拓展性.复用性.本文就以大家熟悉的轮播效果为案例,讲一讲写优质代码的思路和实践. 文章分三个步骤.第一步,实现基本功能:第二步,考虑到代码的封装性和复用性: ...

  6. android魅族轮播图,用angularjs模仿魅族官网的图片轮播功能

    使用指令模仿魅族官网的图片轮播功能(angularjs中DOM操作都在指令中完成) html css .slider{ position: relative; width:900px; height: ...

  7. Swift项目,超美的动画和tableView,collectionView,轮播图的使用,网络请求的封装等

    <一>项目介绍: 这是一个Swift语言的项目,但是其中也有使用一些OC的三方库,比SDWebImage.WebViewJavascriptBridge等,同时实现了基本App的框架功能, ...

  8. Win11新特性:在平板锁屏界面还有酷炫的3D视差效果

    在深入挖掘 Windows 11 系统之后,用户再次发现了隐藏的 Windows 10 Mobile 保留功能.知名破解达人 ADeltaX 表示,如果你的设备有合适的硬件,Windows 11 将在 ...

  9. AxureRP实战(三)Banner轮播图交互(进阶篇)

    前一篇<AxureRP实战(二)Banner轮播图交互(基础篇)>用淘宝的首页banner案例讲解了如何运用动态面板实现banner轮播图的切换,有了基本交互功能.但是,交互效果还有些欠缺 ...

最新文章

  1. numpy 按照指定字段排序
  2. resin服务器配置错误
  3. 查看内核中每个函数花费的时间 initcall_debug
  4. method-dispatch/
  5. 1359C. Mixing Water
  6. c语言简单选择对字符串数组排序,简单了解C语言中直接插入排序与直接选择排序实现...
  7. 安装fusionPBX
  8. 【Centos7】GCC版本编译升级
  9. 软件测试用例设计(三)——场景法
  10. 【翻译】Javac骇客指南
  11. android qq语音按钮,qq语音设置怎么操作?手机qq语音设置在哪里
  12. 【读书笔记】《洛克菲勒写给儿子的38封信》
  13. BP神经网络(Back Propagation Neural Network)Matlab简单实现
  14. [Python爬虫] 三、数据抓取之Requests HTTP 库
  15. 不仅可以邮件撤回还能误删恢复?!
  16. 【CV】SwinIR:使用 Swin Transformer 进行图像恢复
  17. 基于jsp的酒店管理系统
  18. 网络拓扑结构可视化呈现方案
  19. vmware中windows XP启动报ntldr is missing错误
  20. 全球首个5G全覆盖国家诞生,华为成幕后最大功臣!

热门文章

  1. U盘文件或目录损坏且无法读取的修复/解决方法
  2. 职业选择测试发型软件,发型在线测试 挑选适合的修颜造型
  3. 循环语句的要素C语言,C语言中循环语句(while)
  4. 虚拟现实VR遇上云计算 将是怎样的情缘
  5. 无聊又看了_好想好想谈恋爱
  6. 基于stm32的FIR滤波
  7. CPU性能天梯图 V3.90 build 20190101
  8. 暴雪下决心反作弊 18000魔兽帐号被禁
  9. 软件体系结构1~5章知识点整理
  10. ParrotSec-OS渗透测试系统