use of android.text.StaticLayout in project UltimateAndroid by cymcsg.
the class AutofitTextView method getTextSize.
/**
* Recursive binary search to find the best size for the text
*/
private static float getTextSize(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) {
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, 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 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;
}
}
}
use of android.text.StaticLayout in project ABPlayer by winkstu.
the class OutlineTextView method onMeasure.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Layout layout = new StaticLayout(getText(), mTextPaintOutline, measureWidth(widthMeasureSpec), Layout.Alignment.ALIGN_CENTER, mSpacingMult, mSpacingAdd, mIncludePad);
int ex = (int) (mBorderSize * 2 + 1);
setMeasuredDimension(measureWidth(widthMeasureSpec) + ex, measureHeight(heightMeasureSpec) * layout.getLineCount() + ex);
}
use of android.text.StaticLayout in project ABPlayer by winkstu.
the class OutlineTextView method onDraw.
@Override
protected void onDraw(Canvas canvas) {
Layout layout = new StaticLayout(getText(), mTextPaintOutline, getWidth(), Layout.Alignment.ALIGN_CENTER, mSpacingMult, mSpacingAdd, mIncludePad);
layout.draw(canvas);
layout = new StaticLayout(getText(), mTextPaint, getWidth(), Layout.Alignment.ALIGN_CENTER, mSpacingMult, mSpacingAdd, mIncludePad);
layout.draw(canvas);
}
use of android.text.StaticLayout in project TapTargetView by KeepSafe.
the class TapTargetView method updateTextLayouts.
void updateTextLayouts() {
final int textWidth = Math.min(getWidth(), TEXT_MAX_WIDTH) - TEXT_PADDING * 2;
if (textWidth <= 0) {
return;
}
titleLayout = new StaticLayout(title, titlePaint, textWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
if (description != null) {
descriptionLayout = new StaticLayout(description, descriptionPaint, textWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
} else {
descriptionLayout = null;
}
}
use of android.text.StaticLayout in project android_frameworks_base by crdroidandroid.
the class Editor method chooseSize.
private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) {
int wid = tv.getPaddingLeft() + tv.getPaddingRight();
int ht = tv.getPaddingTop() + tv.getPaddingBottom();
int defaultWidthInPixels = mTextView.getResources().getDimensionPixelSize(com.android.internal.R.dimen.textview_error_popup_default_width);
Layout l = new StaticLayout(text, tv.getPaint(), defaultWidthInPixels, Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
float max = 0;
for (int i = 0; i < l.getLineCount(); i++) {
max = Math.max(max, l.getLineWidth(i));
}
/*
* Now set the popup size to be big enough for the text plus the border capped
* to DEFAULT_MAX_POPUP_WIDTH
*/
pop.setWidth(wid + (int) Math.ceil(max));
pop.setHeight(ht + l.getHeight());
}
Aggregations