//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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.example.banner.MainActivity">
<com.youth.banner.Banner
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/banner"
></com.youth.banner.Banner>
</RelativeLayout>
//MainActivity
package com.example.banner;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.youth.banner.Banner;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Banner banner;
private List<String> list=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner = (Banner) findViewById(R.id.banner);
list.add("http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/15/14_56_29_117_12420_01.jpg");
list.add("http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/09/14_11_02_821_12967_01.jpg");
list.add("http://www.93.gov.cn/11002/upload/webcms/content/image/2017/09/27/08_57_48_840_53302_01.jpg");
list.add("http://www.93.gov.cn/11002/upload/webcms/content/image/2017/09/20/09_11_17_163_37240_01111.jpg");
list.add("http://www.93.gov.cn/11002/upload/webcms/content/image/2017/09/21/11_12_58_489_41459_20170914093742676.jpg");
//设置图片加载器
banner.setImageLoader(new MImage());
//设置含有图片路径的集合
banner.setImages(list);
//设置延迟时间
banner.setDelayTime(3000);
//是否自动轮播
banner.isAutoPlay(true);
//开启
banner.start();
}
}
//MImage
package com.example.banner;
import android.content.Context;
import android.widget.ImageView;
import com.youth.banner.loader.ImageLoader;
/**
* Created by 123 on 2017/10/23.
*/
public class MImage extends ImageLoader{
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
com.nostra13.universalimageloader.core.ImageLoader instance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
instance.displayImage((String) path, imageView);
}
}
//MyApplication
package com.example.banner;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
/**
* Created by 123 on 2017/10/23.
*/
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration imageLoaderConfiguration=new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(imageLoaderConfiguration);
}
}
//包,权限
universal-image-loader-1.9.5.jar
gson-2.2.4.jar
design包
联网
.name