我想让我的应用看起来更专业,所以我决定添加一个启动画面。

我应该如何进行实现呢?


当前回答

在Kotlin中编写以下代码:-

 Handler().postDelayed({
            val mainIntent = Intent(this@SplashActivity, LoginActivity::class.java)
            startActivity(mainIntent)
            finish()
        }, 500)

希望这对你有所帮助,谢谢........

其他回答

启动画面示例:

public class MainActivity extends Activity {
    private ImageView splashImageView;
    boolean splashloading = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        splashImageView = new ImageView(this);
        splashImageView.setScaleType(ScaleType.FIT_XY);
        splashImageView.setImageResource(R.drawable.ic_launcher);
        setContentView(splashImageView);
        splashloading = true;
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            public void run() {
                splashloading = false;
                setContentView(R.layout.activity_main);
            }

        }, 3000);

    }

}

Create an activity: Splash Create a layout XML file: splash.xml Put UI components in the splash.xml layout so it looks how you want your Splash.java may look like this: public class Splash extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); int secondsDelayed = 1; new Handler().postDelayed(new Runnable() { public void run() { startActivity(new Intent(Splash.this, ActivityB.class)); finish(); } }, secondsDelayed * 1000); } } change ActivityB.class to whichever activity you want to start after the splash screen check your manifest file and it should look like

<活动 android: name = "。屏” android: label = " @string / app_name " > > < /活动 <活动 android: name = "。飞溅” android: label = " @string / title_activity_splash_screen " > <意图过滤器> <action android:name="android.intent.action. main " /> <category android:name="android.intent.category. launcher " /> < /意图过滤器> > < /活动

阿卜杜拉的回答很好。但我想在我的回答中补充一些细节。

实现启动画面

正确执行闪屏的方法与你想象的略有不同。您所看到的splash视图必须立即准备好,甚至在您可以在splash活动中膨胀布局文件之前。

所以你不会使用布局文件。相反,指定启动画面的背景作为活动的主题背景。为此,首先在res/drawable中创建一个XML drawable。

background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Background Color -->
    <item
        android:drawable="@color/gray"/>

    <!-- Logo -->
    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

它只是一个层列表与标志在中心背景色与它。

现在打开styles.xml并添加这个样式

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
</style>

这个主题将没有动作栏和我们刚才创建的背景。

在manifest中,你需要将SplashTheme设置为你想要用作splash的活动。

<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

然后在你的活动代码中,使用意图引导用户进入特定的屏幕。

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

这才是正确的做法。我用这些参考来回答。

https://material.google.com/patterns/launch-screens.html https://www.bignerdranch.com/blog/splash-screens-the-right-way/ 感谢这些人把我推向正确的方向。我想要帮助其他人,因为公认的答案并不是做启动画面的建议。

Create an Activity SplashScreen.java public class SplashScreen extends Activity { protected boolean _active = true; protected int _splashTime = 3000; // time to display the splash screen in ms @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splashscreen); Thread splashTread = new Thread() { @Override public void run() { try { int waited = 0; while (_active && (waited < _splashTime)) { sleep(100); if (_active) { waited += 100; } } } catch (Exception e) { } finally { startActivity(new Intent(SplashScreen.this, MainActivity.class)); finish(); } }; }; splashTread.start(); } } splashscreen.xml will be like this <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="600px" android:layout_height="1024px" android:background="#FF0000"> </RelativeLayout>

你可以在你的onCreate方法中添加这个

new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // going to next activity
                    Intent i=new Intent(SplashScreenActivity.this,MainActivity.class);
                    startActivity(i);
                    finish();
                }
            },time);

初始化你的时间值在毫秒,因为你想…

private  static int time=5000;

欲了解更多详细信息,请从此链接下载完整代码…

https://github.com/Mr-Perfectt/Splash-Screen