How the Android UI common instance implements the Welcome interface (Splash screen)

Source: Internet
Author: User

Under the Android platform, after downloading an app, the first open is splash screen, not to mention the Android design principles do not advocate this splash screen. Let's take a look at the general scenario of using splash screen:

1, after the first installation, a simple app splash screen to achieve the purpose of brand marketing, complex point of the app to provide novice guidance;

2, version update, description version of the new features;

Some people scoff at this design, some people flock to, who is good or bad is not our discussion of the list.

1, Simple splash screen

This splash screen implementation and simple, commonly used to display product logo or version number and other simple information, we only need to find a way to let Welcomeactivity run a few seconds after the automatic jump to the application of the main interface;

We only need to use a simple method:

//3s后,执行run方法启动主界面Activity    new Handler().postDelayed(new Runnable() {            @Override            publicvoidrun() {                new Intent(SplashScreen.this, MainActivity.class);                startActivity(i);                //启动主Activity后销毁自身                finish();            }        3000);
2, splash screen involving complex operation

The so-called complex operation is because often this application before entering the interface requires a lot of background operations, through splash screen to allow users to wait, generally involved in the operation is:

    • Get data from the network and store it locally
    • Download image
    • Obtaining and parsing files such as Json/xml
    • Send data to the service side
    • Identity verification
    • 。。。。

This is usually a time-consuming operation similar to a network download, but it has to be done before the application enters the main realm. According to the different application, the work is also different, here to get a picture remotely, we enter the Welcome interface, began to download a picture from the remote, we will enter the main interface after the completion of the analysis of the data displayed in the main interface;

    1. image address::
    2. To create a splashscreen layout:res/layout/splash_screen.xml
<linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:gravity  = "center"  android:orientation  = "vertical"  android:layout_height  =" match_parent " android:layout_width  =;     <imageview  android:id
      = "@+id/appimage"  android:src  = "@mipmap/logo"  android:layout_width  =" wrap_content " android:layout_height  =" wrap_content "/>     <TextViewandroid:gravity="center"android:text="Welcome to Ms_ Movie "android:layout_margintop=" 15DP "android:textsize=" 30sp "  Android:textcolor="#00ACED"android:layout_width="Wrap_content"  Android:layout_height="wrap_content" />                                                            </linearlayout>

Layout effect

    1. To create a mainactivity layout: to res/layout/activity_main.xml display only images obtained from remote
    <relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" xmlns:tools="Http://schemas.android.com/tools"android:gravity="center" android:layout_width="Match_parent"android:layout_height="Match_parent" >                    <ImageViewandroid:id= "@+id/image"android:layout_width="Wrap_ Content "android:layout_height=" wrap_content " />                            </relativelayout>

4. Complete the splashscreenactivity, we use Asynctask to perform the acquisition and parsing of the data, passing the data to mainactivity through intent,

 Public  class splashscreenactivity extends Activity {    Private Static FinalString url="Http://www.jycoder.com/json/Image/1.jpg"; @Overrideprotected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.splash_screen);/ * * Simple splash screen Implementation * */        /* * 3s, execute the Run method to start the main interface activity new Handler (). postdelayed (New Runnable () {@Override                    public void Run () {Intent i = new Intent (splashscreen.this, Mainactivity.class);                    StartActivity (i);                Destroy self finish () after initiating main activity;        }}, 3000); * */        //Perform tasks in the background, incoming URLs        NewFetchdatatask (). Execute (URL); } Public  class fetchdatatask extends asynctask<String,Void, Bitmap>{        //Call before execution@Overrideprotected voidOnPreExecute () {Super. OnPreExecute (); }//Perform background tasks@OverrideprotectedBitmap Doinbackground (String ... strings) {Bitmap bitmap=NULL;Try{//Through the incoming picture address, get the pictureHttpURLConnection connection= (HttpURLConnection) (NewURL (strings[0]). OpenConnection ();                InputStream Is=connection.getinputstream ();            Bitmap= Bitmapfactory.decodestream (IS); }Catch(IOException e)            {E.printstacktrace (); }returnBitmap }//The task is called when it completes@Overrideprotected voidOnPostExecute (Bitmap result) {Super. OnPostExecute (Result);//transmit the obtained data to mainactivity via intentIntent intent=NewIntent (splashscreenactivity. This, Mainactivity.class);//Note that when intent pass a picture, the picture object should not be larger than 40KIntent.putextra ("Image", result); StartActivity (Intent);//Destroy itself after starting mainactivityFinish (); }    }}

5. Complete the mainactivity, where you need to be aware of how to accept objects passed by intent

  Public  class mainactivity extends Activity {        PrivateImageView ImageView;@Override        protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        imageview= (ImageView) Findviewbyid (r.id.image); Intent intent=getintent ();if(intent!=NULL){//Note the way intent objects are passedBitmap Bitmap=intent.getparcelableextra ("Image");        Imageview.setimagebitmap (bitmap); }    }}

6. Remember to add networking privileges to manifest.xml and set splashscreenactivity to start activity

<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android"  package ="Com.coder.splashscreen" >        <uses-permission android:name="Android.permission.INTERNET"/>    <applicationandroid:allowbackup="true"android:icon="@mipmap/ Logo "android:label=" @string/app_name "android:theme=" @style/apptheme " >                                        <activityandroid:name=". Splashscreenactivity "android:label=" @string/app_name " >                                    <intent-filter>                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=". Mainactivity "/>    </Application></manifest>

The first time I made a mistake when I was storing a picture in a remote, I encountered a very interesting question:

Later found that this error is due to use intent transfer pictures, size can not exceed 40K, remember OH

After the completion of the effect:

Summarize:

The above examples are very simple, but basically splash screen of the idea is nothing more than this, I hope to be helpful to you.

Remember to pay attention to the public platform below ~~~o (∩_∩) O haha ~

Reference: How to implement Android Splash screen

    • Weibo: @ Ming sang Android Black history
    • E-mail: <[email protected]>
    • Personal homepage: Minsan wins the black history of Android Wang
    • Public Number: Itbird

How the Android UI common instance implements the Welcome interface (Splash screen)

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.