您的位置:首页 > 移动开发 > Android开发

如何实现Android应用的启动画面(闪屏)?

2015-11-03 18:02 405 查看
     Android的启动画面通常用来显示用户的某种形式的进度,在应用程序加载完全之前。有些人使用应用程序的启动动画,只是为了在几秒钟内显示其程序应用和公司的LOGO。不幸的是,和IOS相比,在Android中,我们没有任何内在的机制来显示启动画面。在本教程中,我们将学习如何实现Android应用程序的闪屏。

启动画面的应用场景

启动画面的目的,取决于应用程序的要求。在本教程中,我将讲解实现闪屏的两种实现场景。第一个是使用Timer来显示启动画面,第二个是在进行网络HTTP调用这需要一些时间来获取所需的信息时显示启动画面。这两个教程都是一样的,除了闪屏活动。为了实现闪屏,我们要创建一个单独Activity,一旦这个Activity关闭,我们就启动我们的主Activity.所以首先让我们创建一个新的项目.

1.使用Timer的Android启动画面

1)首先为启动画面创建一个单独的Activity,在新建的项目里新建一个类,命名为SplashScreen.Java.

2)打开你的AndroidManifest.xml文件,让SplashScreen Activity作为启动的Activity.

<!-- Splash screen -->
<activity
android:name="com.androidsplashscreentimer.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- Main activity -->
<activity
android:name="com.androidsplashscreentimer.MainActivity"
android:label="@string/app_name" >
</activity>
3)为SplashScreen在res目录下创建一个layout文件,命名为activity_splash.xml,这个Layout只包含你的应用LOGO和公司LOGO.

activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background" >

<ImageView
android:id="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/wwe_logo" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="12dp"
android:textColor="#454545"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true" />

</RelativeLayout>

4)在SplashScreen.Java Activity中添加以下代码,在下面的代码中Handler被用于等待特定的时间,一旦timer时间用完,我们就启动主Activity.

SplashScreen.java
package com.androidsplashscreentimer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

new Handler().postDelayed(new Runnable() {

/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/

@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);

// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}

}


5)启动这个程序,你将会看到3s的闪屏,而且之后你的主Activity也会被启动.

接下来是第二个场景,我们的应用程序在进入主Activity之前会进行一些网络请求.在这里所有的步骤都与之前相同,除了SplashScreen.Java的代码.在SplashScreen.Java的onCreat()方法中我将使用Asynctask方法请求网络获取需要的信息.一旦网络请求终止,就在onPostExecute()方法中启动主Activity.

2.进行网络请求时的Android闪屏

1)依照上面的步骤创建SplashScreen.Java类.
2)在AndroidManifest.xml文件中添加 INTERNET 权限.     
<span style="white-space:pre">	</span><uses-permission android:name="android.permission.INTERNET"/>

3)在SplashScreen.Java中添加AsyncTask方法请求网络.在本教程中将通过HTTP调用来获取JSON并用log形式显示在屏幕.获取Json数据之后通过Intent传值到MainActivity中.
    
SplashScreen.java
package info.androidhive.androidsplashscreentimer;

import info.androidhive.androidsplashscreennetwork.R;
import info.androidhive.network.JsonParser;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class SplashScreen extends Activity {

String now_playing, earned;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

/**
* Showing splashscreen while making network calls to download necessary
* data before launching the app Will use AsyncTask to make http call
*/
new PrefetchData().execute();

}

/**
* Async Task to make http call
*/
private class PrefetchData extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
// before making http calls

}

@Override
protected Void doInBackground(Void... arg0) {
/*
* Will make http call here This call will download required data
* before launching the app
* example:
* 1. Downloading and storing in SQLite
* 2. Downloading images
* 3. Fetching and parsing the xml / json
* 4. Sending device information to server
* 5. etc.,
*/
JsonParser jsonParser = new JsonParser();
String json = jsonParser
.getJSONFromUrl("http://api.androidhive.info/game/game_stats.json");

Log.e("Response: ", "> " + json);

if (json != null) {
try {
JSONObject jObj = new JSONObject(json)
.getJSONObject("game_stat");
now_playing = jObj.getString("now_playing");
earned = jObj.getString("earned");

Log.e("JSON", "> " + now_playing + earned);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// After completing http call
// will close this activity and lauch main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.putExtra("now_playing", now_playing);
i.putExtra("earned", earned);
startActivity(i);

// close this activity
finish();
}

}

}


MainActivity.java
package com.androidsplashscreentimer;

import com.androidsplashscreennetwork.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

LinearLayout llStats;
TextView txtPlayCount, txtEarned;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

llStats = (LinearLayout) findViewById(R.id.llStats);
txtPlayCount = (TextView) findViewById(R.id.txtNowPlaying);
txtEarned = (TextView) findViewById(R.id.txtEarned);

// layout background transparent
llStats.getBackground().setAlpha(150);
llStats.setVisibility(View.VISIBLE);

Intent i = getIntent();
String now_playing = i.getStringExtra("now_playing");
String earned = i.getStringExtra("earned");

// Diplaying the text
txtPlayCount.setText(now_playing);
txtEarned.setText(earned);
}
}






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android splash