Animation简单的3D动画旋转

    我只是想实现一个简单的3D旋转动画而已,但是在网上看了那么多都是很繁琐,找了半天才找到自己要用的那部分代码。直接抽取出来:

    

import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Camera;
import android.graphics.Matrix;

public class TestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView tv = new TextView(this);
        tv.setText("3D Rotate");
        Rotate3d rotate = new Rotate3d();
        rotate.setDuration(1000);
        tv.startAnimation(rotate);
        setContentView(tv);
    }
}

class Rotate3d extends Animation {
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        Matrix matrix = t.getMatrix();
        Camera camera = new Camera();
        camera.save();

        // 设置camera动作为绕Y轴旋转
        // 总共旋转180度,因此计算在每个补间时间点interpolatedTime的角度即为两着相乘
        camera.rotateY(180 * interpolatedTime); 

        // 根据camera动作产生一个matrix,赋给Transformation的matrix,以用来设置动画效果
        camera.getMatrix(matrix);

        camera.restore();
    }
}
上述动画会转出屏幕,这时就需要对动画进行参数设置了

import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Camera;
import android.graphics.Matrix;

public class TestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView tv = new TextView(this);
        tv.setText("3D Rotate");
        Rotate3d rotate = new Rotate3d();
        rotate.setDuration(1000);
        tv.measure(0, 0);
        rotate.setCenter(tv.getMeasuredWidth() / 2, tv.getMeasuredHeight() / 2);
        rotate.setFillAfter(true); // 使动画结束后定在最终画面,如果不设置为true,则将会回到初始画面
        tv.startAnimation(rotate);
        setContentView(tv);
    }
}

class Rotate3d extends Animation {
    private float mCenterX = 0;
    private float mCenterY = 0;

    public void setCenter(float centerX, float centerY) {
        mCenterX = centerX;
        mCenterY = centerY;
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        Matrix matrix = t.getMatrix();
        Camera camera = new Camera();
        camera.save();
        camera.rotateY(180 * interpolatedTime);
        camera.getMatrix(matrix);
        camera.restore();
        matrix.preTranslate(-mCenterX, -mCenterY);
        matrix.postTranslate(mCenterX, mCenterY);
    }
}



基于谷歌官方提供的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);                            //开始动画
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值