1.第一步,添加依赖:
compile 'com.youth.banner:banner:1.4.9'
compile 'com.github.bumptech.glide:glide:4.0.0-RC0'
3.关于Banner在布局中引用:
private List strList = new ArrayList<>();
private String[] images = {
"http://l2.51fanli.net//tuan//images//1//5806eac956808.jpg",
"http://l2.51fanli.net//tuan//images//b//580991bb30560.jpg",
"http://l0.51fanli.net//tuan//images//b//58115f2593dc3.jpg",
"http://l2.51fanli.net//tuan//images//0//57923840b054d.jpg",
"http://l2.51fanli.net//tuan//images//e//58101e11ab164.jpg"};
for (String s:images) {
strList.add(s);
}
4.2加载从网络获取的图片
用xutils3网络请求数据,需要导入xutils3的依赖和帮助类(自己导入)
private void getDataFromServer() {
//url
String url1 = "http://api.kkmh.com/v1/topic_new/discovery_list?gender=0&sa_event=eyJwcm9qZWN0Ijoia
3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3ODM5MDM5MzE1LCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21t
dW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24iOiI0LjIuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tRmluZENhdGVnb
3J5VGFiTmFtZSI6IuWFqOmDqCIsIklzQXV0b0xvYWQiOmZhbHNlLCIkbGliX3ZlcnNpb24iOiIxLjYuMzQiLCIkbmV0d29ya190eXBlIjoiV0l
GSSIsIiR3aWZpIjp0cnVlLCIkbWFudWZhY3R1cmVyIjoic2Ftc3VuZyIsIkZyb21GaW5kVGFiTmFtZSI6IuaOqOiNkCIsIiRzY3JlZW
5faGVpZ2h0Ijo1NzYsIkNhdGVnb3J5Ijoi5peg5rOV6I635Y-WIiwiSG9tZXBhZ2VVcGRhdGVEYXRlIjowLCJQcm9wZXJ0eUV2ZW5
0IjoiUmVhZEZpbmRQYWdlIiwiRmluZFRhYk5hbWUiOiLmjqjojZAiLCJhYnRlc3RfZ3JvdXAiOjQ2LCIkc2NyZWVuX3dpZHRoIjo
xMDI0LCJGaW5kQ2F0ZWdvcnlUYWJOYW1lIjoi5YWo6YOoIiwiJG9zIjoiQW5kcm9pZCIsIlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2
UiLCIkY2FycmllciI6IkNNQ0MiLCIkbW9kZWwiOiJHVC1QNTIxMCIsIiRhcHBfdmVyc2lvbiI6IjMuOC4xIn0sInR5cGUiOiJ0cmFja
yIsImRpc3RpbmN0X2lkIjoiQTo5MDUxMDQyNzYzNzU1MTA5Iiwib3JpZ2luYWxfaWQiOiJBOjkwNTEwNDI3NjM3NTUxMDkiLCJldmV
udCI6IlJlYWRGaW5kUGFnZSJ9";
RequestParams params1 = new RequestParams();
params1.setUri(url1);
//用xutils3网络请求数据,需要导入xutils3的依赖和帮助类(自己导入)
x.http().get(params1, new Callback.CommonCallback() {
@Override
public void onSuccess(final String result) {
if (result != null) {
Gson gson = new Gson();
BannerBean bannerBean = gson.fromJson(result, BannerBean.class);
List infos = bannerBean.getData().getInfos();
for (BannerBean.DataBean.InfosBean bb : infos) {
List banners = bb.getBanners();
for (BannerBean.DataBean.InfosBean.BannersBean nn : banners) {
String pic = nn.getPic();
strList.add(pic);
}
}
banner.setImageLoader(new BannerGilde());
banner.setImages(strList);
banner.setDelayTime(2000);
banner.isAutoPlay(true);
banner.start();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.i(TAG, "onError: ");
}
@Override
public void onCancelled(CancelledException cex) {
Log.i(TAG, "onCancelled: ");
}
@Override
public void onFinished() {
Log.i(TAG, "onFinished: ");
}
});
}
5.需要继承imageLoader来操作图片的加载
class BannerGilde extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(getContext()).load(path).into(imageView);
}
}