Android Create splash screen

Source: Internet
Author: User

after each Android app launch, a splash splash screen appears, showing the product's logo, company logo, or developer information. Assuming the application startup time is longer, the start-up interface is a very good thing, allowing the user to wait patiently for this tedious time.

1. Make Splash Interface
Highlight product logo, product name, product main features;
Indicate the product version number information;
Indicate the company information or developer information;
Background image, can also be replaced with background color;

2. Working with background resources
Most splash interfaces will wait for a certain amount of time and then switch to the next interface;
In fact, in this period of time, the system can be detected, such as whether the network, power supply is sufficient;
Or, pre-loading relevant data;
In order to allow the start-up interface to display time fixed, it is necessary to calculate the time spent running the above preprocessing tasks, then: Start the interface sleep time = fixed time-preprocessing task time;

  There are two ideas, one for two activity, One to do the start screen, and one is the main page, you can also use a activity to do, Cheng .

method One: two activity, through thread Delay the specified time to run activity jumps, we need to establish two Activity, one is splashactivity, used to do the splash screen. There is also a helloworldactivity. Here is just a description of how to do the start screen, so it is not specific to complete the activity.

Core code:

Package Com.demo.app;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.os.handler;public class Splashactivity extends Activity {private final int splash_display_lenght = 6000;//delay Six seconds @o verrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.splash); new Handler (). postdelayed (New Runnable () {public void run () {Intent mainintent = new Intent ( Splashactivity.this,helloworldactivity.class); SplashActivity.this.startActivity (mainintent); SplashActivity.this.finish ();}}, Splash_display_lenght);}}

Description

Handler (). Postdelayed is deferred for a specified time before running

The handler class is primarily capable of setting the time to run the Runnable object using, for example, the following 3 methods:

  run Runnable Object Public  Final Boolean post (Runnable R) immediately;  runs the Runnable object at the specified time (uptimemillis) public  Final Boolean postattime (Runnable R, Long Uptimemillis);  runs the Runnable object at the specified time interval (delaymillis)  

The following two lines of code start a new activity and close the current activity at the same time.

SplashActivity.this.startActivity (mainintent); SplashActivity.this.finish ();

The following is the life cycle of activity, so that you can understand the jump of activity.

As seen above, the Android program ape can determine an activity's "life", but cannot determine its "death", also said that the program ape can start an activity, but can not manually "end" an activity.

When you call the Activity.finish () method, the result is the same as when the user presses the back key: tells the activity Manager that the activity instance has completed the corresponding work and can be "recycled".

Activity Manager then activates the activity on the second level of the stack and again into the stack, at the same time the original activity is pressed into the second layer of the stack, from the active state to the Paused state.

For example, in the example above: starting from the Splashactivity helloworldactivity, the current is at the top of the stack is helloworldactivity, the second layer is splashactivity.

When we call the Splashactivity.finish () method (which we call through SplashActivity.this.finish () in splashactivity), splashactivity from the Active The state transitions stoped state and is removed from the stack by the system, and the flag can be "recycled".

The state of the Activity is related to its position in the stack such as:

The sample is

Starting with Activity2 from Activity1, the Activity2 is currently at the top of the stack, the second layer is Activity1, and when we call the Activity2 () method in Activity2.finish, the Activity Once again, the Manager activates the Activity1 merge stack, Activity2 transitions from the active state to the stoped state, and the annotation Activity2 can be "recycled" at the same time. Activity1. The Onactivityresult (int requestcode, int resultcode, Intent data) method is run, and the data returned by the Activity2 is returned to Activity1 via data references.

It is also important to note that you should not forget to change the Androidmanifest.xml file. For example, the following:

<?xmlversion= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "      package=" Com.demo.app "       android:versioncode=" 1 "       android:versionname=" 1.0 "    < applicationandroid:icon= "@drawable/icon", android:label= "@string/app_name" >        <activityandroid:name=. Splashactivity "                   android:label=" @string/app_name ">;            <intent-filter>                <actionandroid:name= "Android.intent.action.MAIN"/>                 <categoryandroid: Name= "Android.intent.category.LAUNCHER"/>             </intent-filter>         </activity>     < Activityandroid:name= "Main" >    </activity> </application>  <uses-sdkandroid: minsdkversion= "3"/> </manifest>

Method Two: an activity to do. The startup screen appears when the program starts, and when it is finished, it is hidden

Layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><linearlayout android:id= "@+id/splashscreen" android:orientation= "vertical"                 android:layout_width= " Fill_parent "android:layout_height=" fill_parent "><textview android:id=" @+id/info "                         android:layout_width = "Fill_parent" android:layout_height= "wrap_content"                         android:gravity= "center" android:paddingtop= "10px"                         Android:text= "This is a splash!"/></linearlayout><textview android:layout_width= "Fill_parent" Android: paddingtop= "10px"                 android:layout_height= "wrap_content" android:text= "This is a Context"/></linearlayout >

Description

Here is a linearlayout with ID SplashScreen, which is the part that appears when the program starts. When the boot is complete, it will be hidden.

Core code:

Package Com.demo.app;import Android.app.activity;import Android.os.bundle;import android.os.handler;import Android.os.message;import Android.os.systemclock;import Android.view.view;import Android.view.Window;import Android.widget.linearlayout;import Android.widget.textview;public class Mainactivity extends Activity {private LinearLayout splash;private TextView tv;private static final int stopsplash = 0;//time in millisecondsprivate static FINA L Long splashtime = 1000;private Handler Splashhandler = new Handler () {public void Handlemessage (Message msg) {switch (ms G.what) {case STOPSPLASH:SystemClock.sleep (4000); splash.setvisibility (View.gone); Super.handlemessage (msg);}}; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); GetWindow (). Requestfeature (window.feature_progress); Setcontentview (r.layout.main); splash = (linearlayout) findViewById ( R.id.splashscreen); TV = (TextView) Findviewbyid (r.id.info); Tv.settext ("Data connection is being established"); Message msg = new MeSsage (); msg.what = stopsplash;splashhandler.sendmessagedelayed (msg, splashtime);}} 

When a message is sent after the app is launched, the specified area is set to hidden, splash.setvisibility (View.gone); The start-up interface is implemented.

Summing up, the individual thinks or the first method is more appropriate, when a program main activity in the things are more, assuming we also add the splash screen, will inevitably affect the efficiency of the program, so the actual application, the first method is preferred.


Android Create splash screen

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.