当前位置:操作系统 > 安卓/Android >>

button在底部的多个view切换优化之<实例二3D切换>

在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.

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,