2

我正在尝试将自定义视图显示为真正的闪屏,当颤动初始化其第一帧时会弹出。我正在关注文档,但正如它所说,我们需要创建一个颤振片段并覆盖 provideSplashScreen 方法。我这样做了,但我不知道如何将该片段添加到活动或如何在清单中引用该片段。

我的清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.neer.neer">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="neer">
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:windowSoftInputMode="adjustResize">
            <meta-data
                android:name="io.flutter.embedding.android.FlutterFragment"
                android:value=".MyFlutterFragment" />
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme" />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
                android:name="io.flutter.embedding.android.SplashScreenProvider"
                android:resource=".MyFlutterFragment" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

我的 MainActivity.java

package com.neer.neer;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.SplashScreen;

class MainActivity extends FlutterActivity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }

}

MyFlutterFragment.java文件。我需要这个片段显示为闪屏

package com.neer.neer;

import android.graphics.drawable.Drawable;

import io.flutter.embedding.android.DrawableSplashScreen;
import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.android.SplashScreen;

public class MyFlutterFragment extends FlutterFragment {
    @Override
    public SplashScreen provideSplashScreen() {
        // Load the splash Drawable.
//        Drawable splash = getResources().getDrawable(R.drawable.my_splash);

        // Construct a DrawableSplashScreen with the loaded splash
        // Drawable and return it.
//        return new DrawableSplashScreen(splash);
        return new MySplashScreen();
    }
}

MySplashScreen.java 文件。

package com.neer.neer;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.flutter.embedding.android.SplashScreen;

public class MySplashScreen implements SplashScreen {
    @Nullable
    @Override
    public View createSplashView(@NonNull Context context, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(context).inflate(R.layout.splash_screen, null, false);
    }

    @Override
    public void transitionToFlutter(@NonNull Runnable onTransitionComplete) {
        onTransitionComplete.run();
    }
}

spash_screen.xml文件(启动画面的布局)

<?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"
    android:background="@android:color/background_dark"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="N E E R"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:textSize="@android:dimen/notification_large_icon_width"
        android:textStyle="bold"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:text="Powered by ICCW"
        android:textColor="@android:color/white"
        android:textSize="26sp"
        android:layout_marginBottom="20dp"
        />

</FrameLayout>
4

1 回答 1

4

在 Flutter 文档中没有提到你 Flutter Activity 也有可以被覆盖的方法 provideSplashScreen。因此,我查看了 FlutterActivity 提供的覆盖列表,并在那里看到了方法 provideSplashScreen。它奏效了。

对于发现自己处于相同情况的其他人

只需覆盖扩展 FlutterActivity 的 MainActivity 中的 provideSplashScreen 方法

public class MainActivity extends FlutterActivity {
    @Nullable
    @Override
    public SplashScreen provideSplashScreen() {
        return new MySplashScreen(); //Your Custom Splash Screen
    }
}

class MySplashScreen implements SplashScreen {
    @Nullable
    @Override
    public View createSplashView(@NonNull Context context, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(context).inflate(R.layout.splash_screen, null, false);
    }

    @Override
    public void transitionToFlutter(@NonNull Runnable onTransitionComplete) {
        onTransitionComplete.run();
    }
}
于 2020-07-27T00:57:37.750 回答