添加依赖
implementation 'com.youth.banner:banner:1.4.10'
添加权限
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity主界面
public class MainActivity extends AppCompatActivity {
private Banner banner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner = findViewById(R.id.banner);
//设置banner样式
banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
//BannerConfig.CIRCLE_INDICATOR 小圆点 但是没有标题
//BannerConfig.CIRCLE_INDICATOR_TITLE 显示圆形指示器和标题
//BannerConfig.NUM_INDICATOR 显示数字指示器
// BannerConfig.NUM_INDICATOR_TITLE 显示数字指示器和标题
//banner.setIndicatorGravity(BannerConfig.LEFT);//指示器中间位置
//BannerConfig.CENTER
//BannerConfig.RIGHT
//BannerConfig.LEFT
//图片加载器
banner.setImageLoader(new ImageLoaderInterface<ImageView>() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
BannerResponse.Banner banner= (BannerResponse.Banner) path;
ImageLoader.getInstance().displayImage(banner.getPicUrl(),imageView);
}
@Override
public ImageView createImageView(Context context) {
ImageView imageView=new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
});
initData();
}
private void initData() {
NetUtil.requestData("https://api.tianapi.com/wxnew/?key=c4aa776e0a51d57d6750511e2baa46b6&num=6&page=1", BannerResponse.class, new NetUtil.CallBack<BannerResponse>() {
@Override
public void getdata(BannerResponse o) {
//设置图片集合
banner.setImages(o.getNewslist());
//设置标题源
banner.setBannerTitles(getTitles(o));
//banner设置方法全部调用完毕时最后调用
banner.start();
}
});
}
private List<String> getTitles(BannerResponse bannerResponse) {
List<String> result = new ArrayList<>();
for (BannerResponse.Banner banner: bannerResponse.getNewslist()) {
result.add(banner.getTitle());
}
return result;
}
}
工具类NetUtil
public class NetUtil {
//接口
public interface CallBack<T>{
void getdata(T t);
}
//异步任务
public static void requestData(String strUrl, final Class clazz, final CallBack callBack){
new AsyncTask<String, Void, Object>() {
@Override
protected Object doInBackground(String... strings) {
return NetUtil.requestData(strings[0],clazz);
}
@Override
protected void onPostExecute(Object o) {
callBack.getdata(o);
}
}.execute(strUrl);
}
//网络解析
public static <T> T requestData(String url,Class clazz){
T t= (T) new Gson().fromJson(NetUtil.requestData(url),clazz);
return t;
}
//网络请求
public static String requestData(String str){
String result="";
try {
//定义地址
URL url=new URL(str);
//打开连接
HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
//请求方法
urlConnection.setRequestMethod("GET");
//读取超时
urlConnection.setReadTimeout(5000);
//连接超时
urlConnection.setConnectTimeout(5000);
if (urlConnection.getResponseCode()==200){
result = NetUtil.StreamToString(urlConnection.getInputStream());
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//字节流转换为字符流
private static String StreamToString(InputStream inputStream) throws IOException {
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
BufferedReader buff=new BufferedReader(inputStreamReader);
StringBuilder builder=new StringBuilder();
for (String imp=buff.readLine();imp!=null;imp=buff.readLine()){
builder.append(imp);
}
return builder.toString();
}
}
RannerResponse
public class BannerResponse {
private int code;
private String msg;
private List<Banner> newslist;
private final int SUCCESS_CODE = 200;
public boolean isSuccess() {
return code == SUCCESS_CODE;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public List<Banner> getNewslist() {
return newslist;
}
public class Banner {
private String title;
private String picUrl;
public String getTitle() {
return title;
}
public String getPicUrl() {
return picUrl;
}
}
}
App图片处理
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(
new ImageLoaderConfiguration.Builder(this)
.memoryCacheSizePercentage(10)
.discCacheSize(50 * 1024 * 1024)
.defaultDisplayImageOptions(
new DisplayImageOptions.Builder()
.showImageOnFail(R.mipmap.ic_launcher)
.showImageOnLoading(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisk(true)
.build()
)
.build()
);
}
}