下面是我总结的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>