Android动画--属性动画Property Animation

简介

属性动画包含:
ObjectAnimator   动画的执行类
ValueAnimator    动画的执行类
AnimatorSet      用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
AnimatorInflater 用户加载属性动画的xml文件

ObjectAnimator 单一属性动画

缩放X轴:ScaleX
ObjectAnimator().ofFloat(imageView,”ScaleY”,1.0f,0.0f).setDuration(3000).start();

缩放Y轴:ScaleY
ObjectAnimator().ofFloat(imageView,”ScaleX”,1.0f,0.0f).setDuration(3000).start();

旋转动画(X轴):rotationX
ObjectAnimator().ofFloat(imageView,”rotationX”,0.0f,360f).setDuration(3000).start();

旋转动画(Y轴):rotationY
ObjectAnimator().ofFloat(imageView,”rotationY”,0.0f,360f).setDuration(3000).start();

旋转动画(中心点):rotation
ObjectAnimator().ofFloat(imageView,”rotation”,0.0f,360f).setDuration(3000).start();

淡入淡出:alpha
ObjectAnimator().ofFloat(imageView,”alpha”,1.0f,0.0f).setDuration(3000).start();

位移动画(X轴):
ObjectAnimator().ofFloat(imageView,”translationX”,0.0f,100.0f).setDuration(3000);

位移动画(Y轴):
ObjectAnimator().ofFloat(imageView,”translationY”,0.0f,100.0f).setDuration(3000);

代码实现如下
                imageView.clearAnimation();
                ObjectAnimator objectAnimator=new ObjectAnimator().ofFloat(imageView,"translationX",0.0f,100.0f,0.0f,100.0f).setDuration(3000);
                objectAnimator.start();
在xml中实现
  • 首先需要在res文件夹下新建animator文件夹,在其中定义xml文件如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000">
    <objectAnimator
        android:propertyName="scaleX"
        android:repeatCount="-1"
        android:valueFrom="0.0f"
        android:valueTo="1.0f"></objectAnimator>
</set>
  • 相应的代码中实现如下
                Animator animatorObjext = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
                animatorObjext.setTarget(imageView2);
                animatorObjext.start();

这里写图片描述

ValueAnimator

  • 代码实现如下
                imageView2.clearAnimation();
                imageView2.setImageResource(R.mipmap.ic_launcher);
                //这里的属性值可以根据需要设置多个
                ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.0f, 1.0f, 0.0f, 1.0f);//设置属性值
                animator.setTarget(imageView2);//设置操作对象
                animator.setDuration(5000).start();//动画开始
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        imageView2.setScaleY((Float) animation.getAnimatedValue());//设置Y轴上的变化
                        imageView2.setScaleX((Float) animation.getAnimatedValue());//设置X轴上的变化
                    }
                });

这里写图片描述

PropertyValuesHolder 组合动画

  • 就是设置多个不同的动画,同时让他们加载就好了
  • 相应的代码实现
                imageView.clearAnimation();
                PropertyValuesHolder scaleX=PropertyValuesHolder.ofFloat("scaleX",1,0,1);
                PropertyValuesHolder scaleY=PropertyValuesHolder.ofFloat("scaleY",1,0,1);
                PropertyValuesHolder rotattion=PropertyValuesHolder.ofFloat("rotation",0,360);
                ObjectAnimator objectAnimator2=new ObjectAnimator().ofPropertyValuesHolder(imageView,scaleX,scaleY,rotattion).setDuration(3000);
                objectAnimator2.start();

这里写图片描述

AnimatorSet 组合动画(可以设置动画的顺序)

  • 相应的代码如下
                ObjectAnimator objectAnimator3=new ObjectAnimator().ofFloat(imageView,"translationX",0.0f,100.0f,0.0f,100.0f).setDuration(3000);
                ObjectAnimator objectAnimator4=new ObjectAnimator().ofFloat(imageView,"scaleX",1,0,1).setDuration(3000);
                ObjectAnimator objectAnimator5=new ObjectAnimator().ofFloat(imageView,"rotation",0,360).setDuration(3000);
                AnimatorSet animatorSet=new AnimatorSet();
                //设置动画同时执行
                animatorSet.playTogether(objectAnimator3,objectAnimator4);
                //设置动画的执行顺序
                animatorSet.play(objectAnimator5).after(objectAnimator3);
                animatorSet.start();

这里写图片描述]

AnimatorInflater 加载属性动画的xml文件

  • xml还是在res文件夹下新建的animator文件夹下的xml文件
  • 相应的xml文件的定义如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together"
    android:duration="1000">
    <!-- ordering 指动画是一起播放还是一个接一个播放-->
    <objectAnimator
        android:propertyName="scaleX"
        android:valueFrom="0.0"
        android:valueTo="1.0"></objectAnimator>
    <objectAnimator
        android:propertyName="scaleY"
        android:valueFrom="0.0"
        android:valueTo="1.0"></objectAnimator>
</set>
  • 相应的逻辑代码如下
                imageView2.clearAnimation();
                imageView2.setImageResource(R.mipmap.ic_launcher);
                Animator animatorMy = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animatorinflater);
                animatorMy.setTarget(imageView2);
                animatorMy.start();

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值