Android广告图片轮播控件
GitHub地址/youth514/banner
1.添加依赖
老版本
//图片轮播框架implementation 'com.youth.banner:banner:1.4.10'//glideimplementation 'com.github.bumptech.glide:glide:3.7.0'
最新版
//图片轮播框架implementation 'com.youth.banner:banner:2.1.0'//glideimplementation 'com.github.bumptech.glide:glide:4.11.0'//recyclerviewimplementation 'androidx.recyclerview:recyclerview:1.1.0'//viewpager2implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01'
2.添加权限
<!-- if you want to load images from the internet --><uses-permission android:name="android.permission.INTERNET" />
3.布局中加入banner
<com.youth.banner.Bannerxmlns:app="/apk/res-auto"android:id="@+id/banner"android:layout_width="match_parent"android:layout_height="200dp" />
4.新建一个Java Class 重写图片加载器
import android.content.Context;import android.widget.ImageView;import com.bumptech.glide.Glide;import com.youth.banner.loader.ImageLoader;public class GlideImageLoader extends ImageLoader {@Overridepublic void displayImage(Context context, Object path, ImageView imageView) {//Glide 加载图片简单用法Glide.with(context).load(path).into(imageView);}}
5.测试代码,在MainActivity中
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import com.youth.banner.Banner;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);List images = new ArrayList();images.add("/uploadfile//0907/thumb_1_1380_460_0907013518839623.jpg");images.add("/uploadfile//0906/thumb_1_1380_460_0906040153529630.jpg");images.add("/uploadfile//0907/thumb_1_1380_460_0907114844929630.jpg");Banner banner = (Banner) findViewById(R.id.banner);//设置图片加载器banner.setImageLoader(new GlideImageLoader());//设置图片集合banner.setImages(images);//banner设置方法全部调用完毕时最后调用banner.start();//增加点击事件banner.setOnBannerListener(new OnBannerListener() {@Overridepublic void OnBannerClick(int position) {Toast.makeText(MainActivity.this, "position"+position, Toast.LENGTH_SHORT).show();}});}}
新版的Banner2的用法
java
private void initBanner() {Banner banner = findViewById(R.id.banner);banner.setAdapter(new BannerImageAdapter<DataBean>(DataBean.getTestData3()) {@Overridepublic void onBindView(BannerImageHolder holder, DataBean data, int position, int size) {//图片加载自己实现Glide.with(holder.itemView).load(data.imageUrl).apply(RequestOptions.bitmapTransform(new RoundedCorners(30))).into(holder.imageView);}}).setIndicator(new RectangleIndicator(this)).addBannerLifecycleObserver(this).setOnBannerListener((OnBannerListener<DataBean>) (data, position) -> Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show());}
kotlin
banner.adapter = object : BannerImageAdapter<Advertising.AdvertisementsBean.AdvertisementListBean>(advertisements) {override fun onBindView(holder: BannerImageHolder, data: Advertising.AdvertisementsBean.AdvertisementListBean, position: Int, size: Int) {//图片加载自己实现Glide.with(holder.itemView).load(data.url).apply(RequestOptions.bitmapTransform(RoundedCorners(30))).into(holder.imageView)}}banner.setIndicator(RectangleIndicator(context)).addBannerLifecycleObserver(this).setOnBannerListener(OnBannerListener {data: Advertising.AdvertisementsBean.AdvertisementListBean?, position: Int ->logd("${data?.jumpUrl}")})