我感觉, 预期的效果应该是在主Activity中调用一个类,传入splash图片和显示时间即可实现欢迎界面的功能。于是封装了一个RmfSplash类,实现了简单的功能。
1、layout/rmf_ly_splash.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/rmf_id_splash_img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center" />
</LinearLayout>
2、cn.rydiy.rmf.common.RmfSplash.java
package cn.rydiy.rmf.common;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
/**
* Splash Display Class. It will display splash with full screen and then quit full screen.
* @author Rydiy
* @version 1.0.0
* @Date 2012-07-29
*/
public class RmfSplash {
private Activity mAct; //MainActivity
private int mTime; //delay time
private Handler mHandler; //message handler
private int mLayoutMain; //R.layout.main
private int mLayoutSplash; //R.layout.rmf_ly_splash
private int mIdImgView; //R.id.rmf_id_splash_img
/**
* Constructor
* @param mainActivity : your object's main activity's instance
* @param resrc : a Resources instance for your application's package
*/
public RmfSplash(Activity mainActivity, Resources resrc) {
this.mAct = mainActivity;
//get resources
String packageName = mAct.getPackageName();
//get R.layout.main
mLayoutMain = resrc.getIdentifier("main", "layout", packageName);
//get R.layout.rmf_ly_splash
mLayoutSplash = resrc.getIdentifier("rmf_ly_splash", "layout", packageName);
//get R.id.rmf_id_splash_img
mIdImgView = resrc.getIdentifier("rmf_id_splash_img", "id", packageName);
//create Handler
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.arg1 == 1) {
//quit full screen
final WindowManager.LayoutParams attrs = mAct.getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
mAct.getWindow().setAttributes(attrs);
mAct.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); //set main layout
mAct.setContentView(mLayoutMain);
}
}
};
}
/**
* Show splash
* @param id : The resource id of the image data
* @param time : Delay time (ms)
*/
public void show(int id, int time) {
Log.d("RMF", "RmfSplash show()");
mTime = time;
//no title
mAct.requestWindowFeature(Window.FEATURE_NO_TITLE);
//full screen
mAct.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set splash layout
mAct.setContentView(mLayoutSplash);
//load bitmap
Bitmap bmp = BitmapFactory.decodeResource(mAct.getResources(), id);
ImageView img = (ImageView)mAct.findViewById(mIdImgView);
img.setImageBitmap(bmp);
//delay time
new Thread() {
public void run() {
Message msg = Message.obtain(mHandler);
msg.arg1 = 1;
mHandler.sendMessageDelayed(msg, mTime);
};
}.start();
}
}
3、使用
RmfSplash rSplash = new RmfSplash(this, getResources());
rSplash.show(R.drawable.rmf_img_splash, 3000);