in Unsplash/app/src/main/java/com/example/android/unsplash/transition/TextResize.java [106:222]
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
TransitionValues endValues) {
if (startValues == null || endValues == null) {
return null;
}
final TextResizeData startData = (TextResizeData) startValues.values.get(DATA);
final TextResizeData endData = (TextResizeData) endValues.values.get(DATA);
if (startData.gravity != endData.gravity) {
return null; // Can't deal with changes in gravity
}
final TextView textView = (TextView) endValues.view;
float startFontSize = (Float) startValues.values.get(FONT_SIZE);
// Capture the start bitmap -- we need to set the values to the start values first
setTextViewData(textView, startData, startFontSize);
final float startWidth = textView.getPaint().measureText(textView.getText().toString());
final Bitmap startBitmap = captureTextBitmap(textView);
if (startBitmap == null) {
startFontSize = 0;
}
float endFontSize = (Float) endValues.values.get(FONT_SIZE);
// Set the values to the end values
setTextViewData(textView, endData, endFontSize);
final float endWidth = textView.getPaint().measureText(textView.getText().toString());
// Capture the end bitmap
final Bitmap endBitmap = captureTextBitmap(textView);
if (endBitmap == null) {
endFontSize = 0;
}
if (startFontSize == 0 && endFontSize == 0) {
return null; // Can't animate null bitmaps
}
// Set the colors of the TextView so that nothing is drawn.
// Only draw the bitmaps in the overlay.
final ColorStateList textColors = textView.getTextColors();
final ColorStateList hintColors = textView.getHintTextColors();
final int highlightColor = textView.getHighlightColor();
final ColorStateList linkColors = textView.getLinkTextColors();
textView.setTextColor(Color.TRANSPARENT);
textView.setHintTextColor(Color.TRANSPARENT);
textView.setHighlightColor(Color.TRANSPARENT);
textView.setLinkTextColor(Color.TRANSPARENT);
// Create the drawable that will be animated in the TextView's overlay.
// Ensure that it is showing the start state now.
final SwitchBitmapDrawable drawable = new SwitchBitmapDrawable(textView, startData.gravity,
startBitmap, startFontSize, startWidth, endBitmap, endFontSize, endWidth);
textView.getOverlay().add(drawable);
// Properties: left, top, font size, text color
final PropertyValuesHolder leftProp =
PropertyValuesHolder.ofFloat("left", startData.paddingLeft, endData.paddingLeft);
final PropertyValuesHolder topProp =
PropertyValuesHolder.ofFloat("top", startData.paddingTop, endData.paddingTop);
final PropertyValuesHolder rightProp = PropertyValuesHolder.ofFloat("right",
startData.width - startData.paddingRight, endData.width - endData.paddingRight);
final PropertyValuesHolder bottomProp = PropertyValuesHolder.ofFloat("bottom",
startData.height - startData.paddingBottom, endData.height - endData.paddingBottom);
final PropertyValuesHolder fontSizeProp =
PropertyValuesHolder.ofFloat("fontSize", startFontSize, endFontSize);
final ObjectAnimator animator;
if (startData.textColor != endData.textColor) {
final PropertyValuesHolder textColorProp = PropertyValuesHolder.ofObject("textColor",
new ArgbEvaluator(), startData.textColor, endData.textColor);
animator = ObjectAnimator.ofPropertyValuesHolder(drawable,
leftProp, topProp, rightProp, bottomProp, fontSizeProp, textColorProp);
} else {
animator = ObjectAnimator.ofPropertyValuesHolder(drawable,
leftProp, topProp, rightProp, bottomProp, fontSizeProp);
}
final float finalFontSize = endFontSize;
AnimatorListenerAdapter listener = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
textView.getOverlay().remove(drawable);
textView.setTextColor(textColors);
textView.setHintTextColor(hintColors);
textView.setHighlightColor(highlightColor);
textView.setLinkTextColor(linkColors);
}
@Override
public void onAnimationPause(Animator animation) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, drawable.getFontSize());
final int paddingLeft = Math.round(drawable.getLeft());
final int paddingTop = Math.round(drawable.getTop());
final float fraction = animator.getAnimatedFraction();
final int paddingRight = Math.round(interpolate(startData.paddingRight,
endData.paddingRight, fraction));
final int paddingBottom = Math.round(interpolate(startData.paddingBottom,
endData.paddingBottom, fraction));
textView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
textView.setTextColor(drawable.getTextColor());
}
@Override
public void onAnimationResume(Animator animation) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, finalFontSize);
textView.setPadding(endData.paddingLeft, endData.paddingTop,
endData.paddingRight, endData.paddingBottom);
textView.setTextColor(endData.textColor);
}
};
animator.addListener(listener);
animator.addPauseListener(listener);
return animator;
}