一个简单的材料设计应用程序介绍,具有炫酷的动画和流畅的 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.