在button在底部的多個view切換<實例二3D切換> 代碼結構不甚合理,也不利於實現我後面計劃實現的另外一個功能,先這裡拋磚引玉一下–及通過touch屏幕,也可以實現3D切換。經過考慮之後,決定將代碼重構為:
1.將View的操作借助一個單實例的MainViewmanager分離開來;
2.實現真正意義上的MVC;
3.繼續使用原來的索引也即表驅動的方式來管理view的添加和刪除等;
在重構之後,感覺代碼的結構更簡潔清晰,希望大傢指點批評,下面貼上重構之後的代碼:
MainActivity.java:
view plain
package com.isomobile.widgets;
import android.app.ActivityGroup;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends ActivityGroup implements View.OnClickListener {
private int mCurId = 0;
private MainViewManager mViewManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewManager = MainViewManager.getInstance();
mViewManager.setupViews(this);
}
@Override
public void onClick(View v) {
final int id = v.getId();
if (mCurId == id) {
return;
}
mCurId = id;
mViewManager.processViews(this, id);
mViewManager.onRotateAnimation(mViewManager.getCurButtonIndex(id));
}
}
MainViewManager.java:
view plain
package com.isomobile.widgets;
import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainViewManager {
public static String EXTRA_NAME_BUTTON_INDEX = "buton.index";
public final static Class<?>[] sActivityClasses = {
Activity1.class, Activity2.class, Activity3.class, Activity4.class, Activity5.class
};
public final static int[] sResIds = {
R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5
};
public final static String[] sActivityIds = {
"Activity1", "Activity2", "Activity3", "Activity4", "Activity5"
};
private final static int DISTENCE_X = 240, DISTENCE_Y = 0;
private final static int ROTATE_ANIMATION_DURATION = 300;
private int mPreBtnPos, mCurBtnPos = 0;
private RelativeLayout mViewContainer;
private View mPreView;
private View[] mCurView = new View[sResIds.length];
private Button[] mBtns = new Button[sResIds.length];
private static MainViewManager mInstance = new MainViewManager();
private MainViewManager() {
}
public static MainViewManager getInstance() {
return mInstance;
}
public void setupViews(Context context) {
mViewContainer = (RelativeLayout) ((Activity) context).findViewById(R.id.container);
final Button[] btns = mBtns;
for (int i = 0; i < btns.length; i++) {
btns[i] = (Button) ((Activity) context).findViewById(sResIds[i]);
btns[i].setOnClickListener((OnClickListener) context);
}
// 第一次啟動時,默認跳轉到第一個activity
mCurView[0] = ((ActivityGroup) context).getLocalActivityManager().startActivity(
sActivityIds[0],
new Intent(context, sActivityClasses[0]).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
mViewContainer.addView(mCurView[0]);
mPreView = mCurView[0];
mPreBtnPos = 0;
}
public int getCurButtonIndex(int rid) {
final int length = sResIds.length;
for (int i = 0; i < length; i++) {
if (rid == sResIds[i]) {
return i;
}
}
return 0;
}
public void processViews(Context context, int rid) {
mViewContainer.removeAllViews();
mCurBtnPos = getCurButtonIndex(rid);
final Intent intent = new Intent(context, sActivityClasses[mCurBtnPos]);
intent.putExtra(EXTRA_NAME_BUTTON_INDEX, mCurBtnPos);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mCurView[mCurBtnPos] = ((ActivityGroup) context).getLocalActivityManager().startActivity(
sActivityIds[mCurBtnPos], intent).getDecorView();
}
public void onRotateAnimation(int index) {
if (mPreBtnPos > mCurBtnPos) {
Rotate3d.rightRotate(mPreView, mCurView[index], DISTENCE_X, DISTENCE_Y,
ROTATE_ANIMATION_DURATION, new AnimListener());
} else {
Rotate3d.leftRotate(mPreView, mCurView[index], DISTENCE_X, DISTENCE_Y,
ROTATE_ANIMATION_DURATION, new AnimListener());
}
mPreView = mCurView[index];
mViewContainer.removeAllViews();
mViewContainer.addView(mCurView[index]);
mPreBtnPos = mCurBtnPos;
}
private final static class AnimListener implements Animation.AnimationListener {
public void onAnimationEnd(Animation animation) {
// 可以設置buttons的背景或者狀態(是否可點擊等)
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
// 可以設置buttons的背景或者狀態(是否可點擊等)
}
}
}
作者“androidzhaoxiaogang的專欄”