android对于广告栏的要求:
无限轮播
自动加手动滑动
简单的自定义指示器样式及位置
支持本地图片及网络图片
滑动流畅,无卡顿,无闪烁
广告页面不限制个数
页面点击监听事件
简单易用,高配置,无明显bug
动画效果默认的就好
实现效果图:
前期准备工作:
1.添加类库,在build.gradle中添加对Glide (图片加载框架)的引用:
compile 'com.ydevelop:bannerlayout:1.0.4'
2.AndroidManifest.xml中开网络权限,因为需要获取网络的图片
<uses-permission android:name="android.permission.INTERNET"/>
(1)在values目录下新建一个attr.xml文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="visible_tab_count" format="integer"/> <declare-styleable name="BannerLayoutStyle"> <attr name="selectedIndicatorColor" format="color|reference"/> <attr name="unSelectedIndicatorColor" format="color|reference"/> <attr name="titleBGColor" format="color|reference"/> <attr name="titleColor" 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="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"/> </declare-styleable> </resources>
(2)自定义一个类BannerLayout,实现轮播图效果:注意类的包名,见后面
(3)新建一个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="match_parent" android:orientation="vertical" > <com.example.administrator.testb.BannerLayout android:id="@+id/banner2" android:layout_width="match_parent" android:layout_height="210dp" app:defaultImage="@mipmap/ic_launcher" app:scrollDuration="1000" app:autoPlayDuration="3000" app:unSelectedIndicatorHeight="10dp" app:unSelectedIndicatorWidth="10dp" app:selectedIndicatorHeight="10dp"