How to animate a FadeOut and FadeIn while textView changed text

跟風遠走 提交于 2019-12-10 10:59:55

问题


i try to animate a TextView on a changeText But always see only one direction of the animation, i only see the fadeout

What i try is: beforChange = fadeOut and onChange or after fadein

here is my code in the onCreate method of my activity:

    final Animation out = new AlphaAnimation(1.0f, 0.0f);
    out.setDuration(1000);

    final Animation in = new AlphaAnimation(0.0f, 1.0f);
    in.setDuration(1000);


    bidFirst.setAnimation(out);
    bidMiddle.setAnimation(out);
    bidLast.setAnimation(out);

    TextWatcher bidWatcher = new TextWatcher() {
      public void onTextChanged(CharSequence s, int start, int before, int count) {
        in.startNow();
        bidFirst.setAnimation(out);
        bidMiddle.setAnimation(out);
        bidLast.setAnimation(out);
      }

      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        out.startNow();
        bidFirst.setAnimation(in);
        bidMiddle.setAnimation(in);
        bidLast.setAnimation(in);
      }

      public void afterTextChanged(Editable s) {
      }
    };
    bidFirst.addTextChangedListener(bidWatcher);
    bidMiddle.addTextChangedListener(bidWatcher);
    bidLast.addTextChangedListener(bidWatcher);

i think there is something wrong in my code but for my believe it has to work.

What i have now is: on every setText the changed Text only FadeOut but after the text has changed and never FadeIn!?


回答1:


TextSwitcher is exactly what you are looking for. Just use their setInAnimation() and setOutAnimation. Than the animations will run automatically if you change the text by setText()




回答2:


By the look of your code you are telling the TextView to fade out after a change rather than fade in.

Also I'm not sure how effective this code will be as beforeTextChanged gets called only moments before the text is changed. There simply wont be enough time for any animation to happen on beforeTextChanged as it will be instantly replaced by your code in onTextChanged

Edit** Reply to comment below

So to get the textview to fade out then fade in with new content I would programmatically start a fadeOut animation rather than using a Textwatcher. I would give the fadeOut animation an AnimationListener and on animationEnd you can then set the new text before starting your fadeIn animation.



来源:https://stackoverflow.com/questions/3554685/how-to-animate-a-fadeout-and-fadein-while-textview-changed-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!