具有两个比例的 Android 弹跳动画

Kon*_*pko 0 android android-animation

我想要第一个我的布局放大,第二个它应该通过反弹缩小。但是这段代码不能正常工作。

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially" >
<set>
    <scale
        android:duration="400"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.2"
        android:toYScale="1.2" />
</set>

<set android:interpolator="@android:anim/bounce_interpolator" >
    <scale
        android:duration="800"
        android:fromXScale="1.2"
        android:fromYScale="1.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set></set>
Run Code Online (Sandbox Code Playgroud)

Rez*_*adi 5

我对对象动画师了解不多,但我使用属性动画进行了多次缩放,效果很好!这是代码

    AnimatorSet set = new AnimatorSet(); //this is your animation set. 
    //add as many Value animator to it as you like

    ValueAnimator scaleUp = ValueAnimator.ofFloat(1,(float)1.2);
    scaleUp.setDuration(800);
    scaleUp.setInterpolator(new BounceInterpolator()); //remove this if you prefer default interpolator
    scaleUp.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            Float newValue = (Float) valueAnimator.getAnimatedValue();
            yourView.setScaleY(newValue);
            yourView.setScaleX(newValue);
        }
    });

    ValueAnimator scaleDown = ValueAnimator.ofFloat((float)1.2,1);
    scaleDown.setDuration(800);
    scaleDown.setInterpolator(new BounceInterpolator());
    scaleDown.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            Float newValue = (Float) valueAnimator.getAnimatedValue();
            yourView.setScaleY(newValue);
            yourView.setScaleX(newValue);
        }
    });


    set.play(saceleUp);
    set.play(scaleDown).after(scaleUP);
    set.start();
Run Code Online (Sandbox Code Playgroud)

  • 您可能更喜欢使用 xml 执行此操作,但我认为这更好,原因有两个。1)你写了太多行代码,但你的代码并不复杂!事实上,如果您学习了 value animator 的基础知识(不到 15 分钟),您就会对它非常满意!2)它让你完全控制动画,当动画很复杂时它真的很方便。无论如何,如果你喜欢 xml,我的意见是去吧。 (2认同)