基本使用
自定義一個Activity繼承TSplashActivity
public class TestSplashActivity extends TSplashActivity {
@Override
public int getBackgroundColor() {
//當閃屏PNG圖片無法鋪滿部分機型的屏幕時沪蓬,設置與閃屏顏色配合的背景色會給用戶更好的體驗
return Color.WHITE;
}
@Override
public void onSplashStop() {
//閃屏結(jié)束后扼褪,啟動游戲的Activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
this.finish();
}
}
清單文件中設置閃屏Activity為啟動Activity
<activity
android:name=".TestSplashActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
TSplashActivity 基本實現(xiàn)
private RelativeLayout layout; //Activity的布局
private ImageView imageView;//顯示閃屏的圖片控件
private TSplashSequence sequence = new TSplashSequence();//閃屏對象隊列
//子類實現(xiàn)的設置背景顏色
public abstract int getBackgroundColor();
//子類實現(xiàn)的閃屏結(jié)束
public abstract void onSplashStop();
onCreate函數(shù)中將閃屏對象添加到隊列中
while (true) {
if (count < assetPaths.length) { //assets文件中取圖片
this.sequence.addSplash(new TAssetSpash(this.layout,
this.imageView, assetDir + "/" + assetPaths[count]));
} else { //res/drawable文件中取圖片
int id = getResources().getIdentifier(resourcePrefix + count,
"drawable", getPackageName());
if (id == 0) {
break;
}
this.sequence.addSplash(new TResSplash(this.layout,
this.imageView, id));
}
count++;
}
onResume函數(shù)中執(zhí)行sequence.play方法
this.sequence.play(this, new TSplashListener() {
public void onFinish() {
TSplashActivity.this.onSplashStop();
}
});
TSplashListener 監(jiān)聽閃屏結(jié)束
public abstract interface TSplashListener {
public abstract void onFinish();
}
TSplash 閃屏對象接口
public abstract interface TSplash {
public abstract void play(Activity paramActivity, TSplashListener paramTSplashListener);
}
TBaseImageSplash 閃屏對象的基本實現(xiàn)
public abstract class TBaseImageSplash implements TSplash {
//子類實現(xiàn)的加載圖片方法
abstract void loadImage(Activity paramActivity, ImageView paramImageView,
LoadSplashCallback paramLoadSplashCallback);
@Override
public void play(final Activity context, final TSplashListener listener) {
loadImage(context, this.imageView, new LoadSplashCallback() {
@Override
public void onLoadSuccess() {
//執(zhí)行動畫
TBaseImageSplash.this.playSplash(context, listener);
}
@Override
public void onLoadFailed() {
listener.onFinish();
}
});
}
//內(nèi)部類監(jiān)聽加載是否成功
public static abstract interface LoadSplashCallback {
public abstract void onLoadSuccess();
public abstract void onLoadFailed();
}
}
TAssetSpash 加載assets文件夾下面的圖片
public class TAssetSpash extends TBaseImageSplash {
@Override
void loadImage(final Activity context, final ImageView imageView,
final TBaseImageSplash.LoadSplashCallback callback) {
Bitmap bitmap = null;
try {
InputStream stream = context.getAssets().open(
TAssetSpash.this.assetPath);
bitmap = BitmapFactory.decodeStream(stream);
} catch (IOException e) {
Log.e("T", "load asset splash failed : "
+ TAssetSpash.this.assetPath, e);
}
if (bitmap == null) {
callback.onLoadFailed();
} else {
imageView.setImageBitmap(bitmap);
callback.onLoadSuccess();
}
}
}
TResSplash 加載drawable 下的圖片
public class TResSplash extends TBaseImageSplash {
@Override
void loadImage(Activity context, ImageView imageView,
TBaseImageSplash.LoadSplashCallback callback) {
imageView.setImageResource(this.resourceId);
callback.onLoadSuccess();
}
}
TSplashSequence 閃屏序列執(zhí)行播放
public class TSplashSequence {
private List<TSplash> list = new ArrayList<TSplash>();
public void addSplash(TSplash splash) {
this.list.add(splash);
}
public void play(Activity context, TSplashListener listener) {
play(context, listener, 0);
}
private void play(final Activity context, final TSplashListener listener,
final int index) {
if (index >= this.list.size()) {
listener.onFinish();
} else {
((TSplash) this.list.get(index)).play(context, new TSplashListener() {
public void onFinish() {
TSplashSequence.this.play(context, listener, index + 1);
}
});
}
}
}