Last active
October 23, 2016 14:17
-
-
Save Kvest/8136a0adb1abd2b9694fe7bdb2e6d026 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@TargetApi(Build.VERSION_CODES.KITKAT) | |
public class TextColorTransition extends Transition { | |
private static final String PROPNAME_TEXT_COLOR = "kvest:textColorTransition:textColor"; | |
private static final String[] TRANSITION_PROPERTIES = {PROPNAME_TEXT_COLOR}; | |
public TextColorTransition() { | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public TextColorTransition(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Nullable | |
@Override | |
public String[] getTransitionProperties() { | |
return TRANSITION_PROPERTIES; | |
} | |
@Override | |
public void captureEndValues(@NonNull TransitionValues transitionValues) { | |
captureValues(transitionValues); | |
} | |
@Override | |
public void captureStartValues(@NonNull TransitionValues transitionValues) { | |
captureValues(transitionValues); | |
} | |
private void captureValues(@NonNull TransitionValues transitionValues) { | |
if (transitionValues.view instanceof TextView) { | |
int color = ((TextView) transitionValues.view).getCurrentTextColor(); | |
transitionValues.values.put(PROPNAME_TEXT_COLOR, color); | |
} | |
} | |
@Nullable | |
@Override | |
public Animator createAnimator(@NonNull ViewGroup sceneRoot, @Nullable TransitionValues startValues, | |
@Nullable TransitionValues endValues) { | |
if (startValues == null || endValues == null) { | |
return null; | |
} | |
final Integer startTextColor = (Integer)startValues.values.get(PROPNAME_TEXT_COLOR); | |
final Integer endTextColor = (Integer)endValues.values.get(PROPNAME_TEXT_COLOR); | |
final TextView textView = (TextView)endValues.view; | |
final ArgbEvaluator argbEvaluator = new ArgbEvaluator(); | |
ValueAnimator animator = ValueAnimator.ofFloat(0, 1.0f); | |
animator.addUpdateListener(animation -> { | |
int color = (Integer)argbEvaluator.evaluate(animation.getAnimatedFraction(), startTextColor, endTextColor); | |
textView.setTextColor(color); | |
}); | |
return animator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment