AppIntroの使用
- AppIntro の依存関係 (gradle の依存関係に追加):
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
// AndroidX Capable version
implementation 'com.github.AppIntro:AppIntro:6.1.0'
// *** OR ***
// Latest version compatible with the old Support Library
implementation 'com.github.AppIntro:AppIntro:4.2.3'
}
- Activity を使用して AppIntro を継承する (または別のスライド効果を表示する AppIntro2 を継承する) クラスを作成し、onCreate()、onSkipPressed()、onDonePressed() の 3 つのメソッドを書き換えます。
- onCreate で addSlide() メソッドを使用して、ようこそインターフェイスの画像を表示します
- マニフェストリストファイルで宣言する
- 次のように:
AppIntroActivity.java
package com.it.appintro;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import com.github.appintro.AppIntro;
import com.github.appintro.AppIntroFragment;
import org.jetbrains.annotations.Nullable;
public class AppIntroActivity extends AppIntro {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* AppIntroFragment.newInstance()有多个重载方法,这里使用的是AppIntroFragment.newInstance(CharSequence title,CharSequence
* description,int imageDrawable)
*
* */
addSlide(AppIntroFragment.newInstance("Welcome!",
"This is a demo example in java of AppIntro library, with a custom background on each slide!",
R.drawable.img1));
addSlide(AppIntroFragment.newInstance(
"Clean App Intros",
"This library offers developers the ability to add clean app intros at the start of their apps.",
R.drawable.img2
));
}
@Override
protected void onSkipPressed(@Nullable Fragment currentFragment) {
super.onSkipPressed(currentFragment);
//finish直接返回上一层
finish();
}
@Override
protected void onDonePressed(@Nullable Fragment currentFragment) {
super.onDonePressed(currentFragment);
//finish直接返回上一层
finish();
}
}
manifests清单文件
<activity android:name=".AppIntroActivity"
android:label="AppIntroActivity"/>
ランニング効果は以下の通りです。
合計 2 つのスライドがあり、左下隅と右下隅にジャンプ ボタンと戻るボタンがあります (それぞれ onSkipPress メソッドと onDonePress メソッドに対応) が、背景色のせいで見えません (背景色とボタンの両方)カラーは設定可能) ですが、クリックすると事前定義されたfinish() メソッドが実行されます。
注意点:
- onCreate メソッドでは setContentView() メソッドを使用できません。AppIntro のスーパークラスが自動的に処理します。
- onCreate() メソッドには複数のオーバーロードされたメソッドがあります。使用する必要があるのは、Bundle パラメータを 1 つだけ持つ onCreate メソッドです。
- アプリケーションが起動するたびに MyCustomAppIntro を開始する必要がない限り、MyCustomAppIntro を最初のアクティビティとして宣言することはお勧めできません。理想的には、AppIntro アクティビティはユーザーに 1 回だけ表示し、完了したら非表示にする必要があります (フラグは SharedPreferences で使用できます)。
AppIntroFragment.newInstance () オーバーロード メソッドの学習
addSlide のメソッド呼び出しでは Fragment パラメーターを渡す必要があることがわかっています。通常、Fragment 型のオブジェクトを取得するには AppIntroFragment.newInstance() を使用します。明らかに、AppIntroFragment.newInstance() はシングルトン パターンを使用しており、多くのオーバーロードされていますメソッドですが、これには特定のルールが含まれています。パラメータが多いメソッドとパラメータが少ないメソッドが比較されます。パラメータが多いメソッドには、パラメータが少ないメソッドのすべてのパラメータが含まれるため、パラメータが最も多い AppIntroFragment のみを学習する必要があります。 .newInstance() メソッドは、オーバーロードされたメソッドのほとんどを習得でき、最も多くのパラメータを持つフォームは次のとおりです。
addSlide(AppIntroFragment.newInstance(
title = "The title of your slide",
description = "A description that will be shown on the bottom",
imageDrawable = R.drawable.the_central_icon,
backgroundDrawable = R.drawable.the_background_image,
titleColor = Color.YELLOW,
descriptionColor = Color.RED,
backgroundColor = Color.BLUE,
titleTypefaceFontRes = R.font.opensans_regular,
descriptionTypefaceFontRes = R.font.opensans_regular,
))
AppIntroActivity.javaファイルを変更しましょう。
package com.it.appintro;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import com.github.appintro.AppIntro;
import com.github.appintro.AppIntroFragment;
import org.jetbrains.annotations.Nullable;
public class AppIntroActivity extends AppIntro {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* AppIntroFragment.newInstance()有多个重载方法,这里使用的是AppIntroFragment.newInstance(CharSequence title,CharSequence
* description,int imageDrawable)
*
* */
AppIntroFragment.newInstance();
addSlide(AppIntroFragment.newInstance("Welcome!",
"This is a demo example in java of AppIntro library, with a custom background on each slide!",
R.drawable.img1, Color.GRAY,Color.RED,Color.BLACK)); //最后两个参数分别是标题的字体和讲述的字体,这里不再赘述
addSlide(AppIntroFragment.newInstance(
"Clean App Intros",
"This library offers developers the ability to add clean app intros at the start of their apps.",
R.drawable.img2,Color.GRAY,Color.RED,Color.BLACK
));
}
@Override
protected void onSkipPressed(@Nullable Fragment currentFragment) {
super.onSkipPressed(currentFragment);
//finish直接返回上一层
finish();
}
@Override
protected void onDonePressed(@Nullable Fragment currentFragment) {
super.onDonePressed(currentFragment);
//finish直接返回上一层
finish();
}
}
ランニング効果は以下の通りです。
このうち、AppIntroFragment.newInstance() には、カスタム レイアウト ファイルを通じてカスタム スライドショーを表示する特別なオーバーロード メソッドもあります。コードは次のとおりです。
カスタムアクティビティ.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:background="@drawable/img1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:gravity="center"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:text="这是自定义的"
/>
</LinearLayout>
//添加自定义的幻灯片
addSlide(AppIntroCustomLayoutFragment.newInstance(R.layout.custom_activity));
スライダーからスライドを追加
チェーン プログラミングの概念を使用せずに、手動でスライダーを設定してスライドを設定することもできます。
//通过SliderPage来添加幻灯片
SliderPage sliderPage = new SliderPage();
sliderPage.setTitle("这是SliderPage");
sliderPage.setDescription("这是自定义的SliderPage");
sliderPage.setImageDrawable(R.drawable.img2);
sliderPage.setBackgroundColor(Color.GRAY);
sliderPage.setTitleColor(Color.BLUE);
sliderPage.setDescriptionColor(Color.RED);
addSlide(AppIntroFragment.newInstance(sliderPage));
AppIntro のさまざまな構成
AppIntro を使い始めた後、フェードアウトやトランジションなど、好みのスタイルを見つけるためにさまざまな設定を使用する必要があります。具体的な設定については、公式ドキュメントを参照してください。公式ドキュメントのアドレスは次のとおりです。
https://github.com/AppIntro/AppIntro
最後に、公式ドキュメントに AppIntro の例を添付します (ドキュメント内の特定のメモは中国語であり、AppIntro のさまざまな構成について学ぶことができます)。
package com.github.appintro.example.ui.java;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.github.appintro.AppIntro;
import com.github.appintro.AppIntroFragment;
import com.github.appintro.AppIntroPageTransformerType;
import com.github.appintro.example.R;
public class JavaIntro extends AppIntro {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addSlide(AppIntroFragment.newInstance("Welcome!",
"This is a demo example in java of AppIntro library, with a custom background on each slide!",
R.drawable.ic_slide1));
addSlide(AppIntroFragment.newInstance(
"Clean App Intros",
"This library offers developers the ability to add clean app intros at the start of their apps.",
R.drawable.ic_slide2
));
addSlide(AppIntroFragment.newInstance(
"Simple, yet Customizable",
"The library offers a lot of customization, while keeping it simple for those that like simple.",
R.drawable.ic_slide3
));
addSlide(AppIntroFragment.newInstance(
"Explore",
"Feel free to explore the rest of the library demo!",
R.drawable.ic_slide4
));
// 淡出过渡
setTransformer(AppIntroPageTransformerType.Fade.INSTANCE);
// 显示/隐藏状态栏
showStatusBar(true);
//加快或减慢滚动速度
setScrollDurationFactor(2);
//在两个幻灯片之间启用颜色“淡出”动画(确保幻灯片实现)
setColorTransitionsEnabled(true);
//防止后退按钮退出幻灯片
setSystemBackButtonLocked(true);
//激活向导模式(一些美学上的改变)
setWizardMode(true);
// 显示/隐藏跳过按钮
setSkipButtonEnabled(true);
//启用沉浸模式(无状态和导航条)
setImmersiveMode();
// 启用/禁用页面指示符
setIndicatorEnabled(true);
//展示/隐藏所有按钮
setButtonsEnabled(true);
}
@Override
protected void onSkipPressed(Fragment currentFragment) {
super.onSkipPressed(currentFragment);
finish();
}
@Override
protected void onDonePressed(Fragment currentFragment) {
super.onDonePressed(currentFragment);
finish();
}
}