我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
一种方法是通过创建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,或者在几毫秒后在现有的启动画面布局上膨胀主布局。
其他回答
它真的很简单,在安卓,我们只是使用处理器的概念来实现启动画面
在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);
}
虽然有很好的答案,但我将展示谷歌推荐的方法:
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文件夹如果你的图像不小。
这将导致更快的启动屏幕启动和拯救你从黑屏
这也避免了额外的透支
这是我所见过的关于启动画面的最好的帖子:http://saulmm.github.io/avoding-android-cold-starts
Saúl Molinero介绍了两种不同的启动画面选项:利用窗口背景动画进入初始屏幕和显示占位符UI(这是谷歌目前在大多数应用程序中使用的流行选择)。
每当我需要考虑冷启动时间和避免由于长启动时间而导致用户流失时,我都会参考这篇文章。
希望这能有所帮助!
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>
public class SplashActivity extends Activity {
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.activity_splash);
Thread thread = new Thread(){
public void run(){
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent in = new Intent(ctx,MainActivity.class);
startActivity(in);
finish();
}
};
thread.start();
}
}