Android project optimization (2): The start page optimization

First, the start page black screen issues

1.1 Symptom description

Android App start page black screen problem, the phenomenon as follows: if there is no special treatment, then there will be black and white phenomenon of the moment when Android App development started. Even if you start to load a page interface layout, do not do other time-consuming process, it looks like there will be a moment of black and white issue. When you click the cold from the desktop icon to start a small Launcher App, the program requires some basic initialization, for example, do a lot of time-consuming operations in the Application or SplashActivity, such as initialization and other third-party SDK, when the phone is not good performance, when the configuration is not high, the phenomenon is particularly evident.

Note that, in some places, also known as black screen, the key is how to style Style App settings.

Second, the start page black screen problem analysis

1.2 Analysis

Why is this problem? Mainly in the following points:

1. When the system starts a App, zygote process will first create a new process to run the App, but to create a process that takes time, before the creation, the interface is presented in suspended animation, so the system according to your manifest file different set of theme colors to show a blank screen or a blank screen. And this black (white) screen should be the official title Preview Window, that is, the preview window.

2. This phenomenon is actually the problem is the default theme Activity in android: windowBackground white or black caused.

3. Start the order summary is actually: App start --Preview Window (also called the preview window) - start page.

1.3 solve the black screen problem

Android in the choice of black or white screen when the show is based on the theme of your set is different, that is, although you have code that is executed, your configuration file was read in advance, and used as a showcase Preview Window based interface. So, the solution is the entry point to the entire APP manifest file, more precisely, should be the subject of profiles.

Set up a profile style in the style of windowBackground this property to display a background image Another effect is to launch the application will feel very fast, and contentView loading MainActivity is asynchronous.

A first solution

Solution: Start the current page to add a background style pattern

Set style style as follows:

<style name="SplashTheme" parent="AppTheme">
    <item name="android:windowBackground">@mipmap/splash</item>
    <item name="android:statusBarColor" tools:ignore="NewApi">@color/white</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

Note that, in the manifest file:

<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>

Instant black and white effect does not occur after treatment when App start:

The theme is set to start in the Activity theme, windowBackground is about to show the preview window. Where splash may be a whole picture, it can also be a picture to resolve the XML file resources.

The program points to note: to set the background image Preview Window If not treated, the picture will always exist in memory, so that when we enter into the welcome page, do not forget to set the background image empty:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    // set the window's background blank
    getWindow().setBackgroundDrawable(null);
    super.onCreate(savedInstanceState);
}

How this operation screen adaptation of it? A: Such load diagram style by style setting SplashActivity, so you can not set the zoom function like imageView, so you can use .9 picture.

The second solution

Prohibit loaded Preview Window, specific practices are as follows:

<style name="SplashTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDisablePreview">true</item>
</style>

Activity is set to the theme initiated, can disable the Preview Window, of course, it was through the preview window is set to full transparency, also reached a similar effect.

The role of windowDisablePreview: by setting android: windowDisablePreview property, disabled preview the animation window before SplashActivity display, will never be the theme of the window to display its preview, which also guarantees will not be black and white or black. However, with the set android: windowIsTranslucent attributes, if at the time SplashActivity start, there are too many complex operations, click on the application icon will appear in the phone, but after n seconds, the application will open a bad card Dayton experience effect.

Whether the program has shortcomings? There, after a brief click that does not respond to a few hundred milliseconds, like "suspended animation" of the same, after a while children jump out before the first Activity of our application.

 

Guess you like

Origin www.cnblogs.com/renhui/p/11027248.html