添加依赖
compile 'com.youth.banner:banner:1.4.9'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
权限和以前的一样
先写一个全局的类
package com.bawie.day1018;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
//初始化组件
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(configuration);
}
}
加载图片
package com.bawie.day1018;
import android.content.Context;
import android.widget.ImageView;
import com.youth.banner.loader.ImageLoader;
/**
* Created by Administrator on 2017/10/18.
*/
public class BannerImageLoader extends ImageLoader{
@Override
public void displayImage(Context context, Object obj, ImageView imageView) {
com.nostra13.universalimageloader.core.ImageLoader instance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
instance.displayImage((String) obj,imageView);
}
}
主界面
package com.bawie.day1018;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.youth.banner.Banner;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private Banner banner;
String[] urls = {"https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s160-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s160-c/Another%252520Rockaway%252520Sunset.jpg",
"https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s160-c/Antelope%252520Butte.jpg",
"https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s160-c/Antelope%252520Hallway.jpg",
"https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s160-c/Antelope%252520Walls.jpg"};
ArrayList<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner = (Banner) findViewById(R.id.banner);
//图片加载器
banner.setImageLoader(new BannerImageLoader());
//设置图片集合
banner.setImages(Arrays.asList(urls));
//启动轮播
banner.start();
}
}
布局
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="368dp"
android:layout_height="150dp"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp" />