我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
它真的很简单,在安卓,我们只是使用处理器的概念来实现启动画面
在SplashScreenActivity java文件中粘贴此代码。
在SplashScreenActivity xml文件中使用imageview放置任何图片。
public void LoadScreen() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, AgilanbuGameOptionsActivity.class);
startActivity(i);
}
}, 2000);
}
其他回答
这里有一个简单的!
~卢诺克斯
MainActivity.java
package com.example.splashscreen;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
splashscreen.java
package com.example.splashscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class splashscreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
//Splash Screen duration
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(splashscreen.this, MainActivity.class));
finish();
}
}, secondsDelayed * 3000);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splashlogo"
/>
splashlogo.png
splashlogo.png
GitHub
SplashScreen
一种方法是通过创建FullScreenActivity/EmptyActivity(例如SplashScreenActivity),并将其设置为打开应用程序时显示的第一个活动。在AndroidManifest.xml中添加以下内容到您的活动
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
然后,您可以设置一个处理程序,在几秒钟后取消此活动。
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i= new Intent(SplashScreenActivity.this,MainActivity.class);
startActivity(i); //start new activity
finish();
}
}, 3000); //time in milliseconds
其次,如果你不想创建一个单独的活动,你可以在MainActivity上膨胀一个布局,并将布局可见性设置为GONE,或者在几毫秒后在现有的启动画面布局上膨胀主布局。
注意这个解决方案不会让用户等待更长的时间:启动画面的延迟取决于应用程序的启动时间。
当你打开任何android应用程序时,默认情况下你会看到一个黑屏,上面是应用程序的标题和图标,你可以通过使用样式/主题来改变这种情况。
首先,在values文件夹中创建一个style.xml,并向其添加一个样式。
<style name="splashScreenTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
而不是使用@android:style/Theme.DeviceDefault.Light。NoActionBar你可以使用任何其他主题作为父主题。
其次,在你的应用Manifest.xml中添加android:theme="@style/splashScreenTheme"到你的主活动。
<activity
android:name="MainActivity"
android:label="@string/app_name"
android:theme="@style/splashScreenTheme" >
第三,在onCreate()启动活动中更新主题。
protected void onCreate(Bundle savedInstanceState) {
// Make sure this is before calling super.onCreate
setTheme(R.style.mainAppTheme);
super.onCreate(savedInstanceState);
}
更新 看看这篇文章。
感谢@mat1h和@adelriosantiago
有时用户打开SplashActivity并立即退出,但应用程序仍然在SPLASH_SCREEN_DISPLAY_LENGTH后转到MainActivity。
为了防止它:在SplashActivity中,你应该在移动到MainActivity之前检查SplashActivity是否结束
public class SplashActivity extends Activity {
private final int SPLASH_SCREEN_DISPLAY_LENGTH = 2000;
@Override
public void onCreate(Bundle icicle) {
...
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (!isFinishing()) {//isFinishing(): If the activity is finishing, returns true; else returns false.
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}, SPLASH_SCREEN_DISPLAY_LENGTH);
}
}
}
希望这对你有所帮助
虽然有很好的答案,但我将展示谷歌推荐的方法:
1)首先为启动画面创建一个主题: 你有一个名为splashscreenTheme的主题,你的启动器主题将是:
<style name="splashscreenTheme">
<item name="android:windowBackground">@drawable/launch_screen</item>
</style>
注意:
android:windowBackground已经设置了你的溅屏图像编号 需要在UI中再次这样做。
你也可以在这里使用颜色来代替绘图。
2)设置主题为splashscreenActivity的manifest
<activity
android:name=".activity.splashscreenActivity"
android:screenOrientation="portrait"
android:theme="@style/splashscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
3)确保你的launch_screen drawable不在drawable文件夹如果你的图像不小。
这将导致更快的启动屏幕启动和拯救你从黑屏
这也避免了额外的透支