简体   繁体   English

如何在颤振中创建自定义动画启动屏幕

[英]How to create a custom animated launch screen in flutter

I want to create a launch screen with minimal animations(like rotating an image).我想用最少的动画(如旋转图像)创建一个启动屏幕。 From the documentation found here I thought it was possible to do so, using the method mentioned under the 'Creating a custom SplashScreen' section.此处找到的文档中我认为可以使用“创建自定义 SplashScreen”部分中提到的方法来这样做。 But I have no idea where to start.但我不知道从哪里开始。 I first created a flutter java project using我首先使用创建了一个颤振 java 项目

 flutter create -a java custom_splash

Then I tried to copy paste the code given in the documentation link inside the MainActivity.java file and run the app but the app simply failed to build.然后我尝试复制粘贴MainActivity.java文件中文档链接中给出的代码并运行该应用程序,但该应用程序根本无法构建。 I also tried to use the rotate drawable inside launch_background.xml although it did rotate the image to a particular angle, it was static and not animated.我还尝试在launch_background.xml使用旋转可绘制launch_background.xml尽管它确实将图像旋转到特定角度,但它是静态的而不是动画的。

Note: I have not built any native android apps and java is new to me too注意:我没有构建任何原生 android 应用程序,java 对我来说也是新的

EDIT 1: I guess I must display it as a view instead of a drawable.编辑 1:我想我必须将它显示为视图而不是可绘制对象。 So I tried this.所以我尝试了这个。 Created a file called SplashScreenWithTransition.java next to MainActivity.javaMainActivity.java旁边创建了一个名为SplashScreenWithTransition.java的文件

package com.example.native_splash;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.native_splash.mySplashView;

import io.flutter.embedding.android.SplashScreen;

public class SplashScreenWithTransition implements SplashScreen {
    @Override
    @Nullable
    public View createSplashView(
            @NonNull Context context,
            @Nullable Bundle savedInstanceState
    ) {
        return new mySplashView(context);
    }

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

and another file called mySplashView.java和另一个名为 mySplashView.java 的文件

package com.example.native_splash;
import android.content.Context;

public class mySplashView extends android.view.View {

    public mySplashView(Context context) {
        super(context);
    }
}

and a view.xml inside drawable folder和可绘制文件夹中的view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:orientation="vertical">

    <rotate
        android:fromDegrees="0"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:drawable="@mipmap/ic_launcher"/>
    
</LinearLayout>

Modified the styles.xml as follows修改styles.xml如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <!-- Show a splash screen on the activity. Automatically removed when
             Flutter draws its first frame -->
        <item name="android:windowBackground">@drawable/launch_background</item>
        <item name="android:windowFullscreen">true</item>
    </style>
    <style name="ViewTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@drawable/view</item>
    </style>
</resources>

Finally modified the AndroidManifest.xml as follows最后修改AndroidManifest.xml如下

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.native_splash">
    <!-- 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:label="native_splash"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <meta-data
            android:name="com.example.native_splash.SplashScreenWithTransition"
            android:resource="@style/ViewTheme"
            />
            <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>

Now when I run it with F5 the app builds and runs but does not show the content in the view.xml although the Launch screen does show the LaunchTheme.现在,当我使用 F5 运行它时,应用程序构建并运行但不显示 view.xml 中的内容,尽管启动屏幕确实显示了 LaunchTheme。
Is it possible to have simple animations on the launch screen in a flutter app?是否可以在 Flutter 应用程序的启动屏幕上显示简单的动画? if so what am I doing wrong?如果是这样,我做错了什么?

In MainActivity Add在 MainActivity 添加

@Nullable
    override fun provideSplashScreen(): SplashScreen? {
        return SimpleSplashScreen()
    }
}

Refer the following github example https://github.com/flutter/flutter/tree/master/dev/integration_tests/android_splash_screens参考以下 github 示例https://github.com/flutter/flutter/tree/master/dev/integration_tests/android_splash_screens

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM