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

来源:互联网 发布:淘宝网阿里旺旺打不开 编辑:程序博客网 时间:2024/06/14 07:20
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(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfloat 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
0 0
原创粉丝点击