1. 程式人生 > >android程式開啟動畫封裝

android程式開啟動畫封裝

本文及程式碼原創,轉載請註明出處:http://maosidiaoxian.iteye.com/blog/1682616

許多程式在開啟的時候都會有一個LOGO的展示,然後才進入。參考一個師兄的寫法,我自己也對這樣的行為進行了封裝。程式碼如下:
/*
* @(#)IntroActivity.java Project:com.sinaapp.msdxblog.androidkit
* Date:2012-9-10
*
* Copyright (c) 2011 CFuture09, Institute of Software,
* Guangdong Ocean University, Zhanjiang, GuangDong, China.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sinaapp.msdxblog.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 com.sinaapp.msdxblog.androidkit.thread.HandlerFactory;

/**
* @author Geek_Soledad (
[email protected]
)
*/
public abstract class IntroActivity extends Activity {
private static final String FLAG_RESOURCE = "FLAG_RESOURCE";
/**
* 後臺任務完成的標誌。
*/
private static final int BACKGROUND_FINISH = 0x01;
/**
* 前臺任務完成的標誌。
*/
private static final int FRONTGROUND_FINISH = 0x10;
/**
* 表示要播放開場動畫。
*/
private static final int INTRO_PLAY = 0;
/**
* 開場動畫的資源。
*/
private List<IntroImgResource> mResources = new ArrayList<IntroImgResource>();
/**
* 圖片背景顏色。預設為白色。
*/
private int mBackgroundColor = 0xFFFFFFFF;
/**
* UI執行緒。
*/
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() {
mUiHandler = new UIHandler(this);
}

/**
* 設定開場動畫的圖片資源。
*
* @param resources
* 開場動畫的圖片資源。
*/
protected abstract void setIntroResources(List<IntroImgResource> resources);

/**
* 返回下一個要啟動的Activity。
*
* @return 下一個要啟動的Activity。
*/
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.getNewHandlerInOtherThread("intro_bg").post(
new Runnable() {
@Override
public void run() {
runOnBackground();
mUiHandler.sendEmptyMessage(0x1);
}
});
}

/**
* 建立啟動時的介面Layout。
*
* @return 返回建立的介面Layout.
*/
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.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
layout.addView(mIntroImage, params);

return layout;
}

/**
* 獲取圖片背景。
*
* @return
*/
public int getBackgroundColor() {
return mBackgroundColor;
}

/**
* 設定圖片背景。
*
* @param backgroundColor
*/
public void setBackgroundColor(int backgroundColor) {
this.mBackgroundColor = backgroundColor;
}

/**
* 返回螢幕方向。
*
* @return
*/
public int getmOrientation() {
return mOrientation;
}

/**
* 設定螢幕的方向。預設是豎屏。
*
* @param mOrientation
* 螢幕方向。ActivityInfo.SCREEN_ORIENTATION_PORTRAIT或者是ActivityInfo.
* SCREEN_ORIENTATION_LANDSCAPE。
*/
public void setmOrientation(int mOrientation) {
this.mOrientation = mOrientation;
}

/**
* 在前臺中執行的程式碼。如需對介面進行橫屏的重新設定,請此在執行setmOrientation()方法。
*/
protected void runOnMainThread() {
}

/**
* 在後臺中執行的程式碼。在此進行比較耗時的操作。
*/
protected void runOnBackground() {
}

protected static class UIHandler extends Handler {
/**
* 是否需要等待。
*/
private int isWaiting = 0;
private WeakReference<IntroActivity> activity;

public UIHandler(IntroActivity activity ){
this.activity = new WeakReference<IntroActivity>(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);
activity.get().mIntroImage.setImageResource(resource.mResId);
activity.get().mIntroImage.startAnimation(animation);
return;
}

if (msg.what == BACKGROUND_FINISH || msg.what == FRONTGROUND_FINISH) {

isWaiting |= msg.what;
// 當後臺或前臺的任務未完成時,不執行Activity的跳轉。
if (isWaiting == (BACKGROUND_FINISH | FRONTGROUND_FINISH)) {
activity.get().startActivity(new Intent(activity.get(), activity.get().nextActivity()));
activity.get().finish();
return;
}
}
};
};

/**
* 開場動畫的圖片資源類。封裝了圖片、播放時間、開始時的透明程度。
*
* @author msdx
*
*/
protected class IntroImgResource implements Serializable {
/**
* 序列化ID。
*/
private static final long serialVersionUID = -2257252088641281804L;
/**
* 資源圖片ID.
*/
private int mResId;
/**
* 播放時間,單位為毫秒。
*/
private int playerTime;
/**
* 開始時的透明程度。0-1之間。
*/
private float startAlpha;

/**
* 開場動畫資源的構造方法。
*
* @param mResId
* 圖片資源的ID。
* @param playerTime
* 圖片資源的播放時間,單位為毫秒。。
* @param startAlpha
* 圖片資源開始時的透明程度。0-255之間。
*/
public IntroImgResource(int mResId, int playerTime, float startAlpha) {
super();
this.mResId = mResId;
this.playerTime = playerTime;
this.startAlpha = startAlpha;
}

/**
* 獲取資源圖片ID。
*
* @return 資源圖片ID。
*/
public int getmResId() {
return mResId;
}

/**
* 設定資源圖片ID.
*
* @param mResId
* 要設定的資源圖片ID.
*/
public void setmResId(int mResId) {
this.mResId = mResId;
}

/**
* 返回資源圖片的播放時間。
*
* @return 資源圖片的播放時間。
*/
public int getPlayerTime() {
return playerTime;
}

/**
* 設定資源圖片的播放時間。
*
* @param playerTime
* 資源圖片的播放時間。
*/
public void setPlayerTime(int playerTime) {
this.playerTime = playerTime;
}

/**
* 得到資源開始時的透明程度。
*
* @return
*/
public float getStartAlpha() {
return startAlpha;
}

/**
* 設定資源開始時的透明程度。
*
* @param startAlpha
*/
public void setStartAlpha(float startAlpha) {
this.startAlpha = startAlpha;
}
}

}


使用時繼承上面的類,主要重寫裡面的以下兩個抽象方法:
1、nextActivity(),在這裡返回下一個Activity。
2、setIntroResources(List<IntroImgResource> resources),在這裡新增要展示的圖片資源(包括展示時間)。這裡僅支援圖片,不支援動畫、影片等。展示的效果是漸變顯示。

如果有耗時的操作,重寫runOnBackground()方法,它會在一個非UI執行緒中執行。
如果要進行橫屏等的設定,重寫runOnMainThread()方法,它在主執行緒中執行。設定橫屏等行為,請使用程式碼中提供的方法。

以上程式碼來自我的開源專案cfuture-androidkit,目前託管在谷歌上,網址為:http://code.google.com/p/cfuture-androidkit/