老規矩,先看效果圖:
我們今天的主角_SlidingIntroScreen
An Android library designed to simplify the creation of introduction screens.
簡單翻譯一下:就是簡化Android App啓動的介紹頁_引導頁的創建
官網效果圖:
IntroActivity是此庫的主要類,因爲它會協調並顯示所有其他組件。該活動協調兩個主要組件:一個標準的Android ViewPager和一個自定義的導航欄。視圖分頁器包含幾個頁面,這些頁面依次顯示介紹屏幕的內容。導航欄顯示用戶的簡介進度,並提供三個用於導航的按鈕。向左和向右按鈕顯示在除最後一頁以外的所有頁面上,而最後一個按鈕僅在最後一頁顯示。
IntroActivity是一個抽象類,因此要使用它,您必須創建一個子類並實現一些抽象方法。通過實現
generatePages()
,您可以定義簡介屏幕的內容,並且generateFinalButtonBehaviour()
可以通過實現來定義單擊最後一個按鈕時發生的情況。IntroActivity類的其他方法提供了更多的自定義選項,例如:
- 以編程方式更改頁面。
- 鎖定頁面以觸摸事件和/或編程命令(例如按鈕按下)。
- 隱藏/顯示導航欄的水平分隔線。
- 更改頁面轉換器(在滾動時應用效果)。
- 更改背景管理器(負責在滾動時更新背景)。
- 添加/刪除頁面更改偵聽器。
- 獲取頁面參考。
- 切換按鈕的可見性。
其他組件
該庫的其他值得注意的組件是:
- IntroButton —— 定義按鈕的外觀和行爲
- SelectionIndicator —— 導航欄以DotIndicator的形式直觀地顯示用戶的進度
- BackgroundManager —— 可定義各個頁面的背景
- AnimatorFactory —— 按鈕顯示隱藏,可使按鈕平滑地淡入和淡出,而不是在可見和不可見之間進行劇烈的瞬時過渡
使用該庫,需添加依賴
repositories {
jcenter()
}
dependencies {
implementation 'com.matthew-tamlin:sliding-intro-screen:3.2.0'
}
創建Module,學習此庫使用。
創建引導頁GuideActivity.java,需要繼承IntroActivity.java,並實現generatePages()和generateFinalButtonBehaviour()。
註釋已經寫好。
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import com.matthewtamlin.sliding_intro_screen_library.background.BackgroundManager;
import com.matthewtamlin.sliding_intro_screen_library.background.ColorBlender;
import com.matthewtamlin.sliding_intro_screen_library.buttons.IntroButton;
import com.matthewtamlin.sliding_intro_screen_library.core.IntroActivity;
import com.matthewtamlin.sliding_intro_screen_library.transformers.MultiViewParallaxTransformer;
import com.yunyang.slidingintroscreendemo.fragment.NumberFragment;
import java.util.ArrayList;
import java.util.Collection;
public class GuideActivity extends IntroActivity {
/**
* 用於混合背景的顏色:藍色、粉色、紫色。
*/
private static final int[] BACKGROUND_COLORS = {0xff304FFE, 0xffcc0066, 0xff9900ff};
public static final String DISPLAY_ONCE_PREFS = "display_only_once_spfile";
public static final String DISPLAY_ONCE_KEY = "display_only_once_spkey";
@Override
protected void onCreate(Bundle savedInstanceState) {
// 樣式_標題欄
setTheme(R.style.NoActionBar);
super.onCreate(savedInstanceState);
// 引導頁_介紹頁_只顯示一次_當進入到App主頁面時
if (introductionCompletedPreviously()) {
final Intent nextActivity = new Intent(this, MainActivity.class);
startActivity(nextActivity);
}
// 隱藏狀態欄
hideStatusBar();
configureTransformer();
configureBackground();
}
/**
* 此活動顯示的頁面
*/
@Override
protected Collection<Fragment> generatePages(Bundle savedInstanceState) {
final ArrayList<Fragment> pages = new ArrayList<>();
for (int i = 0; i < BACKGROUND_COLORS.length; i++) {
final NumberFragment fragment = new NumberFragment();
fragment.setNumber(i + 1);
pages.add(fragment);
}
return pages;
}
/**
* 按鈕行爲
*/
@Override
@SuppressLint("CommitPrefEdits")
protected IntroButton.Behaviour generateFinalButtonBehaviour() {
// 引導頁_介紹頁_只顯示一次_當進入到App主頁面時_標識
final SharedPreferences sp = getSharedPreferences(DISPLAY_ONCE_PREFS, MODE_PRIVATE);
final SharedPreferences.Editor pendingEdits = sp.edit().putBoolean(DISPLAY_ONCE_KEY, true);
// 定義跳轉意圖,並創建用於最終按鈕的行爲
final Intent nextActivity = new Intent(this, MainActivity.class);
return new IntroButton.ProgressToNextActivity(nextActivity, pendingEdits);
}
/**
* 檢查跳轉意圖標識
*/
private boolean introductionCompletedPreviously() {
final SharedPreferences sp = getSharedPreferences(DISPLAY_ONCE_PREFS, MODE_PRIVATE);
return sp.getBoolean(DISPLAY_ONCE_KEY, false);
}
/**
* 將此IntroActivity設置爲使用multiviewparallel transformer頁面轉換器。
*/
private void configureTransformer() {
final MultiViewParallaxTransformer transformer = new MultiViewParallaxTransformer();
transformer.withParallaxView(R.id.number_fragment_text_holder, 1.2f);
setPageTransformer(false, transformer);
}
/**
* 將此IntroActivity設置爲使用ColorBlender後臺管理器。
*/
private void configureBackground() {
final BackgroundManager backgroundManager = new ColorBlender(BACKGROUND_COLORS);
setBackgroundManager(backgroundManager);
}
}
ViewPager + Fragment —— NumberFragment.java
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;
import com.yunyang.slidingintroscreendemo.R;
/**
* Created by YunYang.
* Date: 2019/9/28
* Time: 16:24
* Des: ViewPager + Fragment
*/
public class NumberFragment extends Fragment {
private AppCompatTextView textView;
private int number = 0;
public NumberFragment() {
// Required empty public constructor
}
public void setNumber(final int number) {
this.number = number;
if (textView != null) {
textView.setText(Integer.toString(number));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View root = inflater.inflate(R.layout.number_fragment, container, false);
textView = (AppCompatTextView) root.findViewById(R.id.number_fragment_text_holder);
textView.setText(null);
Log.d("test", "" + number);
textView.setText(Integer.toString(number));
return root;
}
}
NumberFragment_碎片的佈局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/number_fragment_text_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@android:color/white"
android:textSize="80sp" />
</FrameLayout>
引導頁跳轉的主頁面MainActivity.java
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
@SuppressWarnings("ConstantConditions")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 當按下時,允許介紹頁_導航頁再次顯示
findViewById(R.id.clear_shared_prefs_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allowIntroductionToShowAgain();
}
});
}
/**
* 清除標誌,該標誌防止介紹顯示兩次。
*/
private void allowIntroductionToShowAgain() {
final SharedPreferences sp = getSharedPreferences(GuideActivity.DISPLAY_ONCE_PREFS,
MODE_PRIVATE);
sp.edit().putBoolean(GuideActivity.DISPLAY_ONCE_KEY, false).apply();
Toast.makeText(this, "清除成功", Toast.LENGTH_SHORT).show();
}
}
主頁面的佈局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:textSize="28sp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:text="@string/ac_main_launcher_success" />
<androidx.appcompat.widget.AppCompatButton
android:layout_gravity="center_horizontal"
android:id="@+id/clear_shared_prefs_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ac_main_clear_sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
引導頁/介紹頁快速創建完成。
省略指示器創建,ViewPager創建,大大節約開發時間,讓開發者只關注顯示活動的頁面。