Almost every game has a loading screen, not because loading screen is beautifule, of course make it beautiful is necessary, because it is the first screen presented to users, but we need loading screen to load necessary resources, such as bitmap or drawable, which will make our app slow if we don’t load them before hand. Below is an example of loading screen:
We can see the screen is simple, just a progress bar and a background image, may be you can also put a text above the progress bar to indicate loading process.
###Loading Screen Layout
The layout of loading screen is simple, like below:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="@drawable/loading">
<ProgressBar
android:id="@+id/pb_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/colorprogress"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:layout_marginBottom="20dip" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="10dip"/>
<TextView
android:id="@+id/tv_loadingtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/pb_progressbar"
android:layout_centerHorizontal="true"
android:text="Loading, please wait..."
android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#ffffffff" android:shadowColor="#000000" android:shadowDx="2.0" android:shadowDy="2.0" android:shadowRadius="3.0"/>
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pb_progressbar"
android:layout_centerHorizontal="true"
android:text="Loading..."
android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#ffffffff" android:shadowColor="#000000" android:shadowDx="2.0" android:shadowDy="2.0" android:shadowRadius="3.0"/>
</RelativeLayout>
We just create a relative layout with a background image and put progress bar and textview at some position. You may also need to custom your own progressbar, you can find many posts about progressbar on the web.
###AsyncTask
After define loading screen layout, what we need to do next is to implement the loading process. This include two parts:
- Load some resources in background thread
- Update progressbar, when all resources are loaded, we will change app to MainMenu screen.(UI Thread)
AsyncTask is a suitable class for this task. AsyncTask allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by 3 generic types, calledParams
,Progress
andResult
, and 4 steps, calledonPreExecute
,doInBackground
,onProgressUpdate
andonPostExecute
.
Let’s see how to implement loading process using AsyncTask.
1 | private class LoadingResourceTask extends AsyncTask<Void, Integer, Void> { |
Code is straightforward, we extends AsyncTask class and override methods to do different tasks. We populate loading screen view in onPreExecute()
, load resource in doInBackground()
, update UI in onProgressUpdate
, and in onPostExecute
we remove load screen view, and populate our MainMenu view.
The last things we have to do is to execute LoadingResourceTask when our Main Activity starts, like below:1
2
3
4
5
6
7
8
9
10
11protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fLayout = new FrameLayout(this);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
PowerManager pManager = (PowerManager) this.getSystemService(POWER_SERVICE);
wakeLock = pManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "mainActivity");
//execute laod resource task here.
new LoadingResourceTask().execute();
}
After doing all these, you can see the loading screen same as image we saw before.
Wish this will help :)