【问题标题】:How to repeat an android xml animation [duplicate]如何重复android xml动画[重复]
【发布时间】:2019-07-11 18:23:08
【问题描述】:

我尝试为片段中的文本视图设置动画以飞入 - 在那里停留几秒钟 - 飞出 - 重复。动画有效,但我不让它重复它。当动画结束时,AnimationListener 会触发 onAnimationEnd 事件。我试过setRepeatCount(Animation.INFINITE);setRepeatMode(Animation.RESTART/Animation.REVERSE);,但似乎没有任何效果。

在这里可以看到带有动画内容的 text_slider_animation.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true"
    android:interpolator="@android:anim/linear_interpolator">

    <set
        android:fillAfter="true"
        android:fillEnabled="true"
        android:startOffset="500"
        android:duration="1000">
        <translate
            android:fromXDelta="-100%"
            android:toXDelta="0%" />
        <alpha
            android:fromAlpha="0.0"
            android:toAlpha="1.0" />
    </set>

    <set
        android:fillAfter="true"
        android:fillEnabled="true"
        android:startOffset="6000"
        android:duration="1000">
        <translate
            android:fromXDelta="0%"
            android:toXDelta="100%" />
        <alpha
            android:fromAlpha="1.0"
            android:toAlpha="0.0" />
    </set>
</set>

我尝试使用以下代码:

AnimationSet mAnimation = (AnimationSet) AnimationUtils.loadAnimation(getActivity(), R.anim.text_slider_animation);
mAnimation.setRepeatCount(Animation.INFINITE);
mAnimation.setRepeatMode(Animation.RESTART);
myTextView.startAnimation(mAnimation);
mAnimation.setAnimationListener(new Animation.AnimationListener() {
    public void onAnimationStart(Animation animation) {}
    public void onAnimationRepeat(Animation animation) {
        Toast.makeText(getActivity(), "Repeat", Toast.LENGTH_SHORT).show();
        // TODO: Replace text in TextView
    }
    public void onAnimationEnd(Animation animation) {
        Toast.makeText(getActivity(), "Finish", Toast.LENGTH_SHORT).show();
    }
});

有人可以帮助我为什么这不起作用吗?提前谢谢!

/////////////////////

编辑:我更正了 mAnimation.setRepeatCount(Animation.INFINITE); mAnimation.setRepeatMode(Animation.RESTART);

【问题讨论】:

  • 我看到了这篇文章,但不幸的是无法解决我的问题

标签: java android xml android-layout animation


【解决方案1】:

在您的问题中,您将 mode 设置为 count 并将 count 设置为 mode

setRepeatCount(Animation.INFINITE); 
setRepeatMode(Animation.RESTART);
// or setRepeatMode(Animation.REVERSE);

编辑:

这里似乎有些混乱。您正在创建 AnimationSet 而不是 Animation。不幸的是,AnimationSet 似乎不像Animation 那样支持重复。

所以你需要在onAnimationEnd 回调中调用animationSet.start()。有关更多详细信息,请查看此答案。 How to repeat an AnimationSet with sequentially added animations

onAnimationEnd 中调用view.startAnimation(animationSet) 似乎也可以。

【讨论】:

  • 哎呀,看来我已经睡着了;)不过,它也不适用于 mAnimation.setRepeatCount(Animation.INFINITE); mAnimation.setRepeatMode(Animation.RESTART/Animation.REVERSE); onAnimationEnd 仍然会触发
  • 知道为什么它仍然不能与更正的上两行一起工作吗? onAnimationRepeat 仍然没有触发。我尝试了许多具有重复模式和计数的星座
  • 你用的是Animation.RESTART还是Animation.REVERSE
  • 两者都不起作用。什么都没发生。 onAnimationEnd 被调用,就是这样。
  • 两者一起?
【解决方案2】:

在 XML 中,添加:

android:repeatMode="restart"

【讨论】:

  • 我试过了,它仍然会触发“结束”事件并且不会重复(将其添加到 root-set-tag 中)。
  • 你想重复多少次?因为每次动画完成时,都会调用 onAnimationEnd
  • 我想重复一遍,片段将被销毁。我明白了,每次都应该调用 onAnimationEnd,但 onAnimationRepeat 永远不会触发。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多