一 引用依赖
// 图片轮播图
implementation 'com.github.dongjunkun:BannerLayout:1.0.6'
二 创建图片加载工具类
public class GlideImageLoader implements BannerLayout.ImageLoader {
@Override
public void displayImage(Context context, String path, ImageView imageView) {
Glide.with(context).load(path).centerCrop().into(imageView);
}
}
三 在layout中引用
<com.yyydjk.library.BannerLayout
android:id="@+id/mBanner"
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoPlayDuration="2000"
app:indicatorMargin="10dp"
app:indicatorPosition="rightBottom"
app:indicatorShape="oval"
app:indicatorSpace="3dp"
app:isAutoPlay="true"
app:scrollDuration="600"
app:selectedIndicatorColor="?attr/colorPrimary"
app:selectedIndicatorHeight="6dp"
app:selectedIndicatorWidth="6dp"
app:unSelectedIndicatorColor="#99ffffff"
app:unSelectedIndicatorHeight="6dp"
app:unSelectedIndicatorWidth="6dp" />
补充:
autoPlayDuration 自动播放间隔时间
indicatorMargin 每个指示器之间的间隔
indicatorPosition 指示器的显示位置 我这里设置的是右下角
indicatorShape 指示器的形状 我这里设置的是圆形 也可以设置方形(rect)也可以是其他 根据项目所需
四 在Activity中引用
final List<String> imgUrls = new ArrayList<>();
imgUrls.add("");
imgUrls.add("");
imgUrls.add("");
//添加需要展示的图片的url
mBanner.setImageLoader(new GlideImageLoader());//设置加载器
mBanner.setViewUrls(imgUrls);绑定图片资源
//添加点击监听
bannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
}
到这里就实现了图片的轮播
引用了https://github.com/dongjunkun/BannerLayout进行了自己整理和论述