Text color animation

前端 未结 8 1832
南旧
南旧 2020-12-14 10:09

Is there a way to animate a text color change (from anycolor to white)?

The only variant I came up with, is placing two textviews (with the same text) in one place,

8条回答
  •  执念已碎
    2020-12-14 10:51

    No need to keep handles to the two text views. First add the fadeIn/fadeOut animations:

    textSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
    textSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
    

    then:

    TextView currentTextView = (TextView)(textSwitcher.getNextView().equals(
      textSwitcher.getChildAt(0)) ? 
      textSwitcher.getChildAt(1) : textSwitcher.getChildAt(0)
    );
    // setCurrentText() first to be the same as newText if you need to
    textSwitcher.setTextColor(fadeOutColor);
    ((TextView) textSwitcher.getNextView()).setTextColor(Color.WHITE);
    textSwitcher.setText(newText);
    

    Just implemented it like this so proven to work.

提交回复
热议问题