How to Implement the welcome Screen and androidsplash for common Android UI instances
On the Android platform, after downloading an app, Splash Screen is the first thing that will pop up for the first time. This Splash Screen is not recommended in terms of Android design principles. Let's take a look at the general use of Splash Screen:
1. After the first installation, the pop-up screen of a simple APP can achieve the goal of brand marketing. The complicated APP is used to provide guidance for new users;
2. Update the version to describe the new features of the version;
Some people sneer at this kind of design, and others are eager to follow it. We will not discuss the advantages and disadvantages.
1. Simple Splash Screen
The implementation of this Splash Screen is simple. It is often used to display simple information such as the product Logo or version number. We only need to find a way to automatically jump to the main interface of the application after the WelcomeActivity runs for several seconds;
We only need to use a simple method:
// After 3 s, run 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); // after the main Activity is started, destroy itself finish () ;}, 3000 );
2. Splash Screen involving complex operations
The so-called complex operation is often because such an application requires many background operations before entering the interface. The Splash Screen allows users to wait. The operations generally involve:
- Obtain data from the network and store it locally
- Download images
- Obtain and parse JSON/XML files
- Send data to the server
- Authentication
- ....
Generally, this is a time-consuming operation similar to online download, but you have to do the work before the application enters the main interface. According to different applications, the work is also different. Here we obtain an image remotely. after entering the welcome page, we start to download an image remotely. Then we enter the main interface, display the parsed data on the main interface;
<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="match_parent"> <ImageView android:id="@+id/appImage" android:src="@mipmap/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android: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
<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"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
4. After SplashScreenActivity is completed, we use AsyncTask to obtain and parse data and pass the data to MainActivity through Intent,
Public class SplashScreenActivity extends Activity {private static final String url = "http://www.jycoder.com/json/Image/1.jpg"; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. splash_screen);/** simple Splash Screen implementation ** // ** after 3 s, run 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); // after the main Activity is started, destroy itself finish () ;}, 3000); ** // execute the task in the background, input url new fetchdatatask(cmd.exe cute (url);} public class FetchDataTask extends AsyncTask <String, Void, Bitmap> {// call @ Override protected void onPreExecute () {super. onPreExecute ();} // executes the background task @ Override protected Bitmap doInBackground (String... strings) {Bitmap bitmap = null; try {// obtain the image HttpURLConnection connection = (HttpURLConnection) (new URL (strings [0]) through the input image address. openConnection (); InputStream is = connection. getInputStream (); bitmap = BitmapFactory. decodeStream (is);} catch (IOException e) {e. printStackTrace ();} return bitmap;} // call @ Override protected void onPostExecute (Bitmap result) {super. onPostExecute (result); // transmits the obtained data to MainActivity Intent = new intent (SplashScreenActivity. this, MainActivity. class); // note that when intent transmits an image, the image object size should not exceed 40 kb intent. putExtra ("Image", result); startActivity (intent); // destroy itself after MainActivity is started finish ();}}}
5. Complete MainActivity. Pay attention to how to accept the objects passed by Intent.
Public class MainActivity extends Activity {private ImageView 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 transmits objects Bitmap bitmap = intent. getParcelableExtra ("Image"); imageView. setImageBitmap (bitmap );}}}
6. Remember to add the Internet permission 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"/> <application android:allowBackup="true" android:icon="@mipmap/logo" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android: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>
I encountered an error when I used an image stored remotely for the first time and encountered a very interesting problem:
Later I found this error because the size of the image cannot exceed 40 kb when I use Intent. Remember
Effect after completion:
Summary:
The above examples are very simple, but the idea of Splash Screen is basically nothing more than these, I hope it will help you.
Pay attention to the following public platforms ~~~ O (∩) O Haha ~
Reference: How to implement Android Splash Screen
- Weibo: @ mingsang Android Black History
- Mailbox: <13141459344@163.com>
- Personal Homepage: The history of mingsang's victory over Android Wang
Public Account: ITBird