Use cached instance of TextPaint

This commit is contained in:
serso 2016-02-06 22:50:33 +01:00
parent 9e45f62003
commit 75e50893c3

View File

@ -53,6 +53,7 @@ public class AutoResizeTextView extends TextView {
private float mSpacingAdd = 0.0f; private float mSpacingAdd = 0.0f;
// Add ellipsis to text that overflows at the smallest text size // Add ellipsis to text that overflows at the smallest text size
private boolean mAddEllipsis = true; private boolean mAddEllipsis = true;
private final TextPaint tmpPaint = new TextPaint();
// Default constructor override // Default constructor override
public AutoResizeTextView(Context context) { public AutoResizeTextView(Context context) {
@ -286,11 +287,11 @@ public class AutoResizeTextView extends TextView {
// modified: make a copy of the original TextPaint object for measuring // modified: make a copy of the original TextPaint object for measuring
// (apparently the object gets modified while measuring, see also the // (apparently the object gets modified while measuring, see also the
// docs for TextView.getPaint() (which states to access it read-only) // docs for TextView.getPaint() (which states to access it read-only)
TextPaint paintCopy = new TextPaint(paint); tmpPaint.set(paint);
// Update the text paint object // Update the text paint object
paintCopy.setTextSize(textSize); tmpPaint.setTextSize(textSize);
// Measure using a static layout // Measure using a static layout
StaticLayout layout = new StaticLayout(source, paintCopy, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true); StaticLayout layout = new StaticLayout(source, tmpPaint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true);
return layout.getHeight(); return layout.getHeight();
} }