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

建议在看实例二之前参看一下实例1。

1. 镶嵌View的主ActivityGroup

package com.isomobile.widgets;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends ActivityGroup implements View.OnClickListener {
    private final static Class<?>[] sActivityClasses = {
            Activity1.class, Activity2.class, Activity3.class, Activity4.class, Activity5.class
    };

    private final static int[] sResIds = {
            R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5
    };

    private final static String[] sActivityIds = {
            "Activity1", "Activity2", "Activity3", "Activity4", "Activity5"
    };

    private RelativeLayout mViewContainer;

    private Button[] mBtns = new Button[sResIds.length];

    private View mPreView;

    private View[] mCurView = new View[sResIds.length];

    private int mCurId = 0;

    private int mPreBtnPos, mCurBtnPos = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupViews();
    }

    private void setupViews() {
        mViewContainer = (RelativeLayout) findViewById(R.id.container);
        final Button[] btns = mBtns;
        for (int i = 0; i < btns.length; i++) {
            btns[i] = (Button) findViewById(sResIds[i]);
            btns[i].setOnClickListener(this);
        }

        //第一次启动时,默认跳转到第一个activity
        mCurView[0] = getLocalActivityManager().startActivity(
                sActivityIds[0],
                new Intent(MainActivity.this, sActivityClasses[0])
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
        mViewContainer.addView(mCurView[0]);
        mPreView = mCurView[0];
        mPreBtnPos = 0;
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if (mCurId == id) {
            return;
        }
        mCurId = id;
        processViews(id);
        onRotateAnimation(getCurButtonIndex(id));
    }

    private void processViews(int rid) {
        mViewContainer.removeAllViews();
        mCurBtnPos = getCurButtonIndex(rid);
        mCurView[mCurBtnPos] = getLocalActivityManager().startActivity(
                sActivityIds[mCurBtnPos],
                new Intent(this, sActivityClasses[mCurBtnPos])
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    }

    private 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 onRotateAnimation(int index) {
        if (mPreBtnPos > mCurBtnPos) {
            Rotate3d.rightRotate(mPreView, mCurView[index], 240, 0, 300, new AnimListener());
        } else {
            Rotate3d.leftRotate(mPreView, mCurView[index], 240, 0, 300, new AnimListener());
        }

        mPreView = mCurView[index];
        mViewContainer.removeAllViews();
        mViewContainer.addView(mCurView[index]);
        mPreBtnPos = mCurBtnPos;
    }

    private class AnimListener implements Animation.AnimationListener {

        public void onAnimationEnd(Animation animation) {
          //可以设置buttons的背景或者状态(是否可点击等)
        }

        public void onAnimationRepeat(Animation animation) {

        }

        public void onAnimationStart(Animation animation) {
          //可以设置buttons的背景或者状态(是否可点击等)
        }
    }
}

2. Rotate3d.class:

package com.isomobile.widgets;

import android.view.View;

public class Rotate3d {
    private Rotate3d() {
    }

    public static void leftRotate(View layoutFrom, View layoutTo, int centerX, int centerY,
            int duration, android.view.animation.Animation.AnimationListener animationListener) {
        final Animation3d fromAnimation = new Animation3d(0.0F, -90F, 0.0F, 0.0F, centerX, centerY);
        final Animation3d toAnimation = new Animation3d(90F, 0.0F, 0.0F, 0.0F, centerX, centerY);
        fromAnimation.setDuration(duration);
        toAnimation.setDuration(duration);
        toAnimation.setAnimationListener(animationListener);
        layoutFrom.startAnimation(fromAnimation);
        layoutTo.startAnimation(toAnimation);
        layoutTo.setVisibility(0);
        layoutFrom.setVisibility(8);
    }

    public static void rightRotate(View layoutFrom, View layoutTo, int centerX, int centerY,
            int duration, android.view.animation.Animation.AnimationListener animationListener) {
        final Animation3d fromAnimation = new Animation3d(0.0F, 90F, 0.0F, 0.0F, centerX, centerY);
        final Animation3d toAnimation = new Animation3d(-90F, 0.0F, 0.0F, 0.0F, centerX, centerY);
        fromAnimation.setDuration(duration);
        toAnimation.setDuration(duration);
        toAnimation.setAnimationListener(animationListener);
        layoutFrom.startAnimation(fromAnimation);
        layoutTo.startAnimation(toAnimation);
        layoutTo.setVisibility(0);
        layoutFrom.setVisibility(8);
    }
}

3. Animation3d.class:

package com.isomobile.widgets;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class Animation3d extends Animation {
    private final float mFromDegree;

    private final float mToDegree;

    private final float mCenterX;

    private final float mCenterY;

    private final float mLeft;

    private final float mTop;

    private Camera mCamera;

    public Animation3d(float fromDegree, float toDegree, float left, float top, float centerX,
            float centerY) {
        mFromDegree = fromDegree;
        mToDegree = toDegree;
        mLeft = left;
        mTop = top;
        mCenterX = centerX;
        mCenterY = centerY;
    }

    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCamera = new Camera();
    }

    protected void applyTransformation(float interpolatedTime, Transformation t) {
        final float FromDegree = mFromDegree;
        float degrees = FromDegree + (mToDegree - mFromDegree) * interpolatedTime;
        final float centerX = mCenterX;
        final float centerY = mCenterY;
        Matrix matrix = t.getMatrix();
        if (degrees <= -76F) {
            degrees = -90F;
            mCamera.save();
            mCamera.rotateY(degrees);
            mCamera.getMatrix(matrix);
            mCamera.restore();
        } else if (degrees >= 76F) {
            degrees = 90F;
            mCamera.save();
            mCamera.rotateY(degrees);
            mCamera.getMatrix(matrix);
            mCamera.restore();
        } else {
            mCamera.save();
            mCamera.translate(0.0F, 0.0F, centerX);
            mCamera.rotateY(degrees);
            mCamera.translate(0.0F, 0.0F, -centerX);
            mCamera.getMatrix(matrix);
            mCamera.restore();
        }
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}

整个工程源代码见:  http://download.csdn.net/detail/androidzhaoxiaogang/3624450  (注意将processViews里面的addView方法去掉,不小心add了2次,特此说明。)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值