使用XML定义时,翻译动画效果非常好,而且只有代码完美一次 - Android

时间:2012-07-02 15:45:12

标签: android android-layout android-animation

我遇到了这个奇怪的问题。基本上我用翻译动画制作一个视图。 (转换到屏幕并通过2个不同的事件输出)我的翻译动画代码是:

    final Animation  animtopOut = new TranslateAnimation(0, 0, 0, -mainHeaderlayout.getMeasuredHeight());
                    animtopOut.setDuration(500);
                    animtopOut.setFillAfter(true);
mainHeaderlayout.setAnimation(animtopOut);

xml代码是:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/accelerate_interpolator" >

<translate 
    android:fromYDelta="0%p"
    android:toYDelta="-99%p"
    android:duration="600"
    android:fillAfter="true">

</translate>
</set>

使用代码进行设置:

final Animation animtopOut = AnimationUtils.loadAnimation(mContext, R.anim.header_animate_out);

当我触发动画时,如果我使用xml动画属性,它可以正常工作。 问题是当我通过代码使用它时。这就是我想要的。它仅在第一次使用翻译动画时运行。第二次,当它被触发时,视图在屏幕内部没有动画。如果我遗漏任何财产,请有人帮助我。感谢。


编辑:(额外信息)

实际上有两种不同的动画通过两个不同的事件在同一视图上触发。我实际上发布了一个动画属性。另一个几乎是一样的。只是价值观是不同的。

1 个答案:

答案 0 :(得分:4)

您是否尝试过像这样的动画配置

animtopOut.setRepeatCount(Animation.INFINITE);

animtopOut.setRepeatMode(Animation.RESTART);

animtopOut.setInterpolator(new LinearInterpolator());