一. 添加依赖
implementation 'com.youth.banner:banner:1.4.10'
implementation 'com.github.bumptech.glide:glide:3.7.0'
二. 布局
<com.youth.banner.Banner
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"/>
三. 设置图片源数据
public class GlideImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
}
四. 主界面
public class MainActivity extends AppCompatActivity {
private Banner banner;
private GlideImageLoader glideImageLoader;
private static List<Integer> imageList = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData(){
glideImageLoader = new GlideImageLoader();
imageList.add(R.mipmap.hg);
imageList.add(R.mipmap.gbjd);
imageList.add(R.mipmap.bzj);
imageList.add(R.mipmap.xhcy);
}
private void initView() {
banner = findViewById(R.id.banner);
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
banner.setImageLoader(glideImageLoader);
banner.setBannerAnimation(Transformer.ZoomOutSlide);
banner.setDelayTime(3000);
banner.isAutoPlay(true);
banner.setIndicatorGravity(BannerConfig.CENTER);
banner.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 20);
}
});
banner.setClipToOutline(true);
banner.setImages(imageList).start();
}
}
|