use of android.text.StaticLayout in project android-autofittextview by grantland.
the class AutofitHelper method getAutofitTextSize.
/**
* Recursive binary search to find the best size for the text.
*/
private static float getAutofitTextSize(CharSequence text, TextPaint paint, float targetWidth, int maxLines, float low, float high, float precision, DisplayMetrics displayMetrics) {
float mid = (low + high) / 2.0f;
int lineCount = 1;
StaticLayout layout = null;
paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, displayMetrics));
if (maxLines != 1) {
layout = new StaticLayout(text, paint, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
lineCount = layout.getLineCount();
}
if (SPEW)
Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid + " target=" + targetWidth + " maxLines=" + maxLines + " lineCount=" + lineCount);
if (lineCount > maxLines) {
// For the case that `text` has more newline characters than `maxLines`.
if ((high - low) < precision) {
return low;
}
return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
} else if (lineCount < maxLines) {
return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
} else {
float maxLineWidth = 0;
if (maxLines == 1) {
maxLineWidth = paint.measureText(text, 0, text.length());
} else {
for (int i = 0; i < lineCount; i++) {
if (layout.getLineWidth(i) > maxLineWidth) {
maxLineWidth = layout.getLineWidth(i);
}
}
}
if ((high - low) < precision) {
return low;
} else if (maxLineWidth > targetWidth) {
return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
} else if (maxLineWidth < targetWidth) {
return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
} else {
return mid;
}
}
}
use of android.text.StaticLayout in project android-autofittextview by grantland.
the class AutofitHelper method getLineCount.
private static int getLineCount(CharSequence text, TextPaint paint, float size, float width, DisplayMetrics displayMetrics) {
paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size, displayMetrics));
StaticLayout layout = new StaticLayout(text, paint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
return layout.getLineCount();
}
use of android.text.StaticLayout in project UltimateAndroid by cymcsg.
the class AutofitTextView method getLineCount.
private static int getLineCount(CharSequence text, TextPaint paint, float size, float width, DisplayMetrics displayMetrics) {
paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size, displayMetrics));
StaticLayout layout = new StaticLayout(text, paint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
return layout.getLineCount();
}
use of android.text.StaticLayout in project UltimateAndroid by cymcsg.
the class CreditsRollView method measureAndLayoutText.
private void measureAndLayoutText(CharSequence text) {
if (TextUtils.isEmpty(text)) {
mTextLayout = null;
return;
}
int availableWidth = getWidth() - mPaddingLeft - mPaddingRight;
mTextLayout = new StaticLayout(text, mTextPaint, availableWidth, Layout.Alignment.ALIGN_CENTER, 1.1f, 0f, true);
}
use of android.text.StaticLayout in project philm by chrisbanes.
the class AutofitTextView method getTextSize.
/**
* Recursive binary search to find the best size for the text
*/
private static float getTextSize(String text, TextPaint paint, float targetWidth, int maxLines, float low, float high, float precision, DisplayMetrics displayMetrics) {
float mid = (low + high) / 2.0f;
int lineCount = 1;
StaticLayout layout = null;
paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, displayMetrics));
if (maxLines != 1) {
layout = new StaticLayout(text, paint, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
lineCount = layout.getLineCount();
}
if (SPEW) {
Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid + " target=" + targetWidth + " maxLines=" + maxLines + " lineCount=" + lineCount);
}
if (lineCount > maxLines) {
return getTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
} else if (lineCount < maxLines) {
return getTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
} else {
float maxLineWidth = 0;
if (maxLines == 1) {
maxLineWidth = paint.measureText(text);
} else {
for (int i = 0; i < lineCount; i++) {
if (layout.getLineWidth(i) > maxLineWidth) {
maxLineWidth = layout.getLineWidth(i);
}
}
}
if ((high - low) < precision) {
return low;
} else if (maxLineWidth > targetWidth) {
return getTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
} else if (maxLineWidth < targetWidth) {
return getTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
} else {
return mid;
}
}
}
Aggregations