Android动画学习心得

在Android3.0以后推出了属性动画以取代之前的四种基础动画。

为什么要用属性动画而不用之前的基础动画呢,首先看两者的区别。

比如一个imageview添加了监听事件,在使用了基础动画右移100dp后,点击右移后的imageview是不会触发监听事件的,所以基础动画只是不断地重绘imageview的图片

而当imageview使用属性动画右移100dp后,点击右移后的imageview会触发监听事件,而点击原来的位置并不会,这就大大方便了动画的开发


首先来看下属性动画的一些基本用法:

 ObjectAnimator animator = ObjectAnimator.ofFloat(imglist.get(i), "translationY", i * 150f,0f);

可以看出 ObjectAnimator.ofFloat方法需要传递四个参数

第一个参数是要进行动画的view,第二个参数是字符串,指定要进行动画的属性(理论上任何属性都可以),第三第四个属性是指定属性要变化的范围

同时

animator.setDuration(500);//设定动画持续时长,以ms为单位
            animator.start();//启动动画
属性动画还有其他的一个特性,在google官方给出的api中提供了多种动画效果,称为差值器

animator.setInterpolator(new BounceInterpolator());//回弹效果

动画效果分别有

AccelerateInterpolator  加速插值器

DecelerateInterpolator 减速插值器

 AccelerateDecelerateInterpolator  加速减速插值器

LinearInterpolator 线性插值器

BounceInterpolator 弹跳插值器

AnticipateInterpolator 回荡秋千插值器

使用方法在

animator.setInterpolator(new BounceInterpolator())参数中new出来一个就可

同时,动画可以添加监听事件,这里推荐使用<pre style="background-color:#ffffff;color:#000000;font-family:'Consolas';font-size:12.0pt;"><span style="background-color:#e4e4ff;">AnimatorListenerAdapter</span>
因为它可以自主选择监听哪些事件,例如监听结束事件
animator.setInterpolator(new BounceInterpolator());//回弹效果
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    Toast.makeText(MainActivity.this,"展开动画已经结束",Toast.LENGTH_SHORT).show();
                }
            });


 同时,AnimatorSet可以很好地控制动画效果: 

AnimatorSet set=new AnimatorSet();
            set.playTogether(animator,animator1);
            set.playSequentially(animator,animator1);
            set.play(animator).with(animator1).after(animator2).before(animator);

有一起播放,有按顺序播放,也有with,after,bofore等。可以很好地控制动画效果



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值