Android ViewPager實(shí)現(xiàn)無限循環(huán)輪播廣告位Banner效果
現(xiàn)在一些app通常會在頭部放一個(gè)廣告位,底部放置一行小圓圈指示器,指示廣告位當(dāng)前的頁碼,輪播展示一些圖片,這些圖片來自于網(wǎng)絡(luò)。這個(gè)廣告位banner是典型的android ViewPager實(shí)現(xiàn),但是如果自己實(shí)現(xiàn)這樣的ViewPager,要解決一系列瑣碎的問題,比如:
(1)這個(gè)廣告位ViewPager要支持無限循環(huán)輪播,例如,有3張圖片,A,B,C,當(dāng)用戶滑到最后C時(shí)候再滑就要滑到A,反之亦然。
(2)ViewPager要實(shí)現(xiàn)自動播放,比如每個(gè)若干秒如2秒,自動切換播放到下一張圖片。
(3)通常這樣的ViewPager下面會放一排指示器小圓圈,用以形象指示當(dāng)前頁碼。
這樣的Android廣告位復(fù)用程度很高,通用長度也很高。Github上有一個(gè)開源項(xiàng)目:https://github.com/youth5201314/banner
實(shí)現(xiàn)了上述的全部功能,并提供多種樣式選擇。使用也簡單,比如寫一個(gè)簡單的例子,xml代碼(片段):
<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="140dp"
app:image_scale_type="center_crop"
app:default_image="@drawable/home"
app:indicator_drawable_selected="@drawable/selected_radius"
app:indicator_drawable_unselected="@drawable/unselected_radius"
app:indicator_height="8dp"
app:indicator_width="8dp"
app:indicator_margin="6dp"/>
引用的selected_radius.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/color_e91e63" /> </shape>
unselected_radius.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#80ffffff" /> </shape>
顏色值#80ffffff是白色半透明。
上層Java代碼:
Banner banner= (Banner) view.findViewById(R.id.banner);
String url1="http://xxx.xxx.xxx.jpg";
String url2="http://xxx.xxx.xxx.jpg";
String url3="http://xxx.xxx.xxx.jpg";
String[] images= new String[] {url1,url2,url3};
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
banner.setImages(images);
banner.setDelayTime(2000);
代碼運(yùn)行結(jié)果:

附: 《Android實(shí)現(xiàn)ViewPager無限循環(huán)滾動回繞》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用ViewPager實(shí)現(xiàn)滾動廣告
- Android 知乎廣告效果實(shí)現(xiàn)代碼
- Android 實(shí)現(xiàn)廣告歡迎界面(倒計(jì)時(shí))
- Android開發(fā)實(shí)現(xiàn)廣告無限循環(huán)功能示例
- Android滾動條廣告實(shí)現(xiàn)代碼示例
- Android 應(yīng)用啟動歡迎界面廣告的實(shí)現(xiàn)實(shí)例
- Android仿淘寶頭條向上滾動廣告條ViewFlipper
- Kotlin FrameLayout與ViewPager2控件實(shí)現(xiàn)滾動廣告欄方法
相關(guān)文章
Android ExpandableListView展開列表控件使用實(shí)例
這篇文章主要介紹了Android ExpandableListView展開列表控件使用實(shí)例,本文實(shí)現(xiàn)了一個(gè)類似手機(jī)QQ好友列表的界面效果,需要的朋友可以參考下2014-07-07
Android開發(fā)Intent跳轉(zhuǎn)傳遞list集合實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Android開發(fā)Intent跳轉(zhuǎn)傳遞list集合實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Android 第三方應(yīng)用接入微信平臺研究情況分享(二)
微信平臺開放后倒是挺火的,許多第三方應(yīng)用都想試下,這里把我的整個(gè)研究情況給出來,希望可以共同學(xué)習(xí),感興趣的朋友可以了解下2013-01-01
Android基礎(chǔ)開發(fā)小案例之短信發(fā)送器
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)開發(fā)小案例之短信發(fā)送器的具體實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-05-05

