我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
这是我所见过的关于启动画面的最好的帖子:http://saulmm.github.io/avoding-android-cold-starts
Saúl Molinero介绍了两种不同的启动画面选项:利用窗口背景动画进入初始屏幕和显示占位符UI(这是谷歌目前在大多数应用程序中使用的流行选择)。
每当我需要考虑冷启动时间和避免由于长启动时间而导致用户流失时,我都会参考这篇文章。
希望这能有所帮助!
其他回答
有时用户打开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);
}
}
}
希望这对你有所帮助
进一步阅读:
应用发布时间和主题发布屏幕(Android Performance Patterns Season 6 Ep. 4) Android中的启动画面:正确的方法
旧的回答:
如何:简单的启动画面
这个答案会告诉你如何在应用启动时固定时间显示启动画面。例如,你可以选择显示3秒的启动画面。然而,如果你想要在可变的时间内(例如应用启动时间)显示飞溅屏幕,你应该看看Abdullah的答案https://stackoverflow.com/a/15832037/401025。但是要注意,应用程序在新设备上的启动速度可能非常快,所以用户只会看到一个flash,这是糟糕的用户体验。
首先,您需要在layout.xml文件中定义飞溅屏幕
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/splash"
android:layout_gravity="center"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, splash"/>
</LinearLayout>
你的活动:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
这就是全部;)
上面的答案很好,但我想补充一些其他的东西。我是Android的新手,我在开发过程中遇到了这些问题。希望这能帮助到像我这样的人。
The Splash screen is the entry point of my app, so add the following lines in AndroidManifest.xml. <activity android:name=".SplashActivity" android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> The splash screen should only show once in the app life cycle, I use a boolean variable to record the state of the splash screen, and only show it on the first time. public class SplashActivity extends Activity { private static boolean splashLoaded = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!splashLoaded) { setContentView(R.layout.activity_splash); int secondsDelayed = 1; new Handler().postDelayed(new Runnable() { public void run() { startActivity(new Intent(SplashActivity.this, MainActivity.class)); finish(); } }, secondsDelayed * 500); splashLoaded = true; } else { Intent goToMainActivity = new Intent(SplashActivity.this, MainActivity.class); goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(goToMainActivity); finish(); } } }
编码快乐!
在Android中,启动画面是一个有点不可用的对象:为了隐藏主活动启动的延迟,它不能尽快加载。使用它有两个原因:广告和网络运营。
实现为对话框使跳跃没有延迟从启动画面到主UI的活动。
public class SplashDialog extends Dialog {
ImageView splashscreen;
SplashLoader loader;
int splashTime = 4000;
public SplashDialog(Context context, int theme) {
super(context, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
setCancelable(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
cancel();
}
}, splashTime);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white">
<ImageView
android:id="@+id/splashscreen"
android:layout_width="190dp"
android:layout_height="190dp"
android:background="@drawable/whistle"
android:layout_centerInParent="true" />
</RelativeLayout>
并开始:
public class MyActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getCategories() != null && getIntent().getCategories().contains("android.intent.category.LAUNCHER")) {
showSplashScreen();
}
}
protected Dialog splashDialog;
protected void showSplashScreen() {
splashDialog = new SplashDialog(this, R.style.SplashScreen);
splashDialog.show();
}
...
}
创建一个活动,让我们活动命名为“a”,然后创建一个名为mysscreen .xml的xml文件,在其中设置一个启动画面图像作为背景,然后使用倒计时计时器从一个活动导航到另一个活动。要知道如何使用倒计时定时器看到我的答案在这个问题的TimerTask在Android?