我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
另一种方法是使用CountDownTimer实现
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new CountDownTimer(5000, 1000) { //5 seconds
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}.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;
}
}
欲了解更多信息,请点击此链接
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();
}
}
进一步阅读:
应用发布时间和主题发布屏幕(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);
}
}
这就是全部;)
- Add in SplashActivity
public class SplashActivity extends Activity {
private ProgressBar progressBar;
int i=0;
Context context;
private GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
context = this;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(Splash.this, LoginActivity.class));
finish();
}
}, 2000);
}
}
- Add in activity_splash.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Splash">
<ImageView
android:id="@+id/ivLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/icon_splash"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
<ProgressBar
android:id="@+id/circle_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:max="100"
android:progressTint="@color/green"
android:visibility="visible" />
</RelativeLayout>
- Add in AndroidManifest.xml
<activity android:name="ex.com.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
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>