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

Android 3D旋转动画效果

这篇文章主要介绍一下如何实现View的3D旋转效果,实现的主要原理就是围绕Y轴旋转,同时在Z轴方面上有一个深入的缩放。

演示的demo主要有以下几个重点:

1,自定义旋转动画

2,动画做完后,重置ImageView

 


先看一下程序的运行效果:

 

\

1,自定义动画类


这里实现了一个Rotate3dAnimation的类,它扩展了Animation类,重写applyTransformation()方法,提供指定时间的矩阵变换,我们在这个方法里,就可以利用Camera类得得到一个围绕Y轴旋转的matrix,把这个matrix设置到Transformation对象中。  具体的实现代码如下:


[java]  @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); 

@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);
}

2,如何使用这个动画类


在Activity中,我们有两个大小一样的ImageView,它们都放在FrameLayout中,这样他们位置是重叠的,对最上面的ImageView做动画(旋转角度从0到90),当动画做完后,再对后面的ImageView做动画(旋转角度从90到180),在这里,要控制相应的ImageView隐藏或显示。

动画的listener实现如下:

 


[java]  private final class DisplayNextView implements Animation.AnimationListener { 
 
        public void onAnimationStart(Animation animation) { 
        } 
 
        public void onAnimationEnd(Animation animation) { 
 
            mContainer.post(new SwapViews()); 
        } 
 
        public void onAnimationRepeat(Animation animation) { 
        } 
    } 

private final class DisplayNextView implements Animation.AnimationListener {

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {

            mContainer.post(new SwapViews());
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }

动画做完后,执行的代码如下:

 

[java]  private final class SwapViews implements Runnable 
    { 
        @Override 
        public void run() 
        { 
            mImageView1.setVisibility(View.GONE); 
            mImageView2.setVisibility(View.GONE); 
 
            mIndex++; 
            if (0 == mIndex % 2) 
            { 
                mStartAnimView = mImageView1; 
            } 
            else 
            { 
                mStartAnimView = mImageView2; 
            } 
 
            mStartAnimView.setVisibility(View.VISIBLE); 
            mStartAnimView.requestFocus(); 
 
            Rotate3dAnimation rotation = new Rotate3dAnimation( 
                    -90, 
                    0, 
               &nb

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