Android Development notes: Splash implementation of the detailed _android

Source: Internet
Author: User
What is Splash
Splash is the application started before the start of a screen, the above simple introduction of the application of the manufacturer, the manufacturer's logo, name and version of the information, more than a picture, display a few seconds later will automatically message, and then show the application of the main page. On PCs, it's common for applications with a variety of platforms, mostly a picture displayed in the center of the screen, such as the Microsoft Office series, or GIMP. Splash is the most common in a variety of games, almost all games begin with a Zhang Quan picture, which usually shows the manufacturer's logo, the name of the game, and so on. In mobile devices such as mobile phones, such as PC Splash very few, at least for Android and iOS native applications do not have this splash, but I do not know since when, this splash began to become popular in third party applications, Almost all third-party applications have startup Splash. These splash features are filled with the entire screen, logo above, the name of the manufacturer, the name version of the application, and so on, about 3-5 seconds later, the splash automatically disappears, the application Main page is displayed. Many applications also display the loading process on splash pages.
Here 's how to implement Splash and its pros and cons in Android:
using activity as a splash
This is probably the most common way to use an activity, to set a background to it, or to display information (manufacturer, LOGO, name, and version), show it for a few seconds, then finish () and start the application main activity.
Copy Code code as follows:

<activity android:name= ". Splashactivity "
Android:theme= "@android: Style/theme.notitlebar.fullscreen"
Android:nohistory= "true"
Android:configchanges= "Orientation|keyboardhidden"
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

Copy Code code as follows:

public class Splashactivity extends activity {
Private Handler Mmainhandler = new Handler () {
@Override
public void Handlemessage (msg) {
Intent Intent = new Intent (intent.action_main);
Intent.setclass (Getapplication (), nottomorrowactivity.class);
Intent.setflags (Intent.flag_activity_new_task);
StartActivity (Intent);
Overridependingtransition must is called after finish () or startactivity, or it won ' t work.
Overridependingtransition (r.anim.activity_in, r.anim.splash_out);
}
};

@Override
public void OnCreate (Bundle icicle) {
Super.oncreate (Icicle);
GetWindow (). Setbackgrounddrawableresource (r.drawable.kg);
Mmainhandler.sendemptymessagedelayed (0, 5000);
}

Much easier to handle key events
@Override
public void onbackpressed () {
}
}




The advantage of using activity is that:
Easy to control key events
Because when you display splash, you should not respond to events, such as touch events, or back or menu, because this is a separate activity, so you can easily ban these user events without worrying about affecting other logic, or worrying about not being able to re-enable them in time.
Easy Customization
Because it's a separate activity, you can set it to a screen, or there's no titlebar, or any other style or style. In practical use, almost all splash are set to full screen. Because it's a separate activity, it doesn't matter if your application is not full-screen, because just setting the splashactivity to Full-screen does not affect other activity.
Logic is separate from subject logic and easy to maintain
As in the previous two points, because it is a separate activity, all splash related logic is in it, and the activity of the application subject is separate, so the logic in the splash does not affect other activity, which is also easier to modify and maintain, Because it will not be twisted together to affect each other.
The only disadvantage of using an activity is that it does not use the time splash display to do data loading. Because it is a separate activity, there is no control over other activity, and other activity is not yet created.
use Viewswitcher as Splash
This can also be used as a splash. Viewswitcher is a viewgroup, which has two child view, which can only display one at a time. The main approach is that the rootview of the activity is set to Viewswitcher, and a layout (such as ImageView) as splash as the first child view of the Viewswitcher , and then the subject layout of the activity as the second child view; When the activity starts, it displays the ImageView as splash first, and then the main layout is displayed after a few seconds. In fact, Viewswitcher usually use the activity to load data, display a progress bar, and then show the real layout when there is data.
Advantages of using Viewswitcher
The advantage of using Viewswitcher is that you can use the splash time to load the data so that the user does not have to wait for the splash to wait for the data to load.
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<viewswitcher xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/view_container"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:inanimation= "@anim/activity_in"
android:outanimation= "@anim/splash_out" >
<imageview android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:src= "@drawable/kg"
Android:scaletype= "Fitxy"/>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<textview android:id= "@+id/header"
style= "@style/header_text"/>
<textview android:id= "@+id/header_tip"
style= "@style/task_text"/>
<listview android:id= "@+id/task_list"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:footerdividersenabled= "true"
android:background= "@color/white"/>
</LinearLayout>
</ViewSwitcher>

Copy Code code as follows:

public class Nottomorrowactivity extends activity {
Protected static final String TAG = "notomorrowactivity";
protected static final int msg_show_layout = 10;
private static final int menu_add_task = 0;
Private Handler Mmainhandler = new Handler () {
@Override
public void Handlemessage (msg) {
Switch (msg.what) {
Case Msg_show_layout:
Final Viewswitcher container = (viewswitcher) Findviewbyid (R.id.view_container);
Container.shownext ();
ImageView view = (imageview) container.getchildat (0);
View.setimageresource (0);
Container.removeviewat (0);
Msplashing = false;
Break
Default
Break
}
}
};
Private Boolean msplashing;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Msplashing = true;
Setcontentview (r.layout.not_tomorrow_activity);
Mmainhandler.sendemptymessagedelayed (Msg_show_layout, 5000);
}
@Override
public void onbackpressed () {
if (!msplashing) {
Super.onbackpressed ();
}
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
if (msplashing) {
return false;
}
Menu.add (0, Menu_add_task, 0, R.string.add_hint);
return Super.oncreateoptionsmenu (menu);
}

@Override
public boolean Onprepareoptionsmenu (Menu menu) {
if (msplashing) {
return false;
}
return Super.onprepareoptionsmenu (menu);
}
}




Disadvantages of using Viewswitcher
Be aware of events
Events such as Back,menu must be banned when the splash is displayed, and then re-enabled after Splash is finished.
no way full screen.
Unless the main activity is full-screen, there is no way to make the view Full-screen and then set back.
Logic twisted together, difficult to maintain
As with the above prohibitions and enabling events, these things are in the activity, all the logic in an event, naturally difficult to maintain and modify, and error-prone.
Recommended Practices
The recommended practice is not to use splash, or at most the first time after the application is installed, from the user's point of view, it makes no sense, so you see Android or iOS in the native applications there is no splash or something. Should let the application directly into the business, let the user immediately into his most concerned about the page. Similarly, the use of tips is useless, the real good should be concise and operation, not to learn will be, rather than make a lot of tutorials or tips. Instead of spending time working on splash or using hints, consider how to simplify the operation.

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.