Android游戲閃屏實現步驟詳解
下面是我總結的Android閃屏的經驗,供大家參考,以下代碼,可以直接粘貼,稍作修改就好
1.導入一張圖片,我起名叫sp.png
2.在res/layout目錄下創建splashy.xml加入如下代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sp" />
</LinearLayout>
3.創建一個名為SplashyDemo.java的類,加入如下代碼:
package com.example.splashydemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class SplashyDemo extends Activity {
private long splashyTime = 3000;//閃屏停留時間
private boolean isStop =false;//閃屏暫停
private boolean isActivity = true;//是否跳過閃屏直接進入主Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 隱藏標題
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);// 設置全屏
setContentView(R.layout.splashy);
Thread splashyThread = new Thread()
{
public void run()
{
try {
long ms = 0;
while(isActivity && ms <splashyTime)
{
sleep(100);
if(!isStop)
{
ms+=100;
}
Log.i("TAG",ms+"");
}
//加入此 會去配置文件AndroidManifest.xml找對應的com.google.app.splashy.CLEARSPLASH,有此標識的Activity是閃屏后切換的界面
startActivity(new Intent("com.google.app.splashy.CLEARSPLASH"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
splashyThread.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
isStop = true;
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
isStop = false;
}
}
4.在AndroidManifest.xml文件中進行配置
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
//這里是我的主Activity的路徑
android:name="com.example.splashydemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.google.app.splashy.CLEARSPLASH"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
<activity
android:name="com.example.splashydemo.SplashyDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>