Pro Android学习笔记(一一十):2D动画(5):Camera

文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处http://blog.csdn.net/flowingflying以及作者@恺风Wei

利用Camera类可以为View的2D动画提供更好的视觉效果,并能在一定程度上模仿简单的3D效果。下面,我们利用Camera类实现View从原至近(z轴)不断放大,同时围绕Y轴旋转的效果。

小例子的代码片段

public class ViewAnimationCamera extends Animation{
    private float centerX,centerY;
    private Camera camera = null;     
    public ViewAnimationCamera(){
        camera = new Camera();
    }
   
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) { 
        super.initialize(width, height, parentWidth, parentHeight);
        centerX = width/2.0f;
        centerY = height/2.0f;
        setDuration(2500);
        setFillAfter(true);
        setInterpolator(new LinearInterpolator());

    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
        final Matrix matrix = t.getMatrix(); 
       //保存状态,与restore()成对出现
        camera.save(); 
        //三个参数分别代表x,y,z的坐标,随着时间,从(0,0,1300)向(0,0,0) 变化,实现从远到近的效果。对于z轴(景深)变化,已经提供了透视的效果,无需scale设置。
        camera.translate(0.0f, 0.0f, 1300 * (1.0f- interpolatedTime));
        //围绕Y轴作一周(360度)的旋转
        camera.rotateY(360*interpolatedTime);
        //将camera产生的效果加诸于参数matrix上
        camera.getMatrix(matrix);
 
        //下面两行代码,标识动画中心点是中央,前面已经详细讨论过。 
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
 
        //在使用camera设置动画之前,进行了camera.save(),将状态恢复到保存的状态。
        camera.restore();

    }

}

 

小例子代码在:Pro Android学习:2D动画小例子

相关链接:我的Android开发相关文章

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值