Android自定义封装banner组件

所属分类: 软件编程 / Android 阅读数: 33
收藏 0 赞 0 分享

自定义封装 banner 组件,供大家参考,具体内容如下

1. 效果图预览

 2.基本功能

一个简单方便的轮播图组件,基于viewpager 基础上进行的封装。
可设置 项目中图片,网络图片, View;
支持循环自动播放,手势滑动切换,item点击事件,可设置 点点的样式宽高、颜色、大小、位置 ;
可设置蒙层;可设置 是否允许滑动;可设置 是否允许循环。 

3.基本实现

1). 自定义属性

 <declare-styleable name="BannerLayoutStyle">
  <attr name="maskStartColor" format="color|reference" />
  <attr name="maskEndColor" format="color|reference" />
  <attr name="selectedIndicatorColor" format="color|reference" />
  <attr name="unSelectedIndicatorColor" format="color|reference" />
  <attr name="indicatorShape" format="enum">
   <enum name="rect" value="0" />
   <enum name="oval" value="1" />
  </attr>
  <attr name="selectedIndicatorHeight" format="dimension|reference" />
  <attr name="selectedIndicatorWidth" format="dimension|reference" />
  <attr name="unSelectedIndicatorHeight" format="dimension|reference" />
  <attr name="unSelectedIndicatorWidth" format="dimension|reference" />
  <attr name="indicatorPosition" format="enum">
   <enum name="centerBottom" value="0" />
   <enum name="rightBottom" value="1" />
   <enum name="leftBottom" value="2" />
   <enum name="centerTop" value="3" />
   <enum name="rightTop" value="4" />
   <enum name="leftTop" value="5" />
  </attr>
  <attr name="indicatorSpace" format="dimension|reference" />
  <attr name="indicatorMargin" format="dimension|reference" />
  <attr name="autoPlayDuration" format="integer|reference" />
  <attr name="scrollDuration" format="integer|reference" />
  <attr name="isAutoPlay" format="boolean" />
  <attr name="defaultImage" format="integer|reference" />
  <attr name="isIndicatorVisible" format="boolean" />
  <attr name="cornerRadii" format="dimension|reference" />
 </declare-styleable>

2).基本方法

//添加本地图片路径
public void setViewRes(List<Integer> viewRes) {。。。}

//添加网络图片路径
public void setViewUrls(List<String> urls) {。。。}

//添加任意View视图
private void setViews2(final List<View> views) {。。。}

// 设置是否允许 循环
 public void setLoop(boolean loop) {

 }

 // 设置是否可以滑动
 public void setSlideable(boolean slideable) {

 }

更多用法 详见代码,这里就不全部粘贴了。

3).使用示例:

<com.dzq.widget.CustomBannerView
  android:id="@+id/banner"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  app:autoPlayDuration="2000"
  app:defaultImage="@drawable/bg_banner"
  app:indicatorMargin="@dimen/indicatorMargin"
  app:indicatorPosition="rightBottom"
  app:indicatorShape="oval"
  app:indicatorSpace="@dimen/indicatorSpace"
  app:isAutoPlay="true"
  app:isIndicatorVisible="true"
  app:scrollDuration="1000"
  app:selectedIndicatorColor="@color/color_ec407a"
  app:selectedIndicatorHeight="5dp"
  app:selectedIndicatorWidth="5dp"
  app:unSelectedIndicatorColor="@color/color_71d9e7"
  app:unSelectedIndicatorHeight="5dp"
  app:unSelectedIndicatorWidth="5dp"

  />

 <com.dzq.widget.CustomBannerView
  android:id="@+id/banner2"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="5dp"
  android:layout_weight="1"
  app:autoPlayDuration="2000"
  app:defaultImage="@drawable/bg_banner"
  app:indicatorMargin="@dimen/indicatorMargin"
  app:indicatorPosition="rightBottom"
  app:indicatorShape="rect"
  app:indicatorSpace="@dimen/indicatorSpace"
  app:isAutoPlay="false"
  app:isIndicatorVisible="true"
  app:scrollDuration="1000"
  app:selectedIndicatorColor="@color/color_ec407a"
  app:selectedIndicatorHeight="5dp"
  app:selectedIndicatorWidth="10dp"
  app:unSelectedIndicatorColor="@color/color_71d9e7"
  app:unSelectedIndicatorHeight="10dp"
  app:unSelectedIndicatorWidth="5dp"

  />

 <com.dzq.widget.CustomBannerView
  android:id="@+id/banner3"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="5dp"
  android:layout_weight="1"
  app:cornerRadii="5dp"
  app:indicatorMargin="@dimen/indicatorMargin"
  app:indicatorPosition="centerTop"
  app:indicatorShape="rect"
  app:indicatorSpace="@dimen/indicatorSpace"
  app:isAutoPlay="false"
  app:isIndicatorVisible="true"
  app:maskEndColor="#00000000"
  app:maskStartColor="#99000000"
  app:scrollDuration="1000"
  app:selectedIndicatorColor="#00CAA9"
  app:selectedIndicatorHeight="10dp"
  app:selectedIndicatorWidth="25dp"
  app:unSelectedIndicatorColor="#26000000"
  app:unSelectedIndicatorHeight="10dp"
  app:unSelectedIndicatorWidth="10dp" />

项目源码下载

导入自己项目

How to

To get a Git project into your build:

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

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

Step 2. Add the dependency

dependencies {
  compile 'com.github.dingzuoqiang:CustomBannerView:v1.0'
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

老生常谈Android HapticFeedback(震动反馈)

下面小编就为大家带来一篇老生常谈Android HapticFeedback(震动反馈)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详谈OnTouchListener与OnGestureListener的区别

下面小编就为大家带来一篇详谈OnTouchListener与OnGestureListener的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿知乎悬浮功能按钮FloatingActionButton效果

前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,下面通过本文给大家分享adroid仿知乎悬浮功能按钮FloatingActionButton效果,需要的朋友参考下吧
收藏 0 赞 0 分享

解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

这篇文章主要介绍的Android V7后自定义Toolbar、ActionBar左侧有空白问题的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android常见控件使用详解

这篇文章主要为大家详细介绍了Android常见控件的使用方法,包括ProgressBar进度条控件、AlertDialog对话框控件等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简洁的APP更新dialog数字进度条

这篇文章主要为大家详细介绍了Android实现简洁的APP更新dialog数字进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 判断当前语言环境是否是中文环境

本文主要介绍了Android 判断当前语言环境是否是中文环境的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详谈Android中Matrix的set、pre、post的区别

下面小编就为大家带来一篇详谈Android中Matrix的set、pre、post的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现登录界面记住密码的存储

这篇文章主要为大家详细介绍了Android SharedPreferrences实现登录界面记住密码的存储,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 使用SharedPreferrences储存密码登录界面记住密码功能

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入,下面通过实例代码给大家讲解下,需要的朋友参考下吧
收藏 0 赞 0 分享
查看更多