项目地址
https://github.com/youth5201314/banner
记得加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
导入glide
compile 'com.github.bumptech.glide:glide:3.7.0'
代码来了
MainActivity
package com.example.administrator.demo2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.Activity; import android.os.Bundle; import com.youth.banner.Banner; import com.youth.banner.BannerConfig; import com.youth.banner.Transformer; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { private Banner banner; private List<String> urlList = new ArrayList<>(); private List<String> titles = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); } private void initData() { urlList.add("http://p2.so.qhmsg.com/t012260d3b41f75fd6c.jpg"); urlList.add("http://p4.so.qhmsg.com/t013ffda9265fda17e9.jpg"); urlList.add("http://p1.so.qhmsg.com/t01eb38aed4329fff56.jpg"); urlList.add("http://p3.so.qhmsg.com/t018bad5bce1c4ad613.jpg"); titles.add("图片1"); titles.add("图片2"); titles.add("图片3"); titles.add("图片4"); } private void initView() { banner = (Banner) findViewById(R.id.banner); banner.setImages(urlList).setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE).setBannerTitles(titles) .setBannerAnimation(Transformer.Tablet).setImageLoader(new GlideImageLoader()).start(); } }
GlideImageLoader
package com.example.administrator.demo2; import android.content.Context; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.youth.banner.loader.ImageLoader; /** * Created by Administrator on 2018-01-07. */ public class GlideImageLoader extends ImageLoader { @Override public void displayImage(Context context, Object path, ImageView imageView) { Glide.with(context).load(path).into(imageView); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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.administrator.demo2.MainActivity"> <com.youth.banner.Banner xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="100dp" /> </LinearLayout>