在ViewFlipper中使用Rotate3dAnimation旋转切换界面效果

Rotate3dAnimation是google api demo中copy出来的,具体的例子在Views/Animation/3D Transition中。
这里我就直接贴了:

package com.ata.util;

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

/**
 * An animation that rotates the view on the Y axis between two specified angles.
 * This animation also adds a translation on the Z axis (depth) to improve the effect.
 */
public class Rotate3dAnimation extends Animation {
    private final float mFromDegrees;
    private final float mToDegrees;
    private final float mCenterX;
    private final float mCenterY;
    private final float mDepthZ;
    private final boolean mReverse;
    private Camera mCamera;

    /**
     * Creates a new 3D rotation on the Y axis. The rotation is defined by its
     * start angle and its end angle. Both angles are in degrees. The rotation
     * is performed around a center point on the 2D space, definied by a pair
     * of X and Y coordinates, called centerX and centerY. When the animation
     * starts, a translation on the Z axis (depth) is performed. The length
     * of the translation can be specified, as well as whether the translation
     * should be reversed in time.
     *
     * @param fromDegrees the start angle of the 3D rotation
     * @param toDegrees the end angle of the 3D rotation
     * @param centerX the X center of the 3D rotation
     * @param centerY the Y center of the 3D rotation
     * @param reverse true if the translation should be reversed, false otherwise
     */
    public Rotate3dAnimation(float fromDegrees, float toDegrees,
            float centerX, float centerY, float depthZ, boolean reverse) {
        mFromDegrees = fromDegrees;
        mToDegrees = toDegrees;
        mCenterX = centerX;
        mCenterY = centerY;
        mDepthZ = depthZ;
        mReverse = reverse;
    }

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

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
    	final float fromDegrees = mFromDegrees;
        float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);

        final float centerX = mCenterX;
        final float centerY = mCenterY;
        final Camera camera = mCamera;

        final Matrix matrix = t.getMatrix();

        camera.save();
        if (mReverse) {
            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
        } else {
            camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
        }
        camera.rotateY(degrees);
        camera.getMatrix(matrix);
        camera.restore();

        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}


接下来就是要在ViewFlipper中使用它了,
首先在布局中加入2个view,类似:
<ViewFlipper 
        android:id="@+id/viewFlipper"
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        >
        <include layout="@layout/layout_0"/>
        <include layout="@layout/layout_1"/>
    </ViewFlipper>

然后通常在代码中怎样写:
我这里是按一个按钮将2个ListView翻来覆去的3D旋转显示。

View.OnClickListener onClickListener=new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			float halfWidth=viewFlipper.getWidth()/2.0f;
			float halfHeight=viewFlipper.getHeight()/2.0f;
			int duration=500;
			int depthz=0;//viewFlipper.getWidth()/2;
			if(btn_rightTop.getText().equals("日历视图")){
				btn_rightTop.setText("分类视图");
				
				Rotate3dAnimation in=new Rotate3dAnimation(-90, 0,halfWidth,halfHeight,depthz,false);  
				in.setDuration(duration);  
				in.setStartOffset(duration);
				viewFlipper.setInAnimation(in);  
				Rotate3dAnimation out=new Rotate3dAnimation(0, 90,halfWidth,halfHeight,depthz,false);  
				out.setDuration(duration);  
				viewFlipper.setOutAnimation(out);  
				viewFlipper.showNext();  				
			}else{
				btn_rightTop.setText("日历视图");
				
				Rotate3dAnimation in=new Rotate3dAnimation(90, 0,halfWidth,halfHeight,depthz,false);  
				in.setDuration(duration);  
				in.setStartOffset(duration);
				viewFlipper.setInAnimation(in);  
				Rotate3dAnimation out=new Rotate3dAnimation(0, -90,halfWidth,halfHeight,depthz,false);  
				out.setDuration(duration);  
				viewFlipper.setOutAnimation(out);  
				viewFlipper.showPrevious();  
			}
		}
		
	};

这样一个简单的3D旋转效果就出来了。就是感觉景深效果不太好,比较生硬,没iphone自然。

随手势滑动的3D旋转效果
http://www.eoeandroid.com/thread-203645-1-1.html

android 类似微信的摇一摇实现
http://www.cnblogs.com/jxgxy/archive/2012/08/29/2662138.html
基于谷歌官方提供的3D翻转示例进行修改,修复了在不同设备上显示效果差异过大的问题。项目地址:https://github.com/GcsSloop/Rotate3dAnimation 效果图:如何使用:// 计算心点(这里是使用view心作为旋转心点)         final float centerX = view.getWidth() / 2.0f;                 final float centerY = view.getHeight() / 2.0f;        //括号内参数分别为(上下文,开始角度,结束角度,x轴心点,y轴心点,深度,是否扭曲)         final Rotate3dAnimation rotation = new Rotate3dAnimation(this, start, end, centerX, centerY, 1.0f, true);         rotation.setDuration(1500);                               //设置动画时长         rotation.setFillAfter(true);                              //保持旋转效果         rotation.setInterpolator(new AccelerateInterpolator());   //设置插值器         rotation.setAnimationListener(new AnimationListener() {   //设置监听器             @Override             public void onAnimationStart(Animation animation) {             }            @Override             public void onAnimationRepeat(Animation animation) {             }            @Override             public void onAnimationEnd(Animation animation) {             }         });         view.startAnimation(rotation);                            //开始动画
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值