Three solutions for black screen and white screen when Android starts an APP

Source: Internet
Author: User

Three solutions for black screen and white screen when Android starts an APP
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 mizmizations that are NOT specific to a special 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: one background .png background image provides a background image, however, 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. If the NinePatch background image is not very complex and can be made into a NinePatch image, you can directly create a NinePatch image and provide one to adapt to any mobile phone. 3. Use Layout-list to create background images. If you can use this method, we recommend using Layout-list to create background images. 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 LauncherActivity Intent intent = new Intent (LauncherActivity. this, MainActivity. class); startActivity (intent); finish (); break; default: br Eak ;}};@override protected 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 MainActivity mHandler. sendEmptyMessageDelayed (MSG_FINISH_LAUNCHERACTIVITY, 3000) ;}} activity_launcher.xml layout file, which is very simple. Remember that the background of LinearLayout is layout_list_start_pic, which is the same as theme, this eliminates the effects 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"/> </Lin EarLayout> AndroidManifest. xml. Here, 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 = ". lau NcherActivity "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, 2 theme Settings <resources xmlns: android = "http://schemas.android.com/apk/re S/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 mizmizations that are NOT specific to a special 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 mizmizations that are NOT specific to a special API-level can go here. --> </style> </resources> layout_list_start_pic.xml use this as the background image on the startup page <? 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>

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.