我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
上面的答案很好,但我想补充一些其他的东西。我是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(); } } }
编码快乐!
其他回答
简单的代码,它的工作:)简单的飞溅
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(LoginSuccessFull.this, LoginActivity.class));
finish();
}
}, secondsDelayed * 1500);
虽然有很好的答案,但我将展示谷歌推荐的方法:
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文件夹如果你的图像不小。
这将导致更快的启动屏幕启动和拯救你从黑屏
这也避免了额外的透支
创建一个活动,让我们活动命名为“a”,然后创建一个名为mysscreen .xml的xml文件,在其中设置一个启动画面图像作为背景,然后使用倒计时计时器从一个活动导航到另一个活动。要知道如何使用倒计时定时器看到我的答案在这个问题的TimerTask在Android?
以上所有的答案都非常好。但是存在内存泄漏的问题。 这个问题在Android社区通常被称为“泄露活动”。这到底是什么意思呢?
当配置发生变化时,比如方向变化,Android会销毁Activity并重新创建它。通常,垃圾收集器只会清除旧Activity实例的已分配内存,一切正常。
“泄漏一个活动”指的是垃圾收集器无法清除旧活动实例的已分配内存的情况,因为它被(强)引用于一个活动实例外的对象。每个Android应用程序都分配了特定数量的内存。当垃圾回收器不能释放未使用的内存时,应用程序的性能将逐渐下降,最终崩溃并出现OutOfMemory错误。
如何确定应用程序是否泄漏内存?最快的方法是在Android Studio中打开内存选项卡,并在改变方向时注意分配的内存。 如果分配的内存一直在增加而不减少,那么就有内存泄漏。
1.当用户改变方向时内存泄漏。
首先,你需要在布局资源splashscreen.xml文件中定义启动画面
启动画面活动的示例代码。
public class Splash extends Activity {
// 1. Create a static nested class that extends Runnable to start the main Activity
private static class StartMainActivityRunnable implements Runnable {
// 2. Make sure we keep the source Activity as a WeakReference (more on that later)
private WeakReference mActivity;
private StartMainActivityRunnable(Activity activity) {
mActivity = new WeakReference(activity);
}
@Override
public void run() {
// 3. Check that the reference is valid and execute the code
if (mActivity.get() != null) {
Activity activity = mActivity.get();
Intent mainIntent = new Intent(activity, MainActivity.class);
activity.startActivity(mainIntent);
activity.finish();
}
}
}
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;
// 4. Declare the Handler as a member variable
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
// 5. Pass a new instance of StartMainActivityRunnable with reference to 'this'.
mHandler.postDelayed(new StartMainActivityRunnable(this), SPLASH_DISPLAY_LENGTH);
}
// 6. Override onDestroy()
@Override
public void onDestroy() {
// 7. Remove any delayed Runnable(s) and prevent them from executing.
mHandler.removeCallbacksAndMessages(null);
// 8. Eagerly clear mHandler allocated memory
mHandler = null;
}
}
欲了解更多信息,请点击此链接
这是我所见过的关于启动画面的最好的帖子:http://saulmm.github.io/avoding-android-cold-starts
Saúl Molinero介绍了两种不同的启动画面选项:利用窗口背景动画进入初始屏幕和显示占位符UI(这是谷歌目前在大多数应用程序中使用的流行选择)。
每当我需要考虑冷启动时间和避免由于长启动时间而导致用户流失时,我都会参考这篇文章。
希望这能有所帮助!