ViewPager打造輪播圖Banner/引導(dǎo)頁Guide
前言
去年7月時,在Github發(fā)布了一個開源的Banner庫,雖然Star不多,但還是有少部分人使用。
Banner效果:

Github鏈接地址:https://github.com/Allure0/LMBanners
昨天,有使用此庫的同學(xué)提出需求,想在引導(dǎo)頁的時候用這個庫并且最后一頁有進(jìn)入按鈕如何實(shí)現(xiàn),為滿足他的需求,也方便更多開發(fā)者是快速實(shí)現(xiàn)。進(jìn)行了簡單的擴(kuò)展支持Guide模式的使用。
Guide效果圖:

OK,效果如圖所以,咱們此庫滿足了既可在Banner上使用也可以快速在第一次安裝應(yīng)用的時候引導(dǎo)頁使用。
Banner與Guide有什么區(qū)別?
引導(dǎo)頁的最后一頁有按鈕,Banners沒有
引導(dǎo)頁的底部原點(diǎn)距離較大,Banners可以幾乎固定
Banner基礎(chǔ)上擴(kuò)展實(shí)現(xiàn)第一步:添加按鈕
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.allure.lbanners.viewpager.HorizonVerticalViewPager android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="match_parent" android:unselectedAlpha="1"></com.allure.lbanners.viewpager.HorizonVerticalViewPager> <LinearLayout android:id="@+id/indicatorLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center" android:orientation="horizontal" android:padding="20dp"></LinearLayout> <Button android:id="@+id/btn_start" android:layout_width="104dp" android:layout_height="29dp" android:layout_above="@id/indicatorLayout" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" android:background="@drawable/banners_btn_shape" android:gravity="center" android:text="立即開啟" android:textColor="#f24814" android:textSize="12sp" /> </RelativeLayout>
相比于原來咱們新增了按鈕,這時候咱們按照這個布局運(yùn)行在每一個界面都包含了Button,而引導(dǎo)頁模式只有在最后一頁需要展示按鈕。
Banner基礎(chǔ)上擴(kuò)展實(shí)現(xiàn)第二步:按鈕的控制與模式支持
模式的支持
attrs.xml下新增自定義屬性
<!--是否為引導(dǎo)頁--> <attr name="isGuide" format="boolean"></attr> <attr name="indicatorBottomPadding" format="integer"></attr>
按鈕的控制
在ViewPager中咱們控制按鈕可以在ViewPager.OnPageChangeListener的接口方法中onPageScrolled進(jìn)行控制
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.d("LMBanners", "onPageScrolled was invoke()");
int realPosition = position %= showCount;
if(!isGuide){
btnStart.setVisibility(View.GONE);
return;
}
if (realPosition == getItemCount() - 2) {
if (positionOffset > 0.5f) {
if (btnStart != null) {
ViewHelper.setAlpha(btnStart, positionOffset);
btnStart.setVisibility(View.VISIBLE);
}
} else {
btnStart.setVisibility(View.GONE);
}
} else if (realPosition == getItemCount() - 1) {
if (positionOffset < 0.5f) {
btnStart.setVisibility(View.VISIBLE);
ViewHelper.setAlpha(btnStart, 1.0f - positionOffset);
} else {
btnStart.setVisibility(View.GONE);
}
} else {
btnStart.setVisibility(View.GONE);
}
}
以上代碼中isGuide咱們判斷是哪一種模式,如果不是Guide引導(dǎo)頁模式,直接設(shè)置按鈕不顯示,并且跳出程序。
如果是Guide引導(dǎo)頁模式,咱們針對倒數(shù)第二頁與最后的控制的滑動距離來判斷了按鈕的顯示。
核心點(diǎn):
positionOffset:表示滑動的距離
向左滑動1—->>0.5>>0
向右滑動0—->>0.5>>1
根據(jù)滑動的距離進(jìn)行按鈕的一個漸變顯示。
Banner基礎(chǔ)上擴(kuò)展實(shí)現(xiàn)第三步:按鈕的點(diǎn)擊回調(diào)
點(diǎn)擊按鈕需要執(zhí)行開發(fā)者的自身邏輯跳轉(zhuǎn),咱們用接口回調(diào)完成
public interface onStartListener {
void startOpen();
}
Banner基礎(chǔ)上擴(kuò)展實(shí)現(xiàn)第四步:Guide模式使用方式
對比banner只需要增加以下代碼,如果需要其他屬性可以自己設(shè)置(如,不自動滾動,不設(shè)置循環(huán)播放等等)
//設(shè)置為全屏
mLBanners.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
//true為Guide模式,false為banner模式
mLBanners.isGuide(true);
mLBanners.setOnStartListener(new LMBanners.onStartListener() {
@Override
public void startOpen() {
//回調(diào)跳轉(zhuǎn)的邏輯
Toast.makeText(MainActivity.this,"我要進(jìn)入主界面",1).show();
}
});
到此,咱們的banner圖升級為Guide模式的圖庫就完成。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用ViewPager實(shí)現(xiàn)啟動引導(dǎo)頁
- Android自定義引導(dǎo)玩轉(zhuǎn)ViewPager的方法詳解
- Android開發(fā)實(shí)戰(zhàn)之漂亮的ViewPager引導(dǎo)頁
- Android開發(fā)實(shí)現(xiàn)的ViewPager引導(dǎo)頁功能(動態(tài)加載指示器)詳解
- ViewPager實(shí)現(xiàn)漂亮的引導(dǎo)頁
- Android控件ViewPager實(shí)現(xiàn)帶有動畫的引導(dǎo)頁
- ViewPager實(shí)現(xiàn)帶引導(dǎo)小圓點(diǎn)與自動跳轉(zhuǎn)的引導(dǎo)界面
- Android利用ViewPager實(shí)現(xiàn)用戶引導(dǎo)界面效果的方法
- Android使用ViewPager完成app引導(dǎo)頁
- ViewPager實(shí)現(xiàn)輪播圖引導(dǎo)頁
相關(guān)文章
Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
最近在做一個 Android 項(xiàng)目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來與大家分享2013-01-01
Android基于OkHttp實(shí)現(xiàn)下載和上傳圖片
這篇文章主要為大家詳細(xì)介紹了Android基于OkHttp實(shí)現(xiàn)下載和上傳圖片功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
android游戲載入的activity跳轉(zhuǎn)到游戲主菜單的activity具體實(shí)現(xiàn)
停止2s后由游戲載入頁面再跳轉(zhuǎn)到游戲菜單頁面,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-06-06
Android來電監(jiān)聽和去電監(jiān)聽實(shí)現(xiàn)代碼
本文是關(guān)于來點(diǎn)監(jiān)聽和去電監(jiān)聽展開問題,通過實(shí)例代碼講解,對android來電監(jiān)聽和去電監(jiān)聽的相關(guān)知識感興趣的朋友一起看看吧2017-06-06
android初學(xué)者必須掌握的Activity狀態(tài)的四大知識點(diǎn)(必讀)
本篇文章主要介紹了android activity的四種狀態(tài),詳細(xì)的介紹了四種狀態(tài),包括Running狀態(tài)、Paused狀態(tài)、Stopped狀態(tài)、Killed狀態(tài),有興趣的可以了解一下。2016-11-11
揭秘在ListView等AdapterView上動態(tài)添加刪除項(xiàng)的陷阱
今天遇到這么個需求,需要在運(yùn)行時動態(tài)添加ListView的item,看起來很簡單,實(shí)際操作過程中卻遇到了麻煩,下面揭秘在ListView等AdapterView上動態(tài)添加刪除項(xiàng)的陷阱2016-04-04
Android SQLite3多線程操作問題研究總結(jié)
這篇文章主要介紹了Android SQLite3多線程操作問題研究總結(jié),本文總結(jié)了SQLite3是否支持多線程、SQLiteDatabase的同步鎖、多線程讀數(shù)據(jù)庫等問題,需要的朋友可以參考下2015-03-03

