Three solutions for black screen and white screen when Android starts the APP: androidapp

Source: Internet
Author: User

Three solutions for black screen and white screen when Android starts the APP: androidapp

You may wonder why some apps appear on the Activity page after a black or white screen for a while starting, but some apps are not like QQ mobile phones, it is true that we need to handle it here. Here, let's first look at why such a phenomenon occurs. In fact, it is very simple. A simple example of resume can be understood.

In fact, the black screen or white screen is not normal, but has not been loaded into the layout file, it has displayed the window background, black screen is the window background. The Code is as follows. You can write a small demo to understand it.

@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // Note: Add 3 seconds of sleep to ensure the effect of black screen for a while. Remove the 3 seconds of sleep in the project application. try {Thread. sleep (3000);} catch (InterruptedException e) {e. printStackTrace () ;}// previously, black screen or white screen are the background color of the window, which is the background color of the window and has not reached the layout of the interface yet, setContentView (R. layout. activity_launcher );}

Where is the window background provided? In the provided theme, the following provides a white background, that is, the white screen color setting at startup.

    <!-- Application theme. -->    <style name="AppTheme" parent="AppBaseTheme">        <item name="android:windowNoTitle">true</item>        <item name="android:windowBackground">@color/white</item>        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style>

Therefore, setting windowBackground in theme can solve the problem of white screen black at startup for a while. The following three solutions are provided:

1. PNG background image

Providing background images is a solution, but you need to adapt to various screens and provide many images. Unless the image is very complex, you can only use the background image. This method is not recommended.

Background image of November 9.png (NinePatch)

If the pictures are not very complex and can be made into NinePatch images, you can directly create NinePatch images and provide one to adapt to any mobile phone. Why not.

3. Use Layout-list to create a background image

If you can use this method, we recommend that you use this Layout-list to create a background image. The first two types use images to occupy memory. Layout-list saves a lot of memory, and the app does not say that the image is larger in size.

The following code is provided.

LaunchActivity takes three seconds to jump to the MainActivity on the home page after the startup interface is stopped. In order to display the black screen and white screen more effectively, the thread sleeps for 3 seconds before setContentView.

Public class LauncherActivity extends Activity {public final int MSG_FINISH_LAUNCHERACTIVITY = 500; public Handler mHandler = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {case MSG_FINISH_LAUNCHERACTIVITY: // jump to MainActivity and end the current LauncherActivityIntent intent = new Intent (LauncherActivity. this, MainActivity. class); startActivity (intent); finish (); break; default: break ;}};@overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // do not display the title bar of the system. Ensure that windowBackground is the same size as activity_main on the page and there will be no misplacement on the screen (remove this line and try to see the effect) getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); // Note: Add 3 seconds of sleep to ensure the effect of the black screen is obvious for a while. Remove the 3 seconds of sleep in the project application. try {Thread. sleep (3000);} catch (InterruptedException e) {e. printStackTrace ();} setContentView (R. layout. activity_launcher); // send a message after 3 seconds, jump to MainActivitymHandler. sendEmptyMessageDelayed (MSG_FINISH_LAUNCHERACTIVITY, 3000 );}}

The activity_launcher.xml layout file is very simple. Remember that the background of LinearLayout is layout_list_start_pic. It uses the same background as theme, which eliminates the effect of different backgrounds. Here, you must try to learn the benefits and effects of this operation.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/layout_list_start_pic" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#ffffff"        android:text="@string/hello_world" /></LinearLayout>

AndroidManifest. xml. Note that the theme used by the application is AppTheme, while the theme used by LauncherActivity is StartAppTheme. In this way, as long as LauncherActivity uses the StartAppTheme topic, other activities use the AppTheme topic.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.launcheractivity"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".LauncherActivity"            android:label="@string/app_name"            android:theme="@style/StartAppTheme" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                <activity android:name=".MainActivity"></activity>    </application></manifest>

Styles. xml, two theme settings

<resources xmlns:android="http://schemas.android.com/apk/res/android">    <!--        Base application theme, dependent on API level. This theme is replaced        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.    -->    <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar">        <!--            Theme customizations available in newer API levels can go in            res/values-vXX/styles.xml, while customizations related to            backward-compatibility can go here.        -->    </style>    <!-- Application theme. -->    <style name="AppTheme" parent="AppBaseTheme">        <item name="android:windowNoTitle">true</item>        <item name="android:windowBackground">@color/white</item>        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style>    <style name="StartAppTheme" parent="AppBaseTheme">        <item name="android:windowNoTitle">true</item>        <item name="android:windowBackground">@drawable/layout_list_start_pic</item>        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style></resources>

The layout_list_start_pic.xml startup page uses this as the background image.

<? Xml version = "1.0" encoding = "UTF-8"?> <Layer-list xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Set the screen background to white --> <item> <color android: color = "@ color/white"/> </item> <! -- Intermediate logo --> <item> <bitmap android: gravity = "center" android: src = "@ drawable/ic_launcher"/> </item> <! -- Bottom chart --> <item android: bottom = "10dp"> <bitmap android: gravity = "bottom | center_horizontal" android: src = "@ drawable/copyright"/> </item> </layer-list>

Another image is copyright.png.

Now, let's get it done.

During startup, the black screen and white screen are transformed into this image, haha ~

LauncherActivity page installed after startup

Adjust to MainActivity Interface

This is the case. I wanted to take a GIF animation and I didn't know how to send it to my computer ~~~ Later I learned that I could send it to the webpage...

The download link is sent up later. Oh, damn it, you're going to bed ~~ ·~~

If this article is helpful to you, please click here for support. If you have any questions, please leave a message for further discussion ~

Link: http://www.cnblogs.com/liqw/p/4263418.html

: Http://download.csdn.net/detail/lqw770737185/8411705

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.