Text color animation

前端 未结 8 1836
南旧
南旧 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.

    0 讨论(0)
  • 2020-12-14 10:55

    You can use new Property Animation Api for color animation:

    Integer colorFrom = getResources().getColor(R.color.red);
    Integer colorTo = getResources().getColor(R.color.blue);
    ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
    colorAnimation.addUpdateListener(new AnimatorUpdateListener() {
    
        @Override
        public void onAnimationUpdate(ValueAnimator animator) {
            textView.setTextColor((Integer)animator.getAnimatedValue());
        }
    
    });
    colorAnimation.start();
    

    For backward compatability with Android 2.x use Nine Old Androids library from Jake Wharton.

    0 讨论(0)
提交回复
热议问题