Java Code Examples for android.text.TextPaint#getTextSize()
The following examples show how to use
android.text.TextPaint#getTextSize() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DSAvatarImageView.java From DSAvataImageView with MIT License | 6 votes |
private float getTextSizeToFit(int maxWidth,int maxHeight,String input,TextPaint textPaint){ float textSize = textPaint.getTextSize(); String text=getTextForSize(input); if(text.length()==0) return textSize; Rect textBound = new Rect(); textPaint.getTextBounds(text, 0, text.length(), textBound); float width = textBound.width(); float height = textBound.height(); float adjustX = maxWidth / width; float adjustY = maxHeight / height; textSize = textSize * (adjustY < adjustX ? adjustY : adjustX); return textSize; }
Example 2
Source File: RecipientEditTextView.java From ChipsLibrary with Apache License 2.0 | 6 votes |
private DrawableRecipientChip constructChipSpan(final RecipientEntry contact,final boolean pressed,final boolean leaveIconSpace) throws NullPointerException { if(mChipBackground==null) throw new NullPointerException("Unable to render any chips as setChipDimensions was not called."); final TextPaint paint=getPaint(); final float defaultSize=paint.getTextSize(); final int defaultColor=paint.getColor(); Bitmap tmpBitmap; if(pressed) tmpBitmap=createSelectedChip(contact,paint); else tmpBitmap=createUnselectedChip(contact,paint,leaveIconSpace); // Pass the full text, un-ellipsized, to the chip. final Drawable result=new BitmapDrawable(getResources(),tmpBitmap); result.setBounds(0,0,tmpBitmap.getWidth(),tmpBitmap.getHeight()); final DrawableRecipientChip recipientChip=new VisibleRecipientChip(result,contact); // Return text to the original size. paint.setTextSize(defaultSize); paint.setColor(defaultColor); return recipientChip; }
Example 3
Source File: SettingFragment.java From light-novel-library_Wenku8_Android with GNU General Public License v2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void updateDrawState(TextPaint paint) { paint.setStyle(Paint.Style.FILL); Shader shader = new LinearGradient(0, 0, 0, paint.getTextSize() * colors.length, colors, null, Shader.TileMode.MIRROR); Matrix matrix = new Matrix(); matrix.setRotate(90); shader.setLocalMatrix(matrix); paint.setShader(shader); }
Example 4
Source File: SettingFragment.java From light-novel-library_Wenku8_Android with GNU General Public License v2.0 | 5 votes |
@Override public void updateDrawState(TextPaint paint) { paint.setStyle(Paint.Style.FILL); Shader shader = new LinearGradient(0, 0, 0, paint.getTextSize() * colors.length, colors, null, Shader.TileMode.MIRROR); Matrix matrix = new Matrix(); matrix.setRotate(90); shader.setLocalMatrix(matrix); paint.setShader(shader); }
Example 5
Source File: AboutActivity.java From light-novel-library_Wenku8_Android with GNU General Public License v2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void updateDrawState(TextPaint paint) { paint.setStyle(Paint.Style.FILL); Shader shader = new LinearGradient(0, 0, 0, paint.getTextSize() * colors.length, colors, null, Shader.TileMode.MIRROR); Matrix matrix = new Matrix(); matrix.setRotate(90); shader.setLocalMatrix(matrix); paint.setShader(shader); }
Example 6
Source File: SizeAdjustingTextView.java From SizeAdjustingTextView with GNU General Public License v2.0 | 5 votes |
private float findNewTextSize(int width, int height, CharSequence text) { TextPaint textPaint = new TextPaint(getPaint()); float targetTextSize = textPaint.getTextSize(); int textHeight = getTextHeight(text, textPaint, width, targetTextSize); while(textHeight > height && targetTextSize > mMinTextSize) { targetTextSize = Math.max(targetTextSize - 1, mMinTextSize); textHeight = getTextHeight(text, textPaint, width, targetTextSize); } return targetTextSize; }
Example 7
Source File: SizeAdjustingTextView.java From SizeAdjustingTextView with GNU General Public License v2.0 | 5 votes |
private float findNewTextSize(int width, int height, CharSequence text) { TextPaint textPaint = new TextPaint(getPaint()); float targetTextSize = textPaint.getTextSize(); int textHeight = getTextHeight(text, textPaint, width, targetTextSize); while(textHeight > height && targetTextSize > mMinTextSize) { targetTextSize = Math.max(targetTextSize - 1, mMinTextSize); textHeight = getTextHeight(text, textPaint, width, targetTextSize); } return targetTextSize; }
Example 8
Source File: SizeAdjustingTextView.java From SizeAdjustingTextView with GNU General Public License v2.0 | 5 votes |
private float findNewTextSize(int width, int height, CharSequence text) { TextPaint textPaint = new TextPaint(getPaint()); float targetTextSize = textPaint.getTextSize(); int textHeight = getTextHeight(text, textPaint, width, targetTextSize); while(textHeight > height && targetTextSize > mMinTextSize) { targetTextSize = Math.max(targetTextSize - 1, mMinTextSize); textHeight = getTextHeight(text, textPaint, width, targetTextSize); } return targetTextSize; }
Example 9
Source File: RainbowSpanActivity.java From advanced-textview with Apache License 2.0 | 5 votes |
@Override public void updateDrawState(TextPaint paint) { paint.setStyle(Paint.Style.FILL); Shader shader = new LinearGradient(0, 0, 0, paint.getTextSize() * colors.length, colors, null, Shader.TileMode.MIRROR); Matrix matrix = new Matrix(); matrix.setRotate(90); shader.setLocalMatrix(matrix); paint.setShader(shader); }
Example 10
Source File: Sushi.java From android-slidr with Apache License 2.0 | 5 votes |
private void drawMultilineText(Canvas canvas, String text, float x, float y, TextPaint paint, Layout.Alignment aligment) { final float lineHeight = paint.getTextSize(); float lineY = y; for (CharSequence line : text.split("\n")) { canvas.save(); { final float lineWidth = (int) paint.measureText(line.toString()); float lineX = x; if (aligment == Layout.Alignment.ALIGN_CENTER) { lineX -= lineWidth / 2f; } if (lineX < 0) { lineX = 0; } final float right = lineX + lineWidth; if (right > canvas.getWidth()) { lineX = canvas.getWidth() - lineWidth - settings.paddingCorners; } canvas.translate(lineX, lineY); final StaticLayout staticLayout = new StaticLayout(line, paint, (int) lineWidth, aligment, 1.0f, 0, false); staticLayout.draw(canvas); lineY += lineHeight; } canvas.restore(); } }
Example 11
Source File: AutoResizingTextView.java From Simple-Solitaire with GNU General Public License v3.0 | 4 votes |
/** * Resize the text size with specified width and height * * @param width * @param height */ public void resizeText(int width, int height) { CharSequence text = getText(); // Do not resize if the view does not have dimensions or there is no text if (text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) { return; } if (getTransformationMethod() != null) { text = getTransformationMethod().getTransformation(text, this); } // Get the text view's paint object TextPaint textPaint = getPaint(); // Store the current text size float oldTextSize = textPaint.getTextSize(); // If there is a max text size set, use the lesser of that and the default text size float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize; // Get the required text height int textHeight = getTextHeight(text, textPaint, width, targetTextSize); // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes while (textHeight > height && targetTextSize > mMinTextSize) { targetTextSize = Math.max(targetTextSize - 2, mMinTextSize); textHeight = getTextHeight(text, textPaint, width, targetTextSize); } // If we had reached our minimum text size and still don't fit, append an ellipsis if (mAddEllipsis && targetTextSize == mMinTextSize && textHeight > height) { // Draw using a static layout // modified: use a copy of TextPaint for measuring TextPaint paint = new TextPaint(textPaint); // Draw using a static layout StaticLayout layout = new StaticLayout(text, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); // Check that we have a least one line of rendered text if (layout.getLineCount() > 0) { // Since the line at the specific vertical position would be cut off, // we must trim up to the previous line int lastLine = layout.getLineForVertical(height) - 1; // If the text would not even fit on a single line, clear it if (lastLine < 0) { setText(""); } // Otherwise, trim to the previous line and add an ellipsis else { int start = layout.getLineStart(lastLine); int end = layout.getLineEnd(lastLine); float lineWidth = layout.getLineWidth(lastLine); float ellipseWidth = textPaint.measureText(mEllipsis); // Trim characters off until we have enough room to draw the ellipsis while (width < lineWidth + ellipseWidth) { lineWidth = textPaint.measureText(text.subSequence(start, --end + 1).toString()); } setText(text.subSequence(0, end) + mEllipsis); } } } // Some devices try to auto adjust line spacing, so force default line spacing // and invalidate the layout as a side effect setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize); setLineSpacing(mSpacingAdd, mSpacingMult); // Notify the listener if registered if (mTextResizeListener != null) { mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize); } // Reset force resize flag mNeedsResize = false; }
Example 12
Source File: TooManyCommunitiesHintCell.java From Telegram with GNU General Public License v2.0 | 4 votes |
public TooManyCommunitiesHintCell(Context context) { super(context); imageView = new ImageView(context); imageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chats_nameMessage_threeLines), PorterDuff.Mode.MULTIPLY)); headerTextView = new TextView(context); headerTextView.setTextColor(Theme.getColor(Theme.key_chats_nameMessage_threeLines)); headerTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); headerTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf")); headerTextView.setGravity(Gravity.CENTER); addView(headerTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT, 52, 75, 52, 0)); messageTextView = new TextView(context); messageTextView.setTextColor(Theme.getColor(Theme.key_chats_message)); messageTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14); messageTextView.setGravity(Gravity.CENTER); addView(messageTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT, 36, 110, 36, 0)); TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(Color.WHITE); textPaint.setTextSize(AndroidUtilities.dp(12)); textPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf")); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); String s = "500"; imageLayout = new FrameLayout(context) { RectF rect = new RectF(); @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setColor(Theme.getColor(Theme.key_windowBackgroundWhiteRedText)); canvas.save(); canvas.translate(getMeasuredWidth() - textPaint.measureText(s) - AndroidUtilities.dp(8), AndroidUtilities.dpf2(7f)); rect.set(0, 0, textPaint.measureText(s), textPaint.getTextSize()); rect.inset(-AndroidUtilities.dp(6), -AndroidUtilities.dp(3)); float r = (textPaint.getTextSize()) / 2f + AndroidUtilities.dp(3); canvas.drawRoundRect(rect, r, r, paint); canvas.drawText(s, 0, textPaint.getTextSize() - AndroidUtilities.dpf2(2f), textPaint); canvas.restore(); } }; imageLayout.setWillNotDraw(false); imageLayout.addView(imageView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL)); addView(imageLayout, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 12, 0, 6)); headerTextView.setText(LocaleController.getString("TooManyCommunities", R.string.TooManyCommunities)); imageView.setImageResource(R.drawable.groups_limit1); }
Example 13
Source File: AutoResizeTextView.java From juicessh-performancemonitor with Apache License 2.0 | 4 votes |
/** * Resize the text size with specified width and height * @param width * @param height */ public void resizeText(int width, int height) { CharSequence text = getText(); // Do not resize if the view does not have dimensions or there is no text if(text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) { return; } // Get the text view's paint object TextPaint textPaint = getPaint(); // Store the current text size float oldTextSize = textPaint.getTextSize(); // If there is a max text size set, use the lesser of that and the default text size float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize; // Get the required text height int textHeight = getTextHeight(text, textPaint, width, targetTextSize); // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes while(textHeight > height && targetTextSize > mMinTextSize) { targetTextSize = Math.max(targetTextSize - 2, mMinTextSize); textHeight = getTextHeight(text, textPaint, width, targetTextSize); } // If we had reached our minimum text size and still don't fit, append an ellipsis if(mAddEllipsis && targetTextSize == mMinTextSize && textHeight > height) { // Draw using a static layout StaticLayout layout = new StaticLayout(text, textPaint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); // Check that we have a least one line of rendered text if(layout.getLineCount() > 0) { // Since the line at the specific vertical position would be cut off, // we must trim up to the previous line int lastLine = layout.getLineForVertical(height) - 1; // If the text would not even fit on a single line, clear it if(lastLine < 0) { setText(""); } // Otherwise, trim to the previous line and add an ellipsis else { int start = layout.getLineStart(lastLine); int end = layout.getLineEnd(lastLine); float lineWidth = layout.getLineWidth(lastLine); float ellipseWidth = textPaint.measureText(mEllipsis); // Trim characters off until we have enough room to draw the ellipsis while(width < lineWidth + ellipseWidth) { lineWidth = textPaint.measureText(text.subSequence(start, --end + 1).toString()); } setText(text.subSequence(0, end) + mEllipsis); } } } // Some devices try to auto adjust line spacing, so force default line spacing // and invalidate the layout as a side effect textPaint.setTextSize(targetTextSize); setLineSpacing(mSpacingAdd, mSpacingMult); // Notify the listener if registered if(mTextResizeListener != null) { mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize); } // Reset force resize flag mNeedsResize = false; }
Example 14
Source File: AutoResizeTextView.java From oneHookLibraryAndroid with Apache License 2.0 | 4 votes |
/** * Resize the text size with specified width and height * @param width * @param height */ public void resizeText(int width, int height) { CharSequence text = getText(); // Do not resize if the view does not have dimensions or there is no text if (text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) { return; } if (getTransformationMethod() != null) { text = getTransformationMethod().getTransformation(text, this); } // Get the text view's paint object TextPaint textPaint = getPaint(); // Store the current text size float oldTextSize = textPaint.getTextSize(); // If there is a max text size set, use the lesser of that and the default text size float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize; // Get the required text height int textHeight = getTextHeight(text, textPaint, width, targetTextSize); // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes while (textHeight > height && targetTextSize > mMinTextSize) { targetTextSize = Math.max(targetTextSize - 2, mMinTextSize); textHeight = getTextHeight(text, textPaint, width, targetTextSize); } // If we had reached our minimum text size and still don't fit, append an ellipsis if (mAddEllipsis && targetTextSize == mMinTextSize && textHeight > height) { // Draw using a static layout // modified: use a copy of TextPaint for measuring TextPaint paint = new TextPaint(textPaint); // Draw using a static layout StaticLayout layout = new StaticLayout(text, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); // Check that we have a least one line of rendered text if (layout.getLineCount() > 0) { // Since the line at the specific vertical position would be cut off, // we must trim up to the previous line int lastLine = layout.getLineForVertical(height) - 1; // If the text would not even fit on a single line, clear it if (lastLine < 0) { setText(""); } // Otherwise, trim to the previous line and add an ellipsis else { int start = layout.getLineStart(lastLine); int end = layout.getLineEnd(lastLine); float lineWidth = layout.getLineWidth(lastLine); float ellipseWidth = textPaint.measureText(mEllipsis); // Trim characters off until we have enough room to draw the ellipsis while (width < lineWidth + ellipseWidth) { lineWidth = textPaint.measureText(text.subSequence(start, --end + 1).toString()); } setText(text.subSequence(0, end) + mEllipsis); } } } // Some devices try to auto adjust line spacing, so force default line spacing // and invalidate the layout as a side effect setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize); setLineSpacing(mSpacingAdd, mSpacingMult); // Notify the listener if registered if (mTextResizeListener != null) { mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize); } // Reset force resize flag mNeedsResize = false; }
Example 15
Source File: Slidr.java From android-slidr with Apache License 2.0 | 4 votes |
private float calculateTextMultilineHeight(String text, TextPaint textPaint) { return text.split("\n").length * textPaint.getTextSize(); }
Example 16
Source File: TooManyCommunitiesHintCell.java From Telegram-FOSS with GNU General Public License v2.0 | 4 votes |
public TooManyCommunitiesHintCell(Context context) { super(context); imageView = new ImageView(context); imageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chats_nameMessage_threeLines), PorterDuff.Mode.MULTIPLY)); headerTextView = new TextView(context); headerTextView.setTextColor(Theme.getColor(Theme.key_chats_nameMessage_threeLines)); headerTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); headerTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf")); headerTextView.setGravity(Gravity.CENTER); addView(headerTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT, 52, 75, 52, 0)); messageTextView = new TextView(context); messageTextView.setTextColor(Theme.getColor(Theme.key_chats_message)); messageTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14); messageTextView.setGravity(Gravity.CENTER); addView(messageTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT, 36, 110, 36, 0)); TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(Color.WHITE); textPaint.setTextSize(AndroidUtilities.dp(12)); textPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf")); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); String s = "500"; imageLayout = new FrameLayout(context) { RectF rect = new RectF(); @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setColor(Theme.getColor(Theme.key_windowBackgroundWhiteRedText)); canvas.save(); canvas.translate(getMeasuredWidth() - textPaint.measureText(s) - AndroidUtilities.dp(8), AndroidUtilities.dpf2(7f)); rect.set(0, 0, textPaint.measureText(s), textPaint.getTextSize()); rect.inset(-AndroidUtilities.dp(6), -AndroidUtilities.dp(3)); float r = (textPaint.getTextSize()) / 2f + AndroidUtilities.dp(3); canvas.drawRoundRect(rect, r, r, paint); canvas.drawText(s, 0, textPaint.getTextSize() - AndroidUtilities.dpf2(2f), textPaint); canvas.restore(); } }; imageLayout.setWillNotDraw(false); imageLayout.addView(imageView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL)); addView(imageLayout, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 12, 0, 6)); headerTextView.setText(LocaleController.getString("TooManyCommunities", R.string.TooManyCommunities)); imageView.setImageResource(R.drawable.groups_limit1); }
Example 17
Source File: Sushi.java From android-slidr with Apache License 2.0 | 4 votes |
private float calculateTextMultilineHeight(String text, TextPaint textPaint) { return text.split("\n").length * textPaint.getTextSize(); }
Example 18
Source File: FontFitTextView.java From Pocket-Plays-for-Twitch with GNU General Public License v3.0 | 4 votes |
/** * Resize the text size with specified width and height * @param width * @param height */ public void resizeText(int width, int height) { CharSequence text = getText(); // Do not resize if the view does not have dimensions or there is no text if (text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) { return; } if (getTransformationMethod() != null) { text = getTransformationMethod().getTransformation(text, this); } // Get the text view's paint object TextPaint textPaint = getPaint(); // Store the current text size float oldTextSize = textPaint.getTextSize(); // If there is a max text size set, use the lesser of that and the default text size float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize; // Get the required text height int textHeight = getTextHeight(text, textPaint, width, targetTextSize); // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes while (textHeight > height && targetTextSize > mMinTextSize) { targetTextSize = Math.max(targetTextSize - 2, mMinTextSize); textHeight = getTextHeight(text, textPaint, width, targetTextSize); } // If we had reached our minimum text size and still don't fit, append an ellipsis if (mAddEllipsis && targetTextSize == mMinTextSize && textHeight > height) { // Draw using a static layout // modified: use a copy of TextPaint for measuring TextPaint paint = new TextPaint(textPaint); // Draw using a static layout StaticLayout layout = new StaticLayout(text, paint, width, Layout.Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); // Check that we have a least one line of rendered text if (layout.getLineCount() > 0) { // Since the line at the specific vertical position would be cut off, // we must trim up to the previous line int lastLine = layout.getLineForVertical(height) - 1; // If the text would not even fit on a single line, clear it if (lastLine < 0) { setText(""); } // Otherwise, trim to the previous line and add an ellipsis else { int start = layout.getLineStart(lastLine); int end = layout.getLineEnd(lastLine); float lineWidth = layout.getLineWidth(lastLine); float ellipseWidth = textPaint.measureText(mEllipsis); // Trim characters off until we have enough room to draw the ellipsis while (width < lineWidth + ellipseWidth) { lineWidth = textPaint.measureText(text.subSequence(start, --end + 1).toString()); } setText(text.subSequence(0, end) + mEllipsis); } } } // Some devices try to auto adjust line spacing, so force default line spacing // and invalidate the layout as a side effect setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize); setLineSpacing(mSpacingAdd, mSpacingMult); // Notify the listener if registered if (mTextResizeListener != null) { mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize); } // Reset force resize flag mNeedsResize = false; }
Example 19
Source File: FileTypeTextView.java From Aria2App with GNU General Public License v3.0 | 4 votes |
public static float getFitTextSize(@NonNull TextPaint paint, float width, @NonNull String text) { float nowWidth = paint.measureText(text); return width / nowWidth * paint.getTextSize(); }
Example 20
Source File: FontFitTextView.java From Twire with GNU General Public License v3.0 | 4 votes |
/** * Resize the text size with specified width and height * * @param width * @param height */ public void resizeText(int width, int height) { CharSequence text = getText(); // Do not resize if the view does not have dimensions or there is no text if (text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) { return; } if (getTransformationMethod() != null) { text = getTransformationMethod().getTransformation(text, this); } // Get the text view's paint object TextPaint textPaint = getPaint(); // Store the current text size float oldTextSize = textPaint.getTextSize(); // If there is a max text size set, use the lesser of that and the default text size float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize; // Get the required text height int textHeight = getTextHeight(text, textPaint, width, targetTextSize); // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes while (textHeight > height && targetTextSize > mMinTextSize) { targetTextSize = Math.max(targetTextSize - 2, mMinTextSize); textHeight = getTextHeight(text, textPaint, width, targetTextSize); } // If we had reached our minimum text size and still don't fit, append an ellipsis if (mAddEllipsis && targetTextSize == mMinTextSize && textHeight > height) { // Draw using a static layout // modified: use a copy of TextPaint for measuring TextPaint paint = new TextPaint(textPaint); // Draw using a static layout StaticLayout layout = new StaticLayout(text, paint, width, Layout.Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); // Check that we have a least one line of rendered text if (layout.getLineCount() > 0) { // Since the line at the specific vertical position would be cut off, // we must trim up to the previous line int lastLine = layout.getLineForVertical(height) - 1; // If the text would not even fit on a single line, clear it if (lastLine < 0) { setText(""); } // Otherwise, trim to the previous line and add an ellipsis else { int start = layout.getLineStart(lastLine); int end = layout.getLineEnd(lastLine); float lineWidth = layout.getLineWidth(lastLine); float ellipseWidth = textPaint.measureText(mEllipsis); // Trim characters off until we have enough room to draw the ellipsis while (width < lineWidth + ellipseWidth) { lineWidth = textPaint.measureText(text.subSequence(start, --end + 1).toString()); } setText(text.subSequence(0, end) + mEllipsis); } } } // Some devices try to auto adjust line spacing, so force default line spacing // and invalidate the layout as a side effect setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize); setLineSpacing(mSpacingAdd, mSpacingMult); // Notify the listener if registered if (mTextResizeListener != null) { mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize); } // Reset force resize flag mNeedsResize = false; }