Hor*_*stB 14 java android splash-screen
我有一个Android应用程序,显示3秒的"启动画面".之后,MainActivity被加载.
不幸的是,MainActivity需要额外的约4秒才能加载.在第一次启动时甚至更长.但是,当应用程序加载时,一切运行顺利.
现在,如何在Splash Screen的显示期间实现MainActivity的加载?它应该显示一个图像,直到整个东西完全加载.我已经阅读了有关Async-Task的内容,但我不知道该放在哪里以及如何正确使用它.有谁可以帮助我吗?
SplashScreen.java
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startup);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
public class MainActivity extends Activity implements OnClickListener, MediaController.MediaPlayerControl {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Some heavy processing
//starting services
//starting Google Text to Speech
//and so on...
}
}
Run Code Online (Sandbox Code Playgroud)
Bry*_*yan 33
您不应该在启动时创建新线程,而是应该创建一个不必等待资源加载的视图,如本文所述:Splash Screens the Right Way.
如本文所述,您应该创建一个layer-listdrawable而不是layoutXML文件:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Fill the background with a solid color -->
<item android:drawable="@color/gray"/>
<!-- Place your bitmap in the center -->
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
然后使用可绘制文件作为背景创建主题.我使用background属性而不是windowBackground文章中建议的属性,因为background考虑了状态和导航栏,更好地使drawable居中.我也设置windowAnimationStyle为null使闪屏不会转换为MainActivity:
<resources>
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<!-- Splash Screen theme -->
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">@drawable/background_splash</item>
<item name="android:windowAnimationStyle">@null</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后在清单中声明您的主题SplashActivity:
<activity android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
最后你需要做的SplashActivity只是启动你的MainActivity,启动画面只会显示你的应用程序配置所需的时间:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
Run Code Online (Sandbox Code Playgroud)
TmK*_*KVU 13
如果对显示启动画面的时间没有特定限制,您可以AsyncTask按以下方式使用:
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startup);
startHeavyProcessing();
}
private void startHeavyProcessing(){
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
//some heavy processing resulting in a Data String
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "whatever result you have";
}
@Override
protected void onPostExecute(String result) {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.putExtra("data", result);
startActivity(i);
finish();
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
Run Code Online (Sandbox Code Playgroud)
如果结果数据的性质不是String,那么您可以将ParcelableObject作为额外的活动.在onCreate你可以检索数据:
getIntent().getExtras.getString('data');
| 归档时间: |
|
| 查看次数: |
30010 次 |
| 最近记录: |