public AutoResizeTextView(@NonNull final Context context, final AttributeSet attrs, @AttrRes final int defStyle) {
super(context, attrs, defStyle);
// using the minimal recommended font size
_minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics());
_maxTextSize = getTextSize();
_paint = new TextPaint(getPaint());
if (_maxLines == 0)
// no value was assigned during construction
_maxLines = NO_LINE_LIMIT;
// prepare size tester:
_sizeTester = new SizeTester() {
final RectF textRect = new RectF();
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onTestSize(final int suggestedSize, @NonNull final RectF availableSpace) {
_paint.setTextSize(suggestedSize);
final TransformationMethod transformationMethod = getTransformationMethod();
final String text;
if (transformationMethod != null)
text = transformationMethod.getTransformation(getText(), AutoResizeTextView.this).toString();
else
text = getText().toString();
final boolean singleLine = getMaxLines() == 1;
if (singleLine) {
textRect.bottom = _paint.getFontSpacing();
textRect.right = _paint.measureText(text);
} else {
final StaticLayout layout = new StaticLayout(text, _paint, _widthLimit, Layout.Alignment.ALIGN_NORMAL, _spacingMult, _spacingAdd, true);
// return early if we have more lines
if (getMaxLines() != NO_LINE_LIMIT && layout.getLineCount() > getMaxLines())
return 1;
textRect.bottom = layout.getHeight();
int maxWidth = -1;
int lineCount = layout.getLineCount();
for (int i = 0; i < lineCount; i++) {
int end = layout.getLineEnd(i);
if (i < lineCount - 1 && end > 0 && !isValidWordWrap(text.charAt(end - 1)))
return 1;
if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i))
maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i);
}
textRect.right = maxWidth;
}
textRect.offsetTo(0, 0);
if (availableSpace.contains(textRect))
// may be too small, don't worry we will find the best match
return -1;
// else, too big
return 1;
}
};
_initialized = true;
}
java类android.text.method.TransformationMethod的实例源码
AutoResizeTextView.java 文件源码
项目:Jisho
阅读 31
收藏 0
点赞 0
评论 0
AutofitHelper.java 文件源码
项目:android-autofittextview-master
阅读 27
收藏 0
点赞 0
评论 0
/**
* Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
*/
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
int maxLines, float precision) {
if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
// Don't auto-size since there's no limit on lines.
return;
}
int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
if (targetWidth <= 0) {
return;
}
CharSequence text = view.getText();
TransformationMethod method = view.getTransformationMethod();
if (method != null) {
text = method.getTransformation(text, view);
}
Context context = view.getContext();
Resources r = Resources.getSystem();
DisplayMetrics displayMetrics;
float size = maxTextSize;
float high = size;
float low = 0;
if (context != null) {
r = context.getResources();
}
displayMetrics = r.getDisplayMetrics();
paint.set(view.getPaint());
paint.setTextSize(size);
if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
|| getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
displayMetrics);
}
if (size < minTextSize) {
size = minTextSize;
}
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
AutofitHelper.java 文件源码
项目:aMatch
阅读 26
收藏 0
点赞 0
评论 0
/**
* Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
*/
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
int maxLines, float precision) {
if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
// Don't auto-size since there's no limit on lines.
return;
}
int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
if (targetWidth <= 0) {
return;
}
CharSequence text = view.getText();
TransformationMethod method = view.getTransformationMethod();
if (method != null) {
text = method.getTransformation(text, view);
}
Context context = view.getContext();
Resources r = Resources.getSystem();
DisplayMetrics displayMetrics;
float size = maxTextSize;
float high = size;
float low = 0;
if (context != null) {
r = context.getResources();
}
displayMetrics = r.getDisplayMetrics();
paint.set(view.getPaint());
paint.setTextSize(size);
if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
|| getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
displayMetrics);
}
if (mIsHeightFitting) {
int targetHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingTop();
if (targetHeight <= 0) {
if (size < minTextSize) {
size = minTextSize;
}
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
return;
}
float textHeight = getTextHeight(text, paint, targetWidth, size);
textHeight = getTextHeight(text, paint, targetWidth, size);
float heightRatio = targetHeight / textHeight;
float newSize = size * heightRatio;
if (newSize < size) {
size = newSize;
}
if (size < minTextSize) {
size = minTextSize;
}
}
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
UIUtil.java 文件源码
项目:connectedteam-android
阅读 29
收藏 0
点赞 0
评论 0
/**
* Makes any compound drawables associated with this textview draw closer to the text.
* @param button
*/
public static void setTightCompoundDrawables(final TextView button){
if(button!=null) button.post(new Runnable(){
@Override
public void run() {
final Drawable[] existingDrawables = button.getCompoundDrawables();
int width = button.getWidth();
String text = button.getText().toString();
TransformationMethod meth = button.getTransformationMethod();
if (meth!=null ){
//the text returned from getText() may be transformed before it is drawn. IE if textAllCaps is set in XML,
//The text will be subsequently transformed, changing its width. So apply the transformation if available.
text = meth.getTransformation(button.getText(), button).toString();
}
float textWidth = button.getPaint().measureText(text);
float padding=-1;
if(existingDrawables[0]!=null && existingDrawables[2]!=null){
//d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
padding=(width-textWidth-existingDrawables[0].getBounds().width()-existingDrawables[2].getBounds().width()-scale(SPACING_PX))/2f;
}
else if(existingDrawables[0]!=null ){
//d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
padding=(width-textWidth-existingDrawables[0].getBounds().width()-scale(SPACING_PX))/2f;
}
else if(existingDrawables[2]!=null ){
//d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
padding=(width-textWidth-existingDrawables[2].getBounds().width()-scale(SPACING_PX))/2f;
}
//no drawables, or text too wide for view
if(padding<=0 || padding>(width/2f)) padding = scale(SPACING_PX);
//button.setCompoundDrawables(existingDrawables[0], existingDrawables[1], existingDrawables[2], existingDrawables[3]);
button.setPadding((int)(padding+0.0), button.getPaddingTop(), (int)(padding+0.0), button.getPaddingBottom());
}
});
}
AutoResizableTextView.java 文件源码
项目:E-MobileActivities
阅读 14
收藏 0
点赞 0
评论 0
public AutoResizableTextView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
// using the minimal recommended font size
_minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics());
_maxTextSize = getTextSize();
_paint = new TextPaint(getPaint());
if (_maxLines == 0)
// no value was assigned during construction
_maxLines = NO_LINE_LIMIT;
// prepare size tester:
sizeTester = new SizeTester() {
final RectF textRect = new RectF();
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onTestSize(final int suggestedSize, final RectF availableSPace) {
_paint.setTextSize(suggestedSize);
final TransformationMethod transformationMethod = getTransformationMethod();
final String text;
if (transformationMethod != null)
text = transformationMethod.getTransformation(getText(), AutoResizableTextView.this).toString();
else
text = getText().toString();
final boolean singleLine = getMaxLines() == 1;
if (singleLine) {
textRect.bottom = _paint.getFontSpacing();
textRect.right = _paint.measureText(text);
} else {
final StaticLayout layout = new StaticLayout(text, _paint, _widthLimit, Alignment.ALIGN_NORMAL, spacingMult, _spacingAdd, true);
// return early if we have more lines
if (getMaxLines() != NO_LINE_LIMIT && layout.getLineCount() > getMaxLines())
return 1;
textRect.bottom = layout.getHeight();
int maxWidth = -1;
for (int i = 0; i < layout.getLineCount(); i++)
if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i))
maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i);
textRect.right = maxWidth;
}
textRect.offsetTo(0, 0);
if (availableSPace.contains(textRect))
// may be too small, don't worry we will find the best match
return -1;
// else, too big
return 1;
}
};
_initialized = true;
}
EditText.java 文件源码
项目:AndroidMaterialValidation
阅读 20
收藏 0
点赞 0
评论 0
/**
* Sets the transformation that is applied to the text that this TextView is displaying.
*/
public final void setTransformationMethod(final TransformationMethod method) {
getView().setTransformationMethod(method);
}
AutofitHelper.java 文件源码
项目:RealTextView
阅读 34
收藏 0
点赞 0
评论 0
/**
* Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
*/
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
int maxLines, float precision) {
if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
// Don't auto-size since there's no limit on lines.
return;
}
int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
if (targetWidth <= 0) {
return;
}
CharSequence text = view.getText();
TransformationMethod method = view.getTransformationMethod();
if (method != null) {
text = method.getTransformation(text, view);
}
Context context = view.getContext();
Resources r = Resources.getSystem();
DisplayMetrics displayMetrics;
float size = maxTextSize;
float high = size;
float low = 0;
if (context != null) {
r = context.getResources();
}
displayMetrics = r.getDisplayMetrics();
paint.set(view.getPaint());
paint.setTextSize(size);
if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
|| getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
displayMetrics);
}
if (size < minTextSize) {
size = minTextSize;
}
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
AutoResizeTextView.java 文件源码
项目:AutoFitTextView
阅读 27
收藏 0
点赞 0
评论 0
public AutoResizeTextView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
// using the minimal recommended font size
_minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics());
_maxTextSize = getTextSize();
_paint = new TextPaint(getPaint());
if (_maxLines == 0)
// no value was assigned during construction
_maxLines = NO_LINE_LIMIT;
// prepare size tester:
_sizeTester = new SizeTester() {
final RectF textRect = new RectF();
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onTestSize(final int suggestedSize, final RectF availableSpace) {
_paint.setTextSize(suggestedSize);
final TransformationMethod transformationMethod = getTransformationMethod();
final String text;
if (transformationMethod != null)
text = transformationMethod.getTransformation(getText(), AutoResizeTextView.this).toString();
else
text = getText().toString();
final boolean singleLine = getMaxLines() == 1;
if (singleLine) {
textRect.bottom = _paint.getFontSpacing();
textRect.right = _paint.measureText(text);
} else {
final StaticLayout layout = new StaticLayout(text, _paint, _widthLimit, Alignment.ALIGN_NORMAL, _spacingMult, _spacingAdd, true);
// return early if we have more lines
if (getMaxLines() != NO_LINE_LIMIT && layout.getLineCount() > getMaxLines())
return 1;
textRect.bottom = layout.getHeight();
int maxWidth = -1;
int lineCount = layout.getLineCount();
for (int i = 0; i < lineCount; i++) {
int end = layout.getLineEnd(i);
if (i < lineCount - 1 && end > 0 && !isValidWordWrap(text.charAt(end - 1), text.charAt(end)))
return 1;
if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i))
maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i);
}
//for (int i = 0; i < layout.getLineCount(); i++)
// if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i))
// maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i);
textRect.right = maxWidth;
}
textRect.offsetTo(0, 0);
if (availableSpace.contains(textRect))
// may be too small, don't worry we will find the best match
return -1;
// else, too big
return 1;
}
};
_initialized = true;
}
AutofitTextView.java 文件源码
项目:UltimateAndroid
阅读 17
收藏 0
点赞 0
评论 0
/**
* Re size the font so the specified text fits in the text box assuming the text box is the
* specified width.
*/
private void refitText() {
if (!mSizeToFit) {
return;
}
if (mMaxLines <= 0) {
// Don't auto-size since there's no limit on lines.
return;
}
CharSequence text = getText();
TransformationMethod method = getTransformationMethod();
if (method != null) {
text = method.getTransformation(text, this);
}
int targetWidth = getWidth() - getPaddingLeft() - getPaddingRight();
if (targetWidth > 0) {
Context context = getContext();
Resources r = Resources.getSystem();
DisplayMetrics displayMetrics;
float size = mMaxTextSize;
float high = size;
float low = 0;
if (context != null) {
r = context.getResources();
}
displayMetrics = r.getDisplayMetrics();
mPaint.set(getPaint());
mPaint.setTextSize(size);
if ((mMaxLines == 1 && mPaint.measureText(text, 0, text.length()) > targetWidth)
|| getLineCount(text, mPaint, size, targetWidth, displayMetrics) > mMaxLines) {
size = getTextSize(text, mPaint, targetWidth, mMaxLines, low, high, mPrecision,
displayMetrics);
}
if (size < mMinTextSize) {
size = mMinTextSize;
}
super.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
}
AutofitTextView.java 文件源码
项目:UltimateAndroid
阅读 19
收藏 0
点赞 0
评论 0
/**
* Re size the font so the specified text fits in the text box assuming the text box is the
* specified width.
*/
private void refitText() {
if (!mSizeToFit) {
return;
}
if (mMaxLines <= 0) {
// Don't auto-size since there's no limit on lines.
return;
}
CharSequence text = getText();
TransformationMethod method = getTransformationMethod();
if (method != null) {
text = method.getTransformation(text, this);
}
int targetWidth = getWidth() - getPaddingLeft() - getPaddingRight();
if (targetWidth > 0) {
Context context = getContext();
Resources r = Resources.getSystem();
DisplayMetrics displayMetrics;
float size = mMaxTextSize;
float high = size;
float low = 0;
if (context != null) {
r = context.getResources();
}
displayMetrics = r.getDisplayMetrics();
mPaint.set(getPaint());
mPaint.setTextSize(size);
if ((mMaxLines == 1 && mPaint.measureText(text, 0, text.length()) > targetWidth)
|| getLineCount(text, mPaint, size, targetWidth, displayMetrics) > mMaxLines) {
size = getTextSize(text, mPaint, targetWidth, mMaxLines, low, high, mPrecision,
displayMetrics);
}
if (size < mMinTextSize) {
size = mMinTextSize;
}
super.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
}