简体   繁体   English

Android动画只能播放一次?

[英]Android animation only works once?

So i'm trying to do 2 animations simultaneously, one to move a textview, and one to show a linearlayout (also 2 animations to hide them). 所以我想同时做2个动画,一个移动文本视图,一个显示线性布局(也2个动画来隐藏它们)。 I have another animation working as intended to show/hide a seperate layout. 我有另一个动画正在按计划显示/隐藏单独的布局。 When i execute showing the view with 2 animations, it works once, it hides fine, but then doesn't work again. 当我执行显示带有2个动画的视图时,它只能工作一次,它可以很好地隐藏,但随后又无法工作了。 Then when i show the other view, it plays all 3 animations (not intended). 然后,当我显示另一个视图时,它将播放所有3个动画(并非预期)。 I can't figure out why this is happening? 我不知道为什么会这样? When i try to show the 2 animations it does nothing, but then when i try the other show view its like it was added to a queue and it shows all 3. 当我尝试显示2个动画时,它什么也不做,但是当我尝试其他显示视图时,就像将其添加到队列中并显示所有3个动画一样。

My code for initiating the two animations: 我用于启动两个动画的代码:

        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);

My code for hiding the two animations: 隐藏两个动画的代码:

    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);

Then this is the other animation that is working fine, it's only one animation, no textView, just a layout 然后这是另一个效果很好的动画,它只是一个动画,没有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);

So how can i fix this? 那么我该如何解决呢? Sorry if my explanation is bad i'm finding it difficult to explain all the details, please ask for any missing information. 抱歉,如果我的解释不好,我很难解释所有细节,请索取所有丢失的信息。

solved by using solution in this post Android: Using ObjectAnimator to translate a View with fractional values of the View's dimension 通过使用本文中的解决方案来解决Android:使用ObjectAnimator转换具有View维度的分数值的View

wasn't pretty as I had to create seperate translate X and translate Y res files for each view I need to move.. ended up with like 15 anim files. 并不是很漂亮,因为我必须为需要移动的每个视图创建单独的X转换和Y转换文件。最终得到了15个动画文件。 Would still like to know if theres a better solution 仍然想知道是否有更好的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM