Android-图片轮播banner
开源地址
https://gitee.com/hcg2011/banner/tree/master
使用总结
添加依赖 app/build.gradle
1
2
3
4implementation ("com.github.bumptech.glide:glide:4.9.0") {
exclude group: "com.android.support"
}
compile 'com.youth.banner:banner:1.4.10' //最新版本添加访问网络和本地文件权限
1
2
3
4
5<!-- if you want to load images from the internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- if you want to load images from a file OR from the internet -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />页面布局中添加组件
1
2
3
4
5<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="300dp" />GlideImageLoader 图片加载实现
package com.example.myapplication.ui.login; import android.content.Context; import android.net.Uri; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.youth.banner.loader.ImageLoader; public class GlideImageLoader extends ImageLoader { @Override public void displayImage(Context context, Object path, ImageView imageView) { /** 注意: 1.图片加载器由自己选择,这里不限制,只是提供几种使用方法 2.返回的图片路径为Object类型,由于不能确定你到底使用的那种图片加载器, 传输的到的是什么格式,那么这种就使用Object接收和返回,你只需要强转成你传输的类型就行, 切记不要胡乱强转! */ //Glide 加载图片简单用法 Glide.with(context).load(path).into(imageView); //Picasso 加载图片简单用法 // Picasso.with(context).load(path).into(imageView); //用fresco加载图片简单用法,记得要写下面的createImageView方法 // Uri uri = Uri.parse((String) path); // imageView.setImageURI(uri); } //提供createImageView 方法,如果不用可以不重写这个方法,主要是方便自定义ImageView的创建 // @Override // public ImageView createImageView(Context context) { // //使用fresco,需要创建它提供的ImageView,当然你也可以用自己自定义的具有图片加载功能的ImageView // SimpleDraweeView simpleDraweeView=new SimpleDraweeView(context); // return simpleDraweeView; // } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
6. Activity onCreate 中加载网络图片
```java
List<String> images = new ArrayList<String>(){{
add("https://uploadfile.bizhizu.cn/up/00/84/69/0084694a7d0c75849d963eb32fdf2781.jpg");
add("https://www.tuimzi.com/pic/2019/05/19/wy0kr0atbou.jpg");
}};
Banner banner = (Banner) findViewById(R.id.banner);
//设置图片加载器
banner.setImageLoader(new GlideImageLoader());
//设置图片集合
banner.setImages(images);
//banner设置方法全部调用完毕时最后调用
banner.start();
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 王文哲的博客!