如何使用 Object Animator 制作放大动画

Arc*_*nes 4 android android-animation objectanimator

当使用动画时,我可以做这样的事情

ScaleAnimation animation = new ScaleAnimation(0, 1.0, 0, 1.0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Run Code Online (Sandbox Code Playgroud)

从 0 缩放到对象的原始大小。

我怎样才能对ObjectAnimatoror做同样的事情ValueAnimator

Tom*_*kis 6

您可以对ValueAnimator使用类似的东西:

ValueAnimator translate = ValueAnimator.ofFloat(1f, 1.5f);
translate.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float scale = Float.parseFloat(animation.getAnimatedValue().toString());
        yourView.setScaleX(scale);
        yourView.setScaleY(scale);
    }
});
translate.start();
Run Code Online (Sandbox Code Playgroud)

对于ObjectAnimator来说是这样的:

AnimatorSet animationSet = new AnimatorSet();

ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.5f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.5f);

animationSet.playTogether(scaleX, scaleY);
animationSet.start();
Run Code Online (Sandbox Code Playgroud)
您还可以为两个动画设置持续时间/插值器/延迟和类似属性。另外不要忘记配置后启动动画。

注意:没有测试此代码,有些东西可能无法正常工作。