【问题标题】:Android animation only works once?Android 动画只能运行一次?
【发布时间】:2014-12-17 08:52:09
【问题描述】:

所以我正在尝试同时制作 2 个动画,一个用于移动文本视图,一个用于显示线性布局(还有 2 个动画来隐藏它们)。我有另一个动画按预期显示/隐藏单独的布局。当我执行显示带有 2 个动画的视图时,它工作一次,它隐藏得很好,但随后就不再工作了。然后当我显示另一个视图时,它会播放所有 3 个动画(不打算)。我无法弄清楚为什么会这样?当我尝试显示 2 个动画时它什么也不做,但是当我尝试另一个显示视图时,它就像它被添加到队列中一样,它显示了所有 3 个。

我启动两个动画的代码:

        LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
        Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_show);
        anim.setAnimationListener(new AnimListener(layoutMsgs, View.VISIBLE)); // sets visibility on animation end
        layoutMsgs.startAnimation(anim);

        TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
        Animation tvAnim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_show);
        tvMsgs.startAnimation(tvAnim);

我隐藏两个动画的代码:

    LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
    Animation animLayout = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_hide);
    animLayout.setAnimationListener(new AnimListener(layoutMsgs, View.INVISIBLE));
    layoutMsgs.startAnimation(animLayout);

    TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
    Animation animMsgs = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_hide);

    tvMsgs.startAnimation(animMsgs);

然后这是另一个运行良好的动画,它只有一个动画,没有 textView,只是一个布局

        LinearLayout pokeLayout = (LinearLayout)findViewById(R.id.layoutPokes);
        Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.poke_show);
        anim.setAnimationListener(new AnimListener(pokeLayout, View.VISIBLE));
        pokeLayout.startAnimation(anim);

那么我该如何解决这个问题?抱歉,如果我的解释不好,我发现很难解释所有细节,请询问任何缺失的信息。

【问题讨论】:

    标签: android animation layout views


    【解决方案1】:

    通过使用这篇文章中的解决方案Android: Using ObjectAnimator to translate a View with fractional values of the View's dimension解决了

    不是很漂亮,因为我必须为我需要移动的每个视图创建单独的翻译 X 和翻译 Y res 文件。最终得到了大约 15 个动画文件。还是想知道有没有更好的解决办法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 2013-02-12
      • 2019-07-28
      • 2016-05-28
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      相关资源
      最近更新 更多