Android開發(fā)基礎之創(chuàng)建啟動界面Splash Screen的方法
本文實例講述了Android開發(fā)基礎之創(chuàng)建啟動界面Splash Screen的方法。分享給大家供大家參考。具體如下:
啟動界面Splash Screen在應用程序是很常用的,往往在啟動界面中顯示產(chǎn)品Logo、公司Logo或者開發(fā)者信息,如果應用程序啟動時間比較長,那么啟動界面就是一個很好的東西,可以讓用戶耐心等待這段枯燥的時間。
Android 應用程序創(chuàng)建一個啟動界面Splash Screen非常簡單。比如創(chuàng)建一個工程MySample,主Acitity就叫MySample,創(chuàng)建另一個Activity叫 SplashScreen,用于顯示啟動界面,資源文件為splash.xml。至于如何制作SplashSceen界面,這不是本文章要討論的東西,就 此略過。
SplashScreen的代碼如下:
package com.ctoof.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
public class SplashScreen extends Activity {
protected boolean _active = true;
protected int _splashTime = 5000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
// 啟動主應用
startActivity(new Intent("com.ctoof.android.MySample.MyApp"));
stop();
}
}
};
splashTread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_active = false;
}
return true;
}
}
然后在AndroidMainfest.xml中修改代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ctoof.android"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SplashScreen"
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=".MyApp">
<intent-filter>
<action android:name=" com.ctoof.android. MySample.MyApp " />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
在這里負責注冊兩個活動。把負責管理啟動界面Splash Screen的活動Activity作為應用程序的主活動,然后在SplashScreen中負責啟動MyApp。
希望本文所述對大家的Android程序設計有所幫助。
相關文章
Android RecyclerView上拉加載和下拉刷新(基礎版)
這篇文章主要為大家詳細介紹了Android RecyclerView上拉加載和下拉刷新的相實現(xiàn)方法,內(nèi)容簡單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android編程實現(xiàn)自定義分享列表ACTION_SEND功能的方法
這篇文章主要介紹了Android編程實現(xiàn)自定義分享列表ACTION_SEND功能的方法,結合實例形式詳細分析了自定義分享列表功能的步驟與具體操作技巧,需要的朋友可以參考下2017-02-02
Android自定義網(wǎng)絡連接工具類HttpUtil
這篇文章主要介紹了Android自定義網(wǎng)絡連接工具類HttpUtil,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Android App中使用ViewPager實現(xiàn)滑動分頁的要點解析
這篇文章主要介紹了Android App中使用ViewPager實現(xiàn)滑動分頁的要點解析,還附帶了一個禁止ViewPager左右滑動的方法,需要的朋友可以參考下2016-06-06

