app:bottomColor="@color/transparent" //底部遮罩 背景颜色
app:dotGravity="center" //小点位置 中间 左边 右边
app:dotIndicatorFocus="@color/colorPrimary" // 当前选中点的颜色 或者 任意drawable/mipmap资源(自定义样式的指示器)
app:dotIndicatorNormal="@color/colorGray" // 未选中的点的颜色
app:widthProportion="8" //设置宽高比时 宽度比例值
app:heightProportion="3" //设置宽高比时 高度比例值
app:bulge="true" //是否时两边突出的模式
app:bulgeDistance="30" // 两边显示突出的距离
app:bottomType="belowPic" // 底部整体小点在轮播控件里面的位置,是底部覆盖在轮播图上,还是在轮播图下方
app:dotSize="10dp" //小点之间的间距
app:pictureSpacing="10" // 两边突出时 两张图片之间的间隔距离 可以不用设置 默认值是10
//如果指示器不是圆点
设置自定义图片作为指示器选中圆点和为选中圆点
app:dotIndicatorFocus="@drawable/ic_favorite_border"
app:dotIndicatorNormal="@drawable/ic_favorite"
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.fazhongxu:LBannerView:v0.1.8'
}
<json.xxl.com.lbannerview.BannerView
android:layout_marginTop="20dp"
android:id="@+id/banner_view_below"
android:layout_width="match_parent"
android:layout_height="0dp"
app:bottomColor="@color/transparent"
app:dotGravity="center"
app:dotIndicatorFocus="@color/colorPrimary"
app:dotIndicatorNormal="@color/colorGray"
app:widthProportion="8"
app:heightProportion="3"
app:bulge="true"
app:pictureSpacing="10"
app:bulgeDistance="30"
app:bottomType="belowPic"
app:dotSize="10dp" />
final BannerView bannerView = findViewById(R.id.banner_view);
final BannerView belowBannerView = findViewById(R.id.banner_view_below);
bannerView.setAdapter(new BannerAdapter() {
@Override
public View getView(int position, View convertView) {
ImageView imageView = new ImageView(BannerActivity.this);
Glide.with(BannerActivity.this).load(bannberPictures[position]).into(imageView);
return imageView;
}
@Override
public int getCount() {
return bannberPictures.length;
}
@Override
public String getBannerDes(int position) {
return bannerDes[position];
}
});
// 如果 布局文件里面设置了 app:widthProportion="8" app:heightProportion="3" 宽高比 不是直接固定的高度
/* bannerView.post(new Runnable() { // post 方式
@Override
public void run() {
bannerView.setAdapter(new BannerAdapter() {
@Override
public View getView(int position, View convertView) {
ImageView imageView = new ImageView(BannerActivity.this);
Glide.with(BannerActivity.this).load(bannberPictures[position]).into(imageView);
return imageView;
}
@Override
public int getCount() {
return bannberPictures.length;
}
@Override
public String getBannerDes(int position) {
return bannerDes[position];
}
});
}
});
*/
belowBannerView.post(new Runnable() { // 如果布局里宽高比例没设置 是直接写死的高度 则不用post延时
@Override
public void run() {
belowBannerView.setAdapter(new BannerAdapter() { //指示点在banner图片下方 左右两边设置突出
@Override
public View getView(int position, View convertView) {
ImageView imageView = new ImageView(BannerActivity.this);
Glide.with(BannerActivity.this).load(bannberPictures[position]).into(imageView);
return imageView;
}
@Override
public int getCount() {
return bannberPictures.length;
}
});
}
});
banner_view.post { // //设置了宽高比 所以需要post 否则拿不到宽度 显示不出图片 或则显示的图片高度不对
kotlin.run {
banner_view.setAdapter(object : BannerAdapter(){
override fun getView(position: Int, convertView: View?): View {
val imageView = ImageView(this@MainActivity)
Glide.with(this@MainActivity)
.load(bannberPictures.get(position))
.into(imageView)
return imageView
}
override fun getCount(): Int {
return bannberPictures.size
}
override fun getBannerDes(position: Int): String {
return bannerDes.get(position)
}
})
}
}
banner_view_below.post{
kotlin.run {
banner_view_below.setAdapter(object : BannerAdapter() {
override fun getView(position: Int, convertView: View?): View {
val imageView = ImageView(this@MainActivity)
Glide.with(this@MainActivity)
.load(bannberPictures.get(position))
.into(imageView)
return imageView
}
override fun getCount(): Int {
return bannberPictures.size
}
})
}
}
banner_view_below.changeDuration(1200) // 改变轮播速率
banner_view_below.startScroll() //开启自动滚动
备注 依赖的 implementation 'com.github.fazhongxu:LBannerView:v0.1.6' 必须大于 v0.1.6 ,v0.1.6 作者在调试开源依赖 v0.1.6 是第一个完整的可用的版本
自动轮播 belowBannerView.startScroll(); 改变轮播 速度 banner_view_below.changeDuration(1200)