summaryrefslogtreecommitdiffstats
path: root/java/com/android/incallui/autoresizetext/AutoResizeTextView.java
blob: 2789ceac2719f542b145c502828c4c9b6b9417ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.incallui.autoresizetext;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.RectF;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.SparseIntArray;
import android.util.TypedValue;
import android.widget.TextView;
import javax.annotation.Nullable;

/**
 * A TextView that automatically scales its text to completely fill its allotted width.
 *
 * <p>Note: In some edge cases, the binary search algorithm to find the best fit may slightly
 * overshoot / undershoot its constraints. See a bug. No minimal repro case has been
 * found yet. A known workaround is the solution provided on StackOverflow:
 * http://stackoverflow.com/a/5535672
 */
public class AutoResizeTextView extends TextView {
  private static final int NO_LINE_LIMIT = -1;
  private static final float DEFAULT_MIN_TEXT_SIZE = 16.0f;
  private static final int DEFAULT_RESIZE_STEP_UNIT = TypedValue.COMPLEX_UNIT_PX;

  private final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
  private final RectF availableSpaceRect = new RectF();
  private final SparseIntArray textSizesCache = new SparseIntArray();
  private final TextPaint textPaint = new TextPaint();
  private int resizeStepUnit = DEFAULT_RESIZE_STEP_UNIT;
  private float minTextSize = DEFAULT_MIN_TEXT_SIZE;
  private float maxTextSize;
  private int maxWidth;
  private int maxLines;
  private float lineSpacingMultiplier = 1.0f;
  private float lineSpacingExtra = 0.0f;

  public AutoResizeTextView(Context context) {
    super(context, null, 0);
    initialize(context, null, 0, 0);
  }

  public AutoResizeTextView(Context context, AttributeSet attrs) {
    super(context, attrs, 0);
    initialize(context, attrs, 0, 0);
  }

  public AutoResizeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initialize(context, attrs, defStyleAttr, 0);
  }

  public AutoResizeTextView(
      Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initialize(context, attrs, defStyleAttr, defStyleRes);
  }

  private void initialize(
      Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(
        attrs, R.styleable.AutoResizeTextView, defStyleAttr, defStyleRes);
    readAttrs(typedArray);
    textPaint.set(getPaint());
  }

  /** Overridden because getMaxLines is only defined in JB+. */
  @Override
  public final int getMaxLines() {
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
      return super.getMaxLines();
    } else {
      return maxLines;
    }
  }

  /** Overridden because getMaxLines is only defined in JB+. */
  @Override
  public final void setMaxLines(int maxLines) {
    super.setMaxLines(maxLines);
    this.maxLines = maxLines;
  }

  /** Overridden because getLineSpacingMultiplier is only defined in JB+. */
  @Override
  public final float getLineSpacingMultiplier() {
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
      return super.getLineSpacingMultiplier();
    } else {
      return lineSpacingMultiplier;
    }
  }

  /** Overridden because getLineSpacingExtra is only defined in JB+. */
  @Override
  public final float getLineSpacingExtra() {
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
      return super.getLineSpacingExtra();
    } else {
      return lineSpacingExtra;
    }
  }

  /**
   * Overridden because getLineSpacingMultiplier and getLineSpacingExtra are only defined in JB+.
   */
  @Override
  public final void setLineSpacing(float add, float mult) {
    super.setLineSpacing(add, mult);
    lineSpacingMultiplier = mult;
    lineSpacingExtra = add;
  }

  /**
   * Although this overrides the setTextSize method from the TextView base class, it changes the
   * semantics a bit: Calling setTextSize now specifies the maximum text size to be used by this
   * view. If the text can't fit with that text size, the text size will be scaled down, up to the
   * minimum text size specified in {@link #setMinTextSize}.
   *
   * <p>Note that the final size unit will be truncated to the nearest integer value of the
   * specified unit.
   */
  @Override
  public final void setTextSize(int unit, float size) {
    float maxTextSize = TypedValue.applyDimension(unit, size, displayMetrics);
    if (this.maxTextSize != maxTextSize) {
      this.maxTextSize = maxTextSize;
      // TODO(tobyj): It's not actually necessary to clear the whole cache here. To optimize cache
      // deletion we'd have to delete all entries in the cache with a value equal or larger than
      // MIN(old_max_size, new_max_size) when changing maxTextSize; and all entries with a value
      // equal or smaller than MAX(old_min_size, new_min_size) when changing minTextSize.
      textSizesCache.clear();
      requestLayout();
    }
  }

  /**
   * Sets the lower text size limit and invalidate the view.
   *
   * <p>The parameters follow the same behavior as they do in {@link #setTextSize}.
   *
   * <p>Note that the final size unit will be truncated to the nearest integer value of the
   * specified unit.
   */
  public final void setMinTextSize(int unit, float size) {
    float minTextSize = TypedValue.applyDimension(unit, size, displayMetrics);
    if (this.minTextSize != minTextSize) {
      this.minTextSize = minTextSize;
      textSizesCache.clear();
      requestLayout();
    }
  }

  /**
   * Sets the unit to use as step units when computing the resized font size. This view's text
   * contents will always be rendered as a whole integer value in the unit specified here. For
   * example, if the unit is {@link TypedValue#COMPLEX_UNIT_SP}, then the text size may end up
   * being 13sp or 14sp, but never 13.5sp.
   *
   * <p>By default, the AutoResizeTextView uses the unit {@link TypedValue#COMPLEX_UNIT_PX}.
   *
   * @param unit the unit type to use; must be a known unit type from {@link TypedValue}.
   */
  public final void setResizeStepUnit(int unit) {
    if (resizeStepUnit != unit) {
      resizeStepUnit = unit;
      requestLayout();
    }
  }

  private void readAttrs(TypedArray typedArray) {
    resizeStepUnit = typedArray.getInt(
        R.styleable.AutoResizeTextView_autoResizeText_resizeStepUnit, DEFAULT_RESIZE_STEP_UNIT);
    minTextSize = (int) typedArray.getDimension(
        R.styleable.AutoResizeTextView_autoResizeText_minTextSize, DEFAULT_MIN_TEXT_SIZE);
    maxTextSize = (int) getTextSize();
  }

  private void adjustTextSize() {
    int maxWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    int maxHeight = getMeasuredHeight() - getPaddingBottom() - getPaddingTop();

    if (maxWidth <= 0 || maxHeight <= 0) {
      return;
    }

    this.maxWidth = maxWidth;
    availableSpaceRect.right = maxWidth;
    availableSpaceRect.bottom = maxHeight;
    int minSizeInStepSizeUnits = (int) Math.ceil(convertToResizeStepUnits(minTextSize));
    int maxSizeInStepSizeUnits = (int) Math.floor(convertToResizeStepUnits(maxTextSize));
    float textSize = computeTextSize(
        minSizeInStepSizeUnits, maxSizeInStepSizeUnits, availableSpaceRect);
    super.setTextSize(resizeStepUnit, textSize);
  }

  private boolean suggestedSizeFitsInSpace(float suggestedSizeInPx, RectF availableSpace) {
    textPaint.setTextSize(suggestedSizeInPx);
    String text = getText().toString();
    int maxLines = getMaxLines();
    if (maxLines == 1) {
      // If single line, check the line's height and width.
      return textPaint.getFontSpacing() <= availableSpace.bottom
          && textPaint.measureText(text) <= availableSpace.right;
    } else {
      // If multiline, lay the text out, then check the number of lines, the layout's height,
      // and each line's width.
      StaticLayout layout = new StaticLayout(text,
          textPaint,
          maxWidth,
          Alignment.ALIGN_NORMAL,
          getLineSpacingMultiplier(),
          getLineSpacingExtra(),
          true);

      // Return false if we need more than maxLines. The text is obviously too big in this case.
      if (maxLines != NO_LINE_LIMIT && layout.getLineCount() > maxLines) {
        return false;
      }
      // Return false if the height of the layout is too big.
      return layout.getHeight() <= availableSpace.bottom;
    }
  }

  /**
   * Computes the final text size to use for this text view, factoring in any previously
   * cached computations.
   *
   * @param minSize the minimum text size to allow, in units of {@link #resizeStepUnit}
   * @param maxSize the maximum text size to allow, in units of {@link #resizeStepUnit}
   */
  private float computeTextSize(int minSize, int maxSize, RectF availableSpace) {
    CharSequence text = getText();
    if (text != null && textSizesCache.get(text.hashCode()) != 0) {
      return textSizesCache.get(text.hashCode());
    }
    int size = binarySearchSizes(minSize, maxSize, availableSpace);
    textSizesCache.put(text == null ? 0 : text.hashCode(), size);
    return size;
  }

  /**
   * Performs a binary search to find the largest font size that will still fit within the size
   * available to this view.
   * @param minSize the minimum text size to allow, in units of {@link #resizeStepUnit}
   * @param maxSize the maximum text size to allow, in units of {@link #resizeStepUnit}
   */
  private int binarySearchSizes(int minSize, int maxSize, RectF availableSpace) {
    int bestSize = minSize;
    int low = minSize + 1;
    int high = maxSize;
    int sizeToTry;
    while (low <= high) {
      sizeToTry = (low + high) / 2;
      float dimension = TypedValue.applyDimension(resizeStepUnit, sizeToTry, displayMetrics);
      if (suggestedSizeFitsInSpace(dimension, availableSpace)) {
        bestSize = low;
        low = sizeToTry + 1;
      } else {
        high = sizeToTry - 1;
        bestSize = high;
      }
    }
    return bestSize;
  }

  private float convertToResizeStepUnits(float dimension) {
    // To figure out the multiplier between a raw dimension and the resizeStepUnit, we invert the
    // conversion of 1 resizeStepUnit to a raw dimension.
    float multiplier = 1 / TypedValue.applyDimension(resizeStepUnit, 1, displayMetrics);
    return dimension * multiplier;
  }

  @Override
  protected final void onTextChanged(
      final CharSequence text, final int start, final int before, final int after) {
    super.onTextChanged(text, start, before, after);
    adjustTextSize();
  }

  @Override
  protected final void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
    super.onSizeChanged(width, height, oldWidth, oldHeight);
    if (width != oldWidth || height != oldHeight) {
      textSizesCache.clear();
      adjustTextSize();
    }
  }

  @Override
  protected final void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    adjustTextSize();
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }
}