计算机知识分享网

【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
viewpagerMarginviewpager页面间距dimension ,默认为10dp
isClipChildrenModeLessThree少于三张是否支持一屏多显模式 boolean类型,默认为false 默认不开启
bannerBottomMarginbanner轮播区域底部margin,可设置指示器距离轮播图的间距dimension ,默认为0dp
scaleType设置占位图缩放类型scaleType类型
showIndicatorInCenter设一屏多显模式下 指示器是否显示在中间图片位置,默认显示中间boolean类型
isClickSide一屏多显模式下 是否支持点击侧边切换图片,默认开启boolean类型

官网地址: https://github.com/xiaohaibin/XBanner

#Android