我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread t=new Thread()
{
public void run()
{
try {
sleep(2000);
finish();
Intent cv=new Intent(MainActivity.this,HomeScreen.class/*otherclass*/);
startActivity(cv);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
}
其他回答
注意这个解决方案不会让用户等待更长的时间:启动画面的延迟取决于应用程序的启动时间。
当你打开任何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
虽然有很好的答案,但我将展示谷歌推荐的方法:
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文件夹如果你的图像不小。
这将导致更快的启动屏幕启动和拯救你从黑屏
这也避免了额外的透支
我在android中使用线程制作Flash屏幕。
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public class HomeScreen extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_home);
Thread thread = new Thread(){
public void run(){
try {
Thread.sleep(3 * 1000);
Intent i = new Intent(HomeScreen.this, MainActivity.class);
startActivity(i);
} catch (InterruptedException e) {
}
}
};
thread.start();
}
}
以上所有的答案都非常好。但是存在内存泄漏的问题。 这个问题在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;
}
}
欲了解更多信息,请点击此链接
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>