一個簡單的材料設計應用程式介紹,具有酷炫的動畫和流暢的 API。
深受 Google 應用程式介紹的啟發。
Google Play 上提供了演示應用程式:
簡單的幻燈片 | 客製化幻燈片 | 淡入淡出效果 | 許可請求 |
---|---|---|---|
SimpleSlide.java | FragmentSlide.java | IntroActivity.java | SimpleSlide.java |
Material-intro可在jitpack.io上找到。
將其新增至您的根build.gradle
檔案(而不是您的模組build.gradle
檔案):
allprojects {
repositories {
maven { url ' https://jitpack.io ' }
}
}
將其新增至您的模組build.gradle
檔案:
dependencies {
implementation ' com.heinrichreimersoftware:material-intro:x.y.z '
}
活動必須擴展IntroActivity
並具有擴展@style/Theme.Intro
主題:
public class MainIntroActivity extends IntroActivity {
@ Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
// Add slides, edit configuration...
}
}
< manifest ...>
< application ...>
< activity
android : name = " .MainIntroActivity "
android : theme = " @style/Theme.Intro " />
</ application >
</ manifest >
(除非另有說明,以下所有方法呼叫都應在 Activity 的onCreate()
中進行。)
Material-intro具有流暢的樣式建立器,適用於簡單的文字/圖像幻燈片(如 Google 應用程式中所示)以及具有自訂Fragment
或佈局資源的幻燈片。
如果您認為缺少任何投影片類型,請隨時提交問題或拉取請求。
SimpleSlide
):標準投影片,包含標題、簡短說明和圖像,如 Google 的介紹。
addSlide ( new SimpleSlide . Builder ()
. title ( R . string . title_1 )
. description ( R . string . description_1 )
. image ( R . drawable . image_1 )
. background ( R . color . background_1 )
. backgroundDark ( R . color . background_dark_1 )
. scrollable ( false )
. permission ( Manifest . permission . CAMERA )
. build ());
FragmentSlide
):包含Fragment
或版面資源的自訂投影片。
addSlide ( new FragmentSlide . Builder ()
. background ( R . color . background_2 )
. backgroundDark ( R . color . background_dark_2 )
. fragment ( R . layout . fragment_2 , R . style . FragmentTheme )
. build ());
(將FragmentSlide
與Fragment
一起使用時,您應該擴展SlideFragment
以代理對介紹活動的導航呼叫。)
Slide
):基礎幻燈片。如果您想修改幻燈片中顯示的內容,這是正確的方法。
控制左側按鈕行為或隱藏/顯示它。
/* Show/hide button */
setButtonBackVisible ( true );
/* Use skip button behavior */
setButtonBackFunction ( BUTTON_BACK_FUNCTION_SKIP );
/* Use back button behavior */
setButtonBackFunction ( BUTTON_BACK_FUNCTION_BACK );
控制右鍵行為或隱藏/顯示它。
/* Show/hide button */
setButtonNextVisible ( true );
/* Use next and finish button behavior */
setButtonNextFunction ( BUTTON_NEXT_FUNCTION_NEXT_FINISH );
/* Use next button behavior */
setButtonNextFunction ( BUTTON_NEXT_FUNCTION_NEXT );
更改「號召性用語」按鈕的外觀:
/* Show/hide button */
setButtonCtaVisible ( getStartedEnabled );
/* Tint button text */
setButtonCtaTintMode ( BUTTON_CTA_TINT_MODE_TEXT );
/* Tint button background */
setButtonCtaTintMode ( BUTTON_CTA_TINT_MODE_BACKGROUND );
/* Set custom CTA label */
setButtonCtaLabel ( R . string . start )
/**/
setButtonCtaClickListener ( new View . OnClickListener () {
});
全螢幕顯示介紹活動。這會隱藏狀態列。
public class MainIntroActivity extends IntroActivity {
@ Override protected void onCreate ( Bundle savedInstanceState ){
setFullscreen ( true );
super . onCreate ( savedInstanceState );
...
}
}
確保在呼叫super.onCreate()
之前呼叫setFullscreen()
調整單一幻燈片滾動所需的時間。
setPageScrollDuration ( 500 );
(當滾動多個位置時,頁面滾動持續時間會動態調整,以反映 Material design 文件中的動態持續時間。計算滾動持續時間的確切公式是duration * (distance + sqrt(distance)) / 2
。)
阻止投影片導覽的方法有以下三種:
SlideFragment
中(使用FragmentSlide
),透過重寫canGoForward()
/ canGoBackward()
方法。SimpleSlide
,透過設定SimpleSlide.Builder#canGoForward(boolean)
/ SimpleSlide.Builder#canGoBackward(boolean)
。 (如果至少為SimpleSlide
設定了一項權限,則導覽將自動阻止,直到授予所有權限為止。)NavigationPolicy
從IntroActivity
: setNavigationPolicy ( new NavigationPolicy () {
@ Override public boolean canGoForward ( int position ) {
return true ;
}
@ Override public boolean canGoBackward ( int position ) {
return false ;
}
});
使用下列方法之一手動瀏覽簡介,例如從偵聽器回呼中。他們每個人都會尊重被阻止的導航設定。
/* Scroll to any position */
goToSlide ( 5 );
/* Scroll to the next slide in the intro */
nextSlide ();
/* Scroll to the previous slide in the intro */
previousSlide ();
/* Scroll to the last slide of the intro */
goToLastSlide ();
/* Scroll to the first slide of the intro */
goToFirstSlide ();
自動捲動介紹,直到使用者與介紹互動。
/* Start an auto slideshow with 2500ms delay between scrolls and infinite repeats */
autoplay ( 2500 , INFINITE );
/* Start an auto slideshow with default configuration */
autoplay ();
/* Cancel the slideshow */
cancelAutoplay ()
當使用者嘗試導覽至投影片時,如果以上任何一個傳回false
,則會觸發OnNavigationBlockedListener
回調,您可以使用它來顯示訊息。此外,您可以添加普通的ViewPager.OnPageChangeListener
來監聽頁面滾動:
addOnNavigationBlockedListener ( new OnNavigationBlockedListener () {
@ Override public void onNavigationBlocked ( int position , int direction ) { ... }
});
addOnPageChangeListener ( new ViewPager . OnPageChangeListener (){
@ Override public void onPageScrolled ( int position , float positionOffset , int positionOffsetPixels ) { ... }
@ Override public void onPageSelected ( int position ) { ... }
@ Override public void onPageScrollStateChanged ( int state ) { ... }
});
您可以檢查使用者是否取消了介紹(透過按返回)或完成了介紹(包括所有投影片)。只需使用startActivityForResult(intent, REQUEST_CODE_INTRO);
呼叫活動即可然後聽聽結果:
@ Override
protected void onActivityResult ( int requestCode , int resultCode , Intent data ) {
super . onActivityResult ( requestCode , resultCode , data );
if ( requestCode == REQUEST_CODE_INTRO ) {
if ( resultCode == RESULT_OK ) {
// Finished the intro
} else {
// Cancelled the intro. You can then e.g. finish this activity too.
finish ();
}
}
}
透過使用ParallaxFrameLayout.java
、 ParallaxLinearLayout.java
或ParallaxRelativeLayout.java
並為其直接子級定義layout_parallaxFactor
,您可以輕鬆地為任何幻燈片實現漂亮的視差效果。係數越高表示視差效果越強, 0
表示完全沒有視差效果。
< com .heinrichreimersoftware.materialintro.view.parallax.ParallaxLinearLayout
android : layout_width = " match_parent "
android : layout_height = " match_parent "
... >
< TextView
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
app : layout_parallaxFactor = " 0 "
... />
< ImageView
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
app : layout_parallaxFactor = " 1.25 "
... />
</ com .heinrichreimersoftware.materialintro.view.parallax.ParallaxLinearLayout>
請參閱“Canteen”演示以取得佈局範例。
如果您只想在第一次應用程式啟動時顯示介紹,您可以使用onActivityResult()
在使用者完成介紹時儲存共用首選項。
請參閱演示應用程式以取得範例啟動畫面實作:
請參閱發布部分以取得變更日誌。
Material-intro使用以下開源檔案:
MIT License
Copyright (c) 2017 Jan Heinrich Reimer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.