本文所述实例为在android中开起开场动画类,已封装好,进行android开发的朋友可使用。在这个类中,你可以:设置开场动画的图片资源、返回下一个要启动的Activity、显示开场动画、执行耗时的操作、创建启动时的界面Layout、设置屏幕的方向。默认是竖屏、开场动画的图片资源类。封装了图片、播放时间、开始时的透明程度等。
具体实现代码如下:
package com.lurencun.cfuture09.androidkit.ui;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.frameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import com.lurencun.cfuture09.androidkit.thread.HandlerFactory;
public abstract class IntroActivity extends Activity {
private static final String FLAG_RESOURCE = "FLAG_RESOURCE";
private static final byte BACKGROUND_FINISH = 0x01;
private static final byte FRONTGROUND_FINISH = 0x10;
private static final int INTRO_PLAY = 0;
private List mResources;
private int mBackgroundColor = 0xFFFFFFFF;
private Handler mUiHandler;
private ImageView mIntroImage;
private int mOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
runonMainThread();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setRequestedOrientation(mOrientation);
this.setContentView(createLayout());
setIntroResources(mResources);
startonBackground();
showIntro();
}
private void init() {
mResources = new ArrayList();
mUiHandler = new UIHandler(this);
}
protected abstract void setIntroResources(List resources);
protected abstract Class> nextActivity();
protected void showIntro() {
int delayTime = 0;
for (final IntroImgResource resource : mResources) {
Message msg = new Message();
msg.what = INTRO_PLAY;
Bundle data = new Bundle();
data.putSerializable(FLAG_RESOURCE, resource);
msg.setData(data);
mUiHandler.sendMessageDelayed(msg, delayTime);
delayTime += resource.playerTime;
}
mUiHandler.sendEmptyMessageDelayed(FRONTGROUND_FINISH, delayTime);
}
private void startonBackground() {
HandlerFactory.newHandlerInOtherThread("intro_bg").post(
new Runnable() {
@Override
public void run() {
runonBackground();
mUiHandler.sendEmptyMessage(0x1);
}
});
}
private View createLayout() {
frameLayout layout = new frameLayout(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layout.setLayoutParams(layoutParams);
layout.setBackgroundColor(getBackgroundColor());
mIntroImage = new ImageView(this);
frameLayout.LayoutParams params = new frameLayout.LayoutParams(
frameLayout.LayoutParams.FILL_PARENT,
frameLayout.LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
layout.addView(mIntroImage, params);
return layout;
}
public int getBackgroundColor() {
return mBackgroundColor;
}
public void setBackgroundColor(int backgroundColor) {
this.mBackgroundColor = backgroundColor;
}
public int getmOrientation() {
return mOrientation;
}
public void setmOrientation(int mOrientation) {
this.mOrientation = mOrientation;
}
protected void runonMainThread() {
}
protected void runonBackground() {
}
protected static class UIHandler extends Handler {
private int isWaiting = 0;
private WeakReference activity;
public UIHandler(IntroActivity activity) {
this.activity = new WeakReference(activity);
}
public void handleMessage(android.os.Message msg) {
if (msg.what == INTRO_PLAY) {
IntroImgResource resource = (IntroImgResource) msg.getData()
.getSerializable(FLAG_RESOURCE);
AlphaAnimation animation = new AlphaAnimation(
resource.startAlpha, 1f);
animation.setDuration(resource.playerTime);
IntroActivity intro = activity.get();
if (intro != null) {
if (resource.isExpand) {
intro.mIntroImage.setScaleType(ScaleType.FIT_XY);
} else {
intro.mIntroImage.setScaleType(ScaleType.CENTER);
}
intro.mIntroImage.setImageResource(resource.mResId);
intro.mIntroImage.startAnimation(animation);
}
return;
}
if (msg.what == BACKGROUND_FINISH || msg.what == FRONTGROUND_FINISH) {
isWaiting |= msg.what;
// 当后台或前台的任务未完成时,不执行Activity的跳转。
if (isWaiting == (BACKGROUND_FINISH | FRONTGROUND_FINISH)) {
IntroActivity intro = activity.get();
if (intro != null) {
intro.startActivity(new Intent(intro, intro
.nextActivity()));
intro.finish();
}
}
}
};
};
protected class IntroImgResource implements Serializable {
private static final long serialVersionUID = -2257252088641281804L;
private int mResId;
private int playerTime;
private float startAlpha;
private boolean isExpand;
public IntroImgResource(int mResId, int playerTime, float startAlpha, boolean isExpand) {
super();
this.mResId = mResId;
this.playerTime = playerTime;
this.startAlpha = startAlpha;
this.isExpand = isExpand;
}
public int getmResId() {
return mResId;
}
public void setmResId(int mResId) {
this.mResId = mResId;
}
public int getPlayerTime() {
return playerTime;
}
public void setPlayerTime(int playerTime) {
this.playerTime = playerTime;
}
public float getStartAlpha() {
return startAlpha;
}
public void setStartAlpha(float startAlpha) {
this.startAlpha = startAlpha;
}
public boolean isExpand() {
return isExpand;
}
public void setExpand(boolean isExpand) {
this.isExpand = isExpand;
}
}
}
本实例配备了详细的注释说明,读者可以在了解程序代码功能的基础上进行个性化的修改,打造出自己独具个性的Android开场动画!



