Android无限循环轮播广告位Banner



Android无限循环轮播广告位Banner


现在一些app通常会在头部放一个广告位,底部放置一行小圆圈指示器,指示广告位当前的页码,轮播展示一些图片,这些图片来自于网络。这个广告位banner是典型的android ViewPager实现,但是如果自己实现这样的ViewPager,要解决一系列琐碎的问题,比如:

(1)这个广告位ViewPager要支持无限循环轮播,例如,有3张图片,A,B,C,当用户滑到最后C时候再滑就要滑到A,反之亦然。
(2)ViewPager要实现自动播放,比如每个若干秒如2秒,自动切换播放到下一张图片。
(3)通常这样的ViewPager下面会放一排指示器小圆圈,用以形象指示当前页码。
这样的Android广告位复用程度很高,通用长度也很高。Github上有一个开源项目:https://github.com/youth5201314/banner 
实现了上述的全部功能,并提供多种样式选择。使用也简单,比如写一个简单的例子,xml代码(片段):

[html]  view plain  copy
  1. <com.youth.banner.Banner  
  2.                        xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.                        android:id="@+id/banner"  
  4.                        android:layout_width="match_parent"  
  5.                        android:layout_height="140dp"  
  6.                        app:image_scale_type="center_crop"  
  7.                        app:default_image="@drawable/home"  
  8.                        app:indicator_drawable_selected="@drawable/selected_radius"  
  9.                        app:indicator_drawable_unselected="@drawable/unselected_radius"  
  10.                        app:indicator_height="8dp"  
  11.                        app:indicator_width="8dp"  
  12.                        app:indicator_margin="6dp"/>  

引用的selected_radius.xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="oval">  
  4.     <solid android:color="@color/color_e91e63" />  
  5. </shape>  

unselected_radius.xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="oval">  
  4.     <solid android:color="#80ffffff" />  
  5. </shape>  
颜色值#80ffffff是白色半透明。


上层Java代码:

[java]  view plain  copy
  1. Banner banner= (Banner) view.findViewById(R.id.banner);  
  2.        String url1="http://xxx.xxx.xxx.jpg";  
  3.        String url2="http://xxx.xxx.xxx.jpg";  
  4.        String url3="http://xxx.xxx.xxx.jpg";  
  5.        String[] images= new String[] {url1,url2,url3};  
  6.   
  7.        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);  
  8.        banner.setImages(images);  
  9.        banner.setDelayTime(2000);  


代码运行结果:


附录:
1,《Android实现ViewPager无限循环滚动回绕》链接:http://blog.csdn.net/zhangphil/article/details/51605244


GitHub上面有个非常好用的banner:https://github.com/youth5201314/banner


转自:http://blog.csdn.net/zhangphil

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值