美文网首页
android 广告轮播图

android 广告轮播图

作者: 小黄牛 | 来源:发表于2024-01-24 09:54 被阅读0次
微信图片_20240124164807.png

主项目build.gradle:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

build.gradle引入:

implementation 'com.github.dongjunkun:BannerLayout:1.0.6'

或者

<dependency>
<groupId>com.github.dongjunkun</groupId>
<artifactId>BannerLayout</artifactId>
<version>1.0.6</version>
</dependency>
 <com.yyydjk.library.BannerLayout
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="10dp"
                        android:id="@+id/banner"
                        android:layout_width="match_parent"
                        android:layout_height="140dp"
                        app:autoPlayDuration="5000"
                        app:indicatorMargin="10dp"
                        app:indicatorPosition="rightBottom"
                        app:indicatorShape="oval"
                        app:indicatorSpace="3dp"
                        app:scrollDuration="1100"
                        app:selectedIndicatorColor="@color/colorPrimary"
                        app:selectedIndicatorHeight="6dp"
                        app:selectedIndicatorWidth="6dp"
                        app:unSelectedIndicatorColor="#ffffff"
                        app:unSelectedIndicatorHeight="6dp"
                        app:unSelectedIndicatorWidth="6dp" />
BannerLayout bannerLayout;
List<String> urls = new ArrayList<>();
urls.add("图片地址");
urls.add("图片地址");
urls.add("图片地址");
//网络地址
bannerLayout.setViewUrls(urls);
//设置加载器
bannerLayout.setImageLoader(new GlideImageLoader());
//添加点击监听
bannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });
public class GlideImageLoader  implements BannerLayout.ImageLoader {
    @Override
    public void displayImage(Context context, String path, ImageView imageView) {
        Glide.with(context).load(path).centerCrop().into(imageView);
    }
}

补充:
app:autoPlayDuration="5000": 设置轮播自动播放的间隔时间,这里是5000毫秒(5秒)。

app:indicatorMargin="10dp": 设置指示器(用于显示当前轮播位置的小点)与BannerLayout边缘的间距为10dp。

app:indicatorPosition="rightBottom": 设置指示器的位置为右下角,表示指示器将位于BannerLayout的右下角。

app:indicatorShape="oval": 设置指示器的形状为椭圆形。

app:indicatorSpace="3dp": 设置指示器之间的间距为3dp。

app:scrollDuration="1100": 设置轮播切换的动画时长为1100毫秒(1.1秒)。

app:selectedIndicatorColor="@color/colorPrimary": 设置当前轮播位置指示器的颜色为colorPrimary定义的颜色。

app:selectedIndicatorHeight="6dp": 设置当前轮播位置指示器的高度为6dp。

app:selectedIndicatorWidth="6dp": 设置当前轮播位置指示器的宽度为6dp。

app:unSelectedIndicatorColor="#ffffff": 设置非当前轮播位置指示器的颜色为白色。

app:unSelectedIndicatorHeight="6dp": 设置非当前轮播位置指示器的高度为6dp。

app:unSelectedIndicatorWidth="6dp": 设置非当前轮播位置指示器的宽度为6dp。

相关文章

网友评论

      本文标题:android 广告轮播图

      本文链接:https://www.haomeiwen.com/subject/texpodtx.html