导入依赖:
implementation 'com.youth.banner:banner:1.4.9'
添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />
ImageLoader框架配置:
//缓存到SD卡的路径
File cacheFile = new File(Environment.getExternalStorageDirectory()+"/"+"image");
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions 内存缓存文件的最大长宽
.diskCacheExtraOptions(480, 800, null) // 本地缓存的详细信息(缓存的最大长宽),最好不要设置这个
.threadPoolSize(3) //配置线程池的数量 ******
.threadPriority(Thread. NORM_PRIORITY) //默认线程优先级 10表示最高优先级,5是普通优先级,1表示最低优先级,
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
.memoryCacheSizePercentage(13) // default
.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值****
.diskCacheFileCount(100) // 可以缓存的文件数量
.diskCache(new UnlimitedDiskCache(cacheFile))//自定义缓存目录*******
// default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs() // 打印debug log
.build();
ImageLoader instance = ImageLoader.getInstance();
instance.init(configuration);
banner布局:
<com.youth.banner.Banner
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/bannerPager">
</com.youth.banner.Banner>
轮播图片数组:
urls = new String[]{"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2310514390,3580363630&fm=27&gp=0.jpg",
"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=756318500,1648332548&fm=27&gp=0.jpg",
"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=764856423,3994964277&fm=27&gp=0.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg",
"http://img4.imgtn.bdimg.com/it/u=1956283034,3979465440&fm=200&gp=0.jpg",
"http://img1.imgtn.bdimg.com/it/u=232043804,3867273473&fm=26&gp=0.jpg"};
banner配置,启动:
//获取图片实例
final ImageLoader instance = ImageLoader.getInstance();
//给banner设置框架
pager.setImageLoader(new com.youth.banner.loader.ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
instance.displayImage((String) path,imageView);
}
});
//给banner添加接口数据 Arrays.asList(urls)将数组作为集合
pager.setImages(Arrays.asList(urls));
//启动banner,轮播
pager.start();