在我们使用banner的时候同样要先导入依赖:compile 'com.youth.banner:banner:1.4.9'
布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwie.bannerdemo.MainActivity"> <com.youth.banner.Banner android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="200dp"></com.youth.banner.Banner> </android.support.constraint.ConstraintLayout>
Main:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.youth.banner.Banner; import java.util.Arrays; public class MainActivity extends AppCompatActivity { private Banner banner; private String[] urls = {"https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg", "http://pic.90sjimg.com/back_pic/qk/back_origin_pic/00/01/94/3776f58b6e6386fd60de8a349ec4b966.jpg", "http://img.zcool.cn/community/01cc25568a183a6ac7251bb6b49a10.jpg@900w_1l_2o_100sh.jpg", "http://img.zcool.cn/community/0183c8568781b832f8759f040c8d36.jpg@900w_1l_2o_100sh.jpg", "http://img.zcool.cn/community/01cdbc56f9cebf32f875a9449e6dc3.png@900w_1l_2o_100sh.jpg"}; @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(); } }
MApp:
import android.app.Application; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; public class MApp extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); } }
BannerImageLoader:
import android.content.Context; import android.widget.ImageView; import com.youth.banner.loader.ImageLoader; public class BannerImageLoader extends ImageLoader { @Override public void displayImage(Context context, Object path, ImageView imageView) { com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance(); imageLoader.displayImage((String) path, imageView); } }