ObjectAnimator 基本使用

 

 为了能让动画直接与对应控件相关联,以使我们从监听动画过程中解放出来,谷歌的开发人员在 ValueAnimator 的基础上,又派生了一个类 ObjectAnimator;该类可以使用ValueAnimator类中的所有方法。又重新写了几个方法,比如 ofInt(),ofFloat()等,其中比较关键的方法是ofFloat()。

我们先看看利用 ObjectAnimator 重写的 ofFloat 方法如何实现一个动画:(改变透明度)

(1)setAlpha 渐变动画

 ObjectAnimator animator = ObjectAnimator.ofFloat(bt_animate,"alpha",1,0,1);
        animator.setDuration(4000);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();

 

 

 

 

 

可以看到构造方法非常加单:

public static ObjectAnimator ofFloat(Object target, String propertyName, float... values)
  • 第一个参数用于指定这个动画要操作的是哪个控件
  • 第二个参数用于指定这个动画要操作这个控件的哪个属性,对应到类中的setXxxx;采用驼峰方式。
  • 第三个参数是个float的数组,如之前代码中所示,代表alpha值从1到0,又从0到1的变化。
  •  

 (2).旋转动画:

private void rotation() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(bt_animate,"rotation",0,270,180);
        animator.setDuration(4000);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }
private void rotationY() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(bt_animate,"rotationY",0,270,180);
        animator.setDuration(4000);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }

 

 

 

 

 

(3)setTranslationX,setTranslationY相应的移动动画:

ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationX", 0, 200, -200,0);  
animator.setDuration(2000);  
animator.start();  

ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationY", 0, 200, -100,0);  
animator.setDuration(2000);  
animator.start();

(4)、setScaleX 与 setScaleY

  • setScaleX(float scaleX):在 X 轴上缩放,scaleX 表示缩放倍数
  • setScaleY(float scaleY):在 Y 轴上缩放,scaleY 表示缩放倍数 我们来看看 setScaleX 的用法:
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "scaleX", 0, 3, 1);  
animator.setDuration(2000);  
animator.start();

(5)也可以自定义接口:set+驼峰式,如代码中的只要该类中有写setTransformationValue既可。

private void initAnimations(int transformDuration) {
        transformation = ObjectAnimator.ofFloat(this, "transformationValue", 0);
        transformation.setInterpolator(new DecelerateInterpolator(3));
        transformation.setDuration(transformDuration);
        transformation.addListener(new AnimatorListenerAdapter() {
            @Override public void onAnimationEnd(Animator animation) {
                transformationRunning = false;
                setIconState(animatingIconState);
            }
        });
    }

public void setTransformationValue(float value) {
        this.transformationValue = value;
        invalidateSelf();
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值