Android动画机制 视图动画

Animation框架定义了透明度、旋转、缩放和位移等这几种常见的动画。
实现原理:每次绘制视图时,View所在的ViewGoup中的drawChild()函数获取该View的Transformation值,然后调用 canvas.concat(transformToApply.getMatrix()),通过矩阵运算完成动画帧,如果动画没有完成,就继续调用invalitate()函数,启动下次绘制来驱动动画,从而完成整个动画的绘制。
实现的方式:XML和代码控制。

代码控制:
透明度动画
AlphaAnimation alpha = new AlphaAnimation(0,1);//透明度由透明到不透明
alpha.setDuration(1000);
image.startAnimation(alpha);
旋转动画:
/*四个参数为 第一个为起始坐标的角度,
第二个为旋转的角度
后两个值确定了旋转中心
*/// RotateAnimation rotate = new RotateAnimation(0,360,100,100);
//以这个控件自身的一半为X,(0.5f)…..以这个控件的自身的一半为Y(竖直方向)
RotateAnimation rotate = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);

rotate.setDuration(1000);//动画持续时间
image.startAnimation(rotate);

位移动画:
为视图移动时增加位移动画.

//参数1~2:x轴的开始位置
//参数3~4:y轴的开始位置
//参数5~6:x轴的结束位置
//参数7~8:y轴的结束位置
TranslateAnimation translate = new TranslateAnimation
(Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0.5f); //以自身这个控件控件的一半为y
translate.setDuration(1000);//动画时间
image.startAnimation(translate);

缩放动画:为视图的缩放添加动画
/**
//缩放动画,x坐标从1f->2f,y坐标从1f->2f。缩放的轴是相对于自己的一半,等于是自己的中心
ScaleAnimation scale = new ScaleAnimation(1f, 2f, 1f, 2f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scale.setDuration(1000);
set.addAnimation(scale);

    http://www.tuicool.com/articles/vIBrIr

*/
动画集合 AnimationSet,将动画以组合的形式展示出来。
//动画集合
private void animationSet() {

AnimationSet as = new AnimationSet(true);
as.setDuration(1000);
AlphaAnimation aa = new AlphaAnimation(0,1);
aa.setDuration(1000);
as.addAnimation(aa);
RotateAnimation ra = new RotateAnimation
(0,-360,RotateAnimation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
ra.setDuration(1000);
as.addAnimation(ra);

    TranslateAnimation ta = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF,0f,
            Animation.RELATIVE_TO_SELF,0.5f,
            Animation.RELATIVE_TO_SELF,0f,
            Animation.RELATIVE_TO_SELF,0.5f);

    ta.setDuration(1000);
    as.addAnimation(ta);
    image.startAnimation(as);//设置开始动画

补充(来自 http://www.tuicool.com/articles/vIBrIr
set.setStartOffset(1000);//一秒后再执行动画 = 等待1秒后执行动画
set.setFillAfter(true);//设置动画执行后保持最后状态
set.setFillBefore(false);//设置动画执行后不回到原来状态
set.setRepeatCount(3);//设置动画重复执行的次数

}

对于动画事件,android也提供了对应的监听回调,要添加相应的监听方法。
translate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//动画开始
}

        @Override
        public void onAnimationEnd(Animation animation) {
    //动画结束
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
    //重复事件
        }
    });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值