使用Camera实现三维动画

package test.example.lxm.test;

import android.app.Activity;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        //获取屏幕大小
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        int windowWidth = displayMetrics.widthPixels;
        int windowHeight = displayMetrics.heightPixels;

        //开始动画
        int centerX = windowWidth /2;
        int centerY = windowHeight/2;
        MyAnimation myAnimation = new MyAnimation(centerX,centerY,4000);
        ImageView iv = (ImageView)findViewById(R.id.iv_test);
        iv.startAnimation(myAnimation);

    }

    class MyAnimation extends Animation{

        //中心点
        int centerX;
        int centerY;

        int duration;

        //进行三维变动
        Camera camera = new Camera();
        //各方向的位移变化量
        final float TRANS_X = 100f;
        final float TRANS_Y = -150f;
        final float TRANS_Z = 80f;
        //旋转度数
        final int ROTATE_X = 360;
        final int ROTATE_Y = 360;
        final int ROTATE_Z = 360;

        public MyAnimation(int centerX,int centerY, int duration){
            this.centerX = centerX;
            this.centerY = centerY;
            this.duration = duration;
        }

       /**
        * @param width Width of the object being animated
        * @param height Height of the object being animated
        * @param parentWidth Width of the animated object's parent
        * @param parentHeight Height of the animated object's parent
        */
        public void initialize(int width, int height, int parentWidth, int parentHeight) {
           super.initialize(width,height,parentWidth,parentHeight);
            setDuration(duration); //设置持续时间
            setFillAfter(true); //设置保留动画结果
            setInterpolator(new LinearInterpolator());
        }

        /**
         * 设置动画在每一进度所做的事情
         * @param interpolatedTime 动画行进时间比(如:动画进行到一半,interpolatedTime=0.5即1/2)
         * @param t 变化
         */
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime,t);
            camera.save();
            //位移
            camera.translate(TRANS_X - TRANS_X * interpolatedTime,TRANS_Y - TRANS_Y*interpolatedTime,
                    TRANS_Z-TRANS_Z*interpolatedTime);
            //旋转
            camera.rotateX(ROTATE_X *interpolatedTime );
            camera.rotateY(ROTATE_Y * interpolatedTime);
            camera.rotateZ(ROTATE_Z * interpolatedTime);
            //应用
            Matrix matrix = t.getMatrix();
            camera.getMatrix(matrix);
            //位移中心
            matrix.preTranslate(-centerX,-centerY);
            matrix.postTranslate(centerX,centerY);
            camera.restore();
        }



    }

}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值