Provide animation when auto-scaling the font-size

Bug: 15287699
Change-Id: I5e7c5c3bdc96200efa6b4c6f221148647ff91e76
diff --git a/src/com/android/calculator2/CalculatorActivity.java b/src/com/android/calculator2/CalculatorActivity.java
index 5c47535..1600e3d 100644
--- a/src/com/android/calculator2/CalculatorActivity.java
+++ b/src/com/android/calculator2/CalculatorActivity.java
@@ -33,9 +33,13 @@
 import android.view.View.OnLongClickListener;
 import android.view.animation.AccelerateDecelerateInterpolator;
 import android.widget.Button;
+import android.widget.TextView;
+
+import com.android.calculator2.CalculatorEditText.OnTextSizeChangeListener;
+import com.android.calculator2.CalculatorExpressionEvaluator.EvaluateCallback;
 
 public class CalculatorActivity extends Activity
-        implements CalculatorExpressionEvaluator.EvaluateCallback, OnLongClickListener {
+        implements OnTextSizeChangeListener, EvaluateCallback, OnLongClickListener {
 
     public static final String CALCULATOR_ACTIVITY_CURRENT_STATE =
             CalculatorActivity.class.getSimpleName() + "_currentState";
@@ -89,6 +93,8 @@
 
         mFormulaEditText.setEditableFactory(new CalculatorExpressionBuilder.Factory(this));
         mFormulaEditText.addTextChangedListener(mFormulaTextWatcher);
+        mFormulaEditText.setOnTextSizeChangeListener(this);
+
         mDeleteButton.setOnLongClickListener(this);
     }
 
@@ -199,6 +205,32 @@
         }
     }
 
+    @Override
+    public void onTextSizeChanged(final TextView textView, float oldSize) {
+        if (mCurrentState != CalculatorState.INPUT) {
+            // Only animate text changes that occur from user input.
+            return;
+        }
+
+        // Calculate the values needed to perform the scale and translation animations,
+        // maintaining the same apparent baseline for the displayed text.
+        final float textScale = oldSize / textView.getTextSize();
+        final float translationX = (1.0f - textScale) *
+                (textView.getWidth() / 2.0f - textView.getPaddingEnd());
+        final float translationY = (1.0f - textScale) *
+                (textView.getHeight() / 2.0f - textView.getPaddingBottom());
+
+        final AnimatorSet animatorSet = new AnimatorSet();
+        animatorSet.playTogether(
+                ObjectAnimator.ofFloat(textView, View.SCALE_X, textScale, 1.0f),
+                ObjectAnimator.ofFloat(textView, View.SCALE_Y, textScale, 1.0f),
+                ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, translationX, 0.0f),
+                ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, 0.0f));
+        animatorSet.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));
+        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
+        animatorSet.start();
+    }
+
     private void onClear(View sourceView) {
         final int[] clearLocation = new int[2];
         sourceView.getLocationInWindow(clearLocation);