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代碼(片段):

 <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);


代碼運行結果:


附錄:
1,《Android實現ViewPager無限循環滾動回繞》鏈接:http://blog.csdn.net/zhangphil/article/details/51605244


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM