Optimization of button switching among multiple views at the bottom & lt; instance two 3D switching & gt;

Source: Internet
Author: User

The code structure of multiple view switches at the bottom of the button <instance 2 3D switch> is not reasonable, and it is not conducive to implementing another function planned to be implemented later, let's talk about it first-and through touch screen, you can also implement 3D switching. After consideration, I decided to refactor the code:
1. Separate View operations with the MainViewmanager of a single instance;
2. Implement MVC in the true sense;
3. Continue to manage view addition and deletion using the original index, that is, table-driven;
 
After refactoring, I feel that the code structure is more concise and clear. I hope you can give some suggestions and post the restructured code below:
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 );
}
 
// The first activity is redirected to the first activity by default.
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 ){
// You can set the background or status of buttons (whether to click or not)
}
 
Public void onAnimationRepeat (Animation animation ){
 
}
 
Public void onAnimationStart (Animation animation ){
// You can set the background or status of buttons (whether to click or not)
}
}
}

Author: "androidzhaoxiaogang column"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.