前段时间项目中要写一个轮播图,之前直接在网上随便找了一个,但是用起来有些麻烦,并且不灵活,比如不支持指示器的图片设置,不支持指示器图片的大小等等,还有出现了滑动冲突,于是决定自己封装一个,解决这些问题,于是有了这篇博客。也就是说这个Banner 能更灵活的设置指示器,解决冲突, 仅此支持触摸停止自定播放,非触摸重新轮播,简单易用,只要把BannerInfo类集合正确加入即可
1.导入以上的jar包
必须依赖Picasso compile ‘com.squareup.picasso:picasso:2.5.2’
网络请求权限:
测试的图片链接:
http://static.kaytrip.com/uploads/new_style/201703/PJhe9kxkjZ.jpg
http://static.kaytrip.com/uploads/new_style/201704/HMzdNwGL8p.jpg
http://static.kaytrip.com/uploads/new_style/201612/HnJb3hluX1.jpg
http://static.kaytrip.com/uploads/new_style/201612/UCLWlfq39E.jpg
http://static.kaytrip.com/uploads/new_style/201607/lZm9nfpxFN.jpg
2.在布局的响应位置添加FbxBanner
<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="kaiyuan.dmeo.MainActivity">
<kaiyuan.bannerlibrary.FbxBanner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</RelativeLayout>
3.找控件
mFbxBanner = (FbxBanner) findViewById(R.id.banner);
4. BannerInfo广告类的集合
private List<BannerInfo> list = new ArrayList<>();
List<String> urlList = new ArrayList<String>();
urlList.add("http://static.kaytrip.com/uploads/new_style/201703/PJhe9kxkjZ.jpg");
urlList.add("http://static.kaytrip.com/uploads/new_style/201704/HMzdNwGL8p.jpg");
urlList.add("http://static.kaytrip.com/uploads/new_style/201612/HnJb3hluX1.jpg");
urlList.add("http://static.kaytrip.com/uploads/new_style/201612/UCLWlfq39E.jpg");
urlList.add("http://static.kaytrip.com/uploads/new_style/201607/lZm9nfpxFN.jpg");
for (int i = 0; i < urlList.size(); i++) {
BannerInfo bannerInfo = new BannerInfo();
//将网络请求的地址加入
bannerInfo.setUrl("图片的链接");
//BannerInfo集合的大小 ,比方说有五张图片,则一次添加进集合
list.add(bannerInfo);
}
5. BannerInfo集合添加完毕之后就可以开始了
mFbxBanner.setBannerDate(list);//将BannerInfo集合设置进去
mFbxBanner.setCycle(true);//是否可以循环
//指示器选中和未选中的图片
mFbxBanner.setIndecatorDrawable(R.drawable.shop_indicator, R.drawable.shop_indicator_two);
//指示器图片的距离
mFbxBanner.setIndecatorMagin(20);
//指示器的位置,左边:“lift” 右边:“right” 默认中间
mFbxBanner.setIndecatorToLayout("");
//是否自动播放
mFbxBanner.setAutoPlay(true);
//自动播放的时间
mFbxBanner.setTimeToAutoPlay(4000);
//设置指示器图片的大小
mFbxBanner.setIndecatorIconSize(50,20);
//点击图片获取当前的图片位置
mFbxBanner.setOnBannerClickItemListener(new FbxBanner.OnBannerClickItemListener() {
@Override
public void onClickItem(int position) {
Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show();
}
});
//设置完毕调用这个开始执行
mFbxBanner.start();