Android动画只能运行一次?

时间:2014-10-22 00:06:56

标签: android animation layout views

所以我试图同时做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);

那我怎么解决这个问题呢?很抱歉,如果我的解释很糟糕,我发现很难解释所有细节,请询问任何遗漏的信息。

1 个答案:

答案 0 :(得分:0)

使用此帖Android: Using ObjectAnimator to translate a View with fractional values of the View's dimension

中的解决方案解决

并不漂亮,因为我不得不创建单独的翻译X并为我需要移动的每个视图翻译Y res文件..结果与15个anim文件一样。仍然想知道是否有更好的解决方案