Android app loading screen background image takes too long to load
I am developing an android app. In my case, when the app starts it displays a temporary screen while AsyncTask fetches data in the background. This temporary screen contain only a background image (20 KB). But in this background takes a while to load. As a result of this when I start my app I only see a white screen, after a while it shows the background image. Any ideas on how to get rid of this problem and show the background image from the beginning Here is the xml file of the start screen
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/start"
android:orientation="vertical" >
</LinearLayout>
Thanks
I had the same problem. This awesome person has the explanation.
TL;DR (only in case the link goes bad, I would go read it though) :"the purpose of preview windows is to give the user immediate feedback that the app launched, but it also gives your app time to initialize itself. When your app is ready to run, the system removes the window and displays your app's windows and views." You can set this in themes. Minimally, my setup:
Theme in /res/values/styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">@drawable/app_bg</item>
</style>
<!-- Base theme for any other activity other than your starting one. -->
<!-- If only one activity, just set the splash bg as its bg -->
<style name="MyTheme.MainActivity" parent="@style/AppTheme">
<item name="android:windowBackground">@drawable/app_splash_bg</item>
</style>
</resources>
AndroidManifest.xml:
...
<activity
android:name=".MainActivity"
android:theme="@style/MyTheme.MainActivity"
...
<!-- Set the MainActivity theme, can be skipped if you only have -->
<!-- one activity and did not setup anything other than your base theme -->
MainActivity.java:
@Override
protected void onResume() {
super.onResume();
MainActivity.this.getWindow().setBackgroundDrawableResource(R.drawable.app_bg);
}
Basically, you setup a theme with the splash background for your starting activity. You then set the background to your normal one during onResume
of said starting activity.
Also, you can just comment out the windowBackground
declaration in the starting activity theme to see the original (I assume) "ugly white" screen on launch.
In your temporary screen, first you should do setContentView() and then call AsyncTask (With a progress bar, which is dismissed on it's onPostExecute()).
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Now start the AsyncTask.
}
Also, please note that when you transit from one activity to other, there will be some delay; which you can not eliminate. I will suggest you to have only once activity.
链接地址: http://www.djcxy.com/p/11756.html