效果图
项目地址 https://github.com/youth5201314/banner
权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
compile
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.youth.banner:banner:1.4.10'
代码来了
MainActivity
package com.example.administrator.demo3; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.youth.banner.Banner; import com.youth.banner.BannerConfig; import com.youth.banner.listener.OnBannerListener; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<String> img = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Banner banner= (Banner) findViewById(R.id.bannervp); //添加图片 img.add("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1089399937,1684001946&fm=23&gp=0.jpg"); img.add("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=787324823,4149955059&fm=23&gp=0.jpg"); img.add("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3514483810,1208920576&fm=23&gp=0.jpg"); img.add("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1917280427,3920336884&fm=23&gp=0.jpg"); banner.setImageLoader(new GlideImageLoader()); banner.setIndicatorGravity(BannerConfig.RIGHT);//圆点的位置 banner.setImages(img).//加载的图片 setBannerStyle(BannerConfig.CIRCLE_INDICATOR). setDelayTime(2000).start();//图片循环滑动的时间2秒 //设置点击事件 banner.setOnBannerListener(new OnBannerListener() { @Override public void OnBannerClick(int position) { switch (position){ case 0: Toast.makeText(MainActivity.this,"1111",Toast.LENGTH_LONG).show(); break; case 1: Toast.makeText(MainActivity.this,"222",Toast.LENGTH_LONG).show(); break; case 2: Toast.makeText(MainActivity.this,"333",Toast.LENGTH_LONG).show(); break; case 3: Toast.makeText(MainActivity.this,"444",Toast.LENGTH_LONG).show(); break; } } }); } }
GlideImageLoader
package com.example.administrator.demo3; import android.content.Context; import android.widget.ImageView; import com.squareup.picasso.Picasso; 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) { Picasso.with(context).load((String) path).into(imageView); } }
activity_main
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.youth.banner.Banner android:id="@+id/bannervp" android:layout_width="match_parent" android:layout_height="200dp" /> </RelativeLayout>