自定义 View 3d翻转动画 android

由于最近有个一翻转图片(view)的需求,写了个简单的翻转动画不多说上代码:

自定义的animation需要重写的方法有:

 1. public void initialize(int width, int height, int parentWidth, int parentHeight)  初始化,按英文名字大家应该能猜到

 2.protected void applyTransformation(float interpolatedTime, Transformation t) 实现自定义效果

public class FlipAnimation extends Animation {
    private float fromDegree;   // 旋转起始角度
    private float toDegree;     // 旋转终止角度
    private float centerX = 0;
    private float centerY = 0;
    Camera camera = new Camera();

    public FlipAnimation(float fromDegree , float toDegree){
        this.fromDegree = fromDegree;
        this.toDegree = toDegree;
    }


    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        centerX = width / 2;
        centerY = height / 2;
 
    }


    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        //旋转角度, interpolatedTime为时间插值(范围为0-1)第一个参数就是通过getTransformation函数传递 反复调用此函数 不断刷新 view的信息达到动画效果
        float degree = fromDegree + (toDegree - fromDegree) * interpolatedTime;
	//此矩阵大家肯定不陌生, 三维矩阵 图像学中 通过此矩阵进行各种图像变换
        Matrix matrix = t.getMatrix();
         //跟canvas类似
        camera.save();
        // camera平移 达到缩放view效果, 注意camera默认位置(0,0,-8) location才能设置camera位置
        //camera.translate(-0.0f, 0.0f,10.0f);
	//做旋转变换
        camera.rotateY(degree);
        camera.getMatrix(matrix);
        camera.restore();
	//将(0,0)tranlate前、后中心点  具体参见http://www.360doc.com/content/14/0610/21/16623487_385518125.shtml
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);

    }
}


调用方法如下:

public class MainActivity extends ActionBarActivity {
    private ImageView imageView1=null;
    private Animation animation2;
    private Animation animation1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView1= (ImageView) this.findViewById(R.id.imageView1);
        imageView1.postDelayed(new Runnable() {
            @Override
            public void run() {
                playFlipAnim(imageView1);
            }
        },1000);
    }
    private void playFlipAnim(final ImageView view){
        FlipAnimation flipAinm = new FlipAnimation(0f, 90f);
        flipAinm.setFillAfter(true);
        flipAinm.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setImageResource(R.drawable.image2);
                //playFlip2Anim(view);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        flipAinm.setInterpolator(new AccelerateInterpolator());
        flipAinm.setDuration(2000);
        view.clearAnimation();
        view.startAnimation(flipAinm);
    }
    private void playFlip2Anim(ImageView view){
        FlipAnimation flipAinm = new FlipAnimation(270f, 360f);
        flipAinm.setFillAfter(true);
        flipAinm.setInterpolator(new AccelerateInterpolator());
        flipAinm.setDuration(2000);
        view.clearAnimation();
        view.startAnimation(flipAinm);
    }


}
总结由于camera 位置在(0,0,-8)位置 如view 的区域过大会造成一定的区域裁剪(view在翻转过程中显示不全),可以设置camera位置,不知大家有更好的建议没?


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值