【android】Android开发库xbanner使用步骤
gradle引入xbanner的库
implementation ('com.xhb:xbanner:1.3.1') {
exclude group: 'com.android.support'
}
创建布局xml
<com.stx.xhb.xbanner.XBanner
android:id="@+id/xbanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:AutoPlayTime="3000"
app:pointsContainerBackground="#44aaaaaa"
app:tipTextSize="12sp"
app:isShowNumberIndicator="false"
app:isShowIndicatorOnlyOne="false"
app:pageChangeDuration="800"/>
Activity中渲染图片
// 初始化XBanner中展示的数据
final ArrayList<String> images = new ArrayList<>();
for (int i = 0; i <homeBanner.size() ; i++) {
images.add(homeBanner.get(i).getBannerUrl());
}
// 为XBanner绑定数据
xbanner_view.setData(images, null);
// XBanner适配数据
xbanner_view.setmAdapter(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, View view, int position) {
// 选择对应的图片库加载图片就可以,这里演示glide
Glide.with(getActivity()).load(images.get(position)).into((ImageView) view);
}
});
// 设置XBanner的页面切换特效
xbanner_view.setPageTransformer(Transformer.Default);
// 设置XBanner页面切换的时间,即动画时长
xbanner_view.setPageChangeDuration(1000);
Activity生命周期联动:
@Override
public void onResume() {
super.onResume();
xbanner_view.startAutoPlay();
}
@Override
public void onStop() {
super.onStop();
xbanner_view.stopAutoPlay();
}
自定义属性说明
| 属性名 | 属性说明 | 属性值 |
|---|---|---|
| isAutoPlay | 是否支持自动轮播 | boolean类型,默认为true |
| isTipsMarquee | 是否支持提示性文字跑马灯效果 | boolean类型,默认为false |
| AutoPlayTime | 图片轮播时间间隔 | int值,默认为5s |
| pointNormal | 指示器未选中时状态点 | drawable,不设置的话为默认状态点 |
| pointSelect | 指示器选中时状态点 | drawable,不设置的话为默认状态点 |
| pointsVisibility | 是否显示指示器 | boolean类型,默认为true |
| pointsPosition | 指示器显示位置 | LEFT、CENTER、RIGHT类型,默认为CENTER |
| pointsContainerBackground | 指示器背景 | 可自定义设置指示器背景 |
| pointContainerPosition | 指示器容器显示位置 | TOP、BOTTOM类型,默认为BOTTOM |
| pointContainerLeftRightPadding | 指示点容器左右内间距 | dimension,默认为10dp |
| pointTopBottomPadding | 指示点上下内间距 | dimension,默认为6dp |
| pointLeftRightPadding | 指示点左右内间距 | dimension,默认为3dp |
| tipTextColor | 提示文案的文字颜色 | reference |
| tipTextSize | 提示文案的文字大小 | dimension,默认为10dp |
| isShowNumberIndicator | 是否显示数字指示器 | boolean,默认为false不显示 |
| numberIndicatorBacgroud | 数字指示器背景 | reference |
| isShowIndicatorOnlyOne | 当只有一张图片的时候是否显示指示点 | boolean,默认为false,不显示 |
| isShowTips | 是否展示文字 | boolean,默认为false,不显示 |
| pageChangeDuration | 图片切换速度 | int值,默认为1000ms |
| isHandLoop | 是否支持手动无限循环切换图片 | boolean类型,默认为false |
| placeholderDrawable | 设置整体轮播框架占位图 | reference |
| isClipChildrenMode | 是否开启一屏显示多个模式 | boolean类型,默认为false 默认不开启 |
| clipChildrenLeftMargin | 一屏显示多个左间距 | dimension ,默认为30dp |
| clipChildrenRightMargin | 一屏显示多个右间距 | dimension ,默认为30dp |
| clipChildrenTopBottomMargin | 一屏显示多个上下间距 | dimension ,默认为30dp |
| viewpagerMargin | viewpager页面间距 | dimension ,默认为10dp |
| isClipChildrenModeLessThree | 少于三张是否支持一屏多显模式 | boolean类型,默认为false 默认不开启 |
| bannerBottomMargin | banner轮播区域底部margin,可设置指示器距离轮播图的间距 | dimension ,默认为0dp |
| scaleType | 设置占位图缩放类型 | scaleType类型 |
| showIndicatorInCenter | 设一屏多显模式下 指示器是否显示在中间图片位置,默认显示中间 | boolean类型 |
| isClickSide | 一屏多显模式下 是否支持点击侧边切换图片,默认开启 | boolean类型 |