Android 属性动画(ObjectAnimator)

属性动画: 控制属性来实现动画。
特点:最为强大的动画,弥补了补间动画的缺点,实现位置+视觉的变化。并且可以自定义插值器,实现各种效果

移动

移动分为沿x、y轴移动,沿x轴时使用translationX,沿y轴移动使用translationY。
translationX:下个位置大于上个上个位置时,向右移动,反之向左移动;
translationY:下个位置大于上个上个位置时,向下移动,反之向上移动。

//创建动画对象
//"translationX" X轴
ObjectAnimator translationX=ObjectAnimator.ofFloat(mImageView,"translationX",0,300);
//"translationY" Y轴
ObjectAnimator translationY=ObjectAnimator.ofFloat(mImageView,"translationY",300,0);

//设置动画时长(执行X轴)
translationX(translationY).setDuration(3000);
//启动动画
translationX(translationY).start();

旋转

0f -> 360f ,顺时针; 360f -> 0f,逆时针。

//创建动画对象
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f, 0f);
//设置动画时长
animator.setDuration(2000);
//启动动画
animator.start();

透明度

透明度由0~1表示,0表示完全透明,1表示不透明

//创建动画对象
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f);
//设置动画时长
animator.setDuration(1000);
//启动动画
animator.start();

缩放

1f-> 2f,放大成原来的两倍;2f-> 1f,从两倍变为原样。

//创建动画对象
//"scaleY" X
ObjectAnimator animatorX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f, 1f);
//"scaleY" Y
ObjectAnimator animatorY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f, 1f);

//设置动画时长
animatorX(或animatorY ).setDuration(2000);
//启动动画
animatorX(或animatorY ).start();

组合动画

组合动画需要借助AnimatorSet这个类来完成,AnimatorSet提供了play(Animator anim)方法,传入Animator对象之后返回一个AnimatorSet.Builder实例,AnimatorSet.Builder中有下面几个方法:
with(Animator anim):将现有的动画和传入的动画同时执行。
before(Animator anim):将现有的动画在传入的动画之前执行。
after(Animator anim):将现有的动画在传入的动画之后执行。
after(long delay):将现有的动画延时执行。

//沿x轴放大
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f, 1f);
//沿y轴放大
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f, 1f);
//移动
ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f, 0f);
//透明动画
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
//同时沿X,Y轴放大,且改变透明度,然后移动
animatorSet.play(scaleXAnimator).with(scaleYAnimator).with(animator).before(translationXAnimator);
//都设置3s,也可以为每个单独设置
animatorSet.setDuration(3000);
animatorSet.start();

监听事件

    //添加监听事件
    animatorSet.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            //动画开始的时候调用
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            //画结束的时候调用
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            //动画被取消的时候调用
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            //动画重复执行的时候调用

        }
    });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 6,284评论 1 38
  • 文章转载至郭神的博客 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们...
    DanielHan阅读 949评论 0 52
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,385评论 25 709
  • 动画基础概念 动画分类 Android 中动画分为两种,一种是 Tween 动画、还有一种是 Frame 动画。 ...
    Rtia阅读 1,273评论 0 6
  • 关联子查询就是应对分组情况下的标量子查询的比较。定义外查询是 s1定义内查询是 s2让被查询的列,s1.列名=s2...
    Yix1a阅读 369评论 0 0