Java Code Examples for android.text.TextPaint#getTextBounds()
The following examples show how to use
android.text.TextPaint#getTextBounds() .
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: PasswordEntry.java From android-proguards with Apache License 2.0 | 6 votes |
MaskMorphDrawable(Context context, TextPaint textPaint, int baseline, float charWidth, int insetStart) { this.insetStart = insetStart; this.baseline = baseline; this.charWidth = charWidth; paint = new TextPaint(textPaint); Rect maskBounds = new Rect(); paint.getTextBounds(PASSWORD_MASK, 0, 1, maskBounds); maskDiameter = maskBounds.height(); maskCenterY = (maskBounds.top + maskBounds.bottom) / 2f; showPasswordDuration = context.getResources().getInteger(R.integer.show_password_duration); hidePasswordDuration = context.getResources().getInteger(R.integer.hide_password_duration); fastOutSlowIn = AnimUtils.getFastOutSlowInInterpolator(context); }
Example 2
Source File: CustomAbsoluteSizeSpan.java From SimplifySpan with MIT License | 6 votes |
@Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); if (mGravity == SpecialGravity.BOTTOM) return; mTextView.getPaint().getTextBounds(mNormalSizeText, 0, mNormalSizeText.length(), mTextViewRect); ds.getTextBounds(mText, 0, mText.length(), mTextRect); int mMainTextHeight = mTextViewRect.height(); int offset = mTextViewRect.bottom - mTextRect.bottom; switch (mGravity) { case SpecialGravity.TOP: mOffsetBaselineShift = mMainTextHeight - mTextRect.height() - offset; break; case SpecialGravity.CENTER: mOffsetBaselineShift = mMainTextHeight / 2 - mTextRect.height() / 2 - offset; break; } ds.baselineShift -= mOffsetBaselineShift; }
Example 3
Source File: BadgedFourThreeImageView.java From android-proguards with Apache License 2.0 | 5 votes |
GifBadge(Context context) { if (bitmap == null) { final DisplayMetrics dm = context.getResources().getDisplayMetrics(); final float density = dm.density; final float scaledDensity = dm.scaledDensity; final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint .SUBPIXEL_TEXT_FLAG); textPaint.setTypeface(Typeface.create(TYPEFACE, TYPEFACE_STYLE)); textPaint.setTextSize(TEXT_SIZE * scaledDensity); final float padding = PADDING * density; final float cornerRadius = CORNER_RADIUS * density; final Rect textBounds = new Rect(); textPaint.getTextBounds(GIF, 0, GIF.length(), textBounds); height = (int) (padding + textBounds.height() + padding); width = (int) (padding + textBounds.width() + padding); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setHasAlpha(true); final Canvas canvas = new Canvas(bitmap); final Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); backgroundPaint.setColor(BACKGROUND_COLOR); canvas.drawRoundRect(0, 0, width, height, cornerRadius, cornerRadius, backgroundPaint); // punch out the word 'GIF', leaving transparency textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawText(GIF, padding, height - padding, textPaint); } paint = new Paint(); }
Example 4
Source File: BarrageView.java From BarrageView with Apache License 2.0 | 5 votes |
/** * 计算TextView中字符串的长度 * * @param text 要计算的字符串 * @param Size 字体大小 * @return TextView中字符串的长度 */ public float getTextWidth(BarrageItem item, String text, float Size) { Rect bounds = new Rect(); TextPaint paint; paint = item.textView.getPaint(); paint.getTextBounds(text, 0, text.length(), bounds); return bounds.width(); }
Example 5
Source File: BitmapUtils.java From hr with GNU Affero General Public License v3.0 | 5 votes |
public static Bitmap getAlphabetImage(Context context, String content) { Resources res = context.getResources(); Bitmap mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.sym_def_app_icon); int width = mDefaultBitmap.getWidth(); int height = mDefaultBitmap.getHeight(); TextPaint mPaint = new TextPaint(); mPaint.setTypeface(OControlHelper.boldFont()); mPaint.setColor(Color.WHITE); mPaint.setTextAlign(Paint.Align.CENTER); mPaint.setAntiAlias(true); int textSize = res.getDimensionPixelSize(R.dimen.text_size_xxlarge); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(); Rect mBounds = new Rect(); canvas.setBitmap(bitmap); canvas.drawColor(OStringColorUtil.getStringColor(context, content)); if (content == null || content.trim().length() == 0) { content = "?"; } char[] alphabet = {Character.toUpperCase(content.trim().charAt(0))}; mPaint.setTextSize(textSize); mPaint.getTextBounds(alphabet, 0, 1, mBounds); canvas.drawText(alphabet, 0, 1, 0 + width / 2, 0 + height / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint); return bitmap; }
Example 6
Source File: TextChipsEditView.java From talk-android with MIT License | 5 votes |
/** * Given a height, returns a Y offset that will draw the text in the middle of the height. */ protected float getTextYOffset(String text, TextPaint paint, int height) { Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); int textHeight = bounds.bottom - bounds.top; return height - ((height - textHeight) / 2) - (int) paint.descent() / 2; }
Example 7
Source File: RecipientEditTextView.java From ChipsLibrary with Apache License 2.0 | 5 votes |
private static float getTextYOffset(final String text,final TextPaint paint,final int height) { final Rect bounds=new Rect(); paint.getTextBounds(text,0,text.length(),bounds); final int textHeight=bounds.bottom-bounds.top; return height-(height-textHeight)/2-(int)paint.descent(); }
Example 8
Source File: BadgedFourThreeImageView.java From Protein with Apache License 2.0 | 5 votes |
GifBadge(Context context) { if (bitmap == null) { final DisplayMetrics dm = context.getResources().getDisplayMetrics(); final float density = dm.density; final float scaledDensity = dm.scaledDensity; final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint .SUBPIXEL_TEXT_FLAG); textPaint.setTypeface(Typeface.create(TYPEFACE, TYPEFACE_STYLE)); textPaint.setTextSize(TEXT_SIZE * scaledDensity); final float padding = PADDING * density; final float cornerRadius = CORNER_RADIUS * density; final Rect textBounds = new Rect(); textPaint.getTextBounds(GIF, 0, GIF.length(), textBounds); height = (int) (padding + textBounds.height() + padding); width = (int) (padding + textBounds.width() + padding); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setHasAlpha(true); final Canvas canvas = new Canvas(bitmap); final Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); backgroundPaint.setColor(BACKGROUND_COLOR); canvas.drawRoundRect(0, 0, width, height, cornerRadius, cornerRadius, backgroundPaint); // punch out the word 'GIF', leaving transparency textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawText(GIF, padding, height - padding, textPaint); } paint = new Paint(); }
Example 9
Source File: OBUtils.java From GLEXP-Team-onebillion with Apache License 2.0 | 5 votes |
public static float getFontCapHeight (Typeface font, float fontSize) { TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); textPaint.setTypeface(font); textPaint.setTextSize(fontSize); Rect tempRect = new Rect(); textPaint.getTextBounds("H", 0, 1, tempRect); return tempRect.height(); }
Example 10
Source File: PasswordEntry.java From android-proguards with Apache License 2.0 | 5 votes |
PasswordCharacter(String password, int index, TextPaint paint, float maskCharDiameter, float maskCenterY) { paint.getTextBounds(password, index, index + 1, bounds); // scale the mask from the character width, down to it's own width maskToTextScale = Math.max(1f, bounds.width() / maskCharDiameter); // scale text from it's height down to the mask character height textToMaskScale = Math.min(0f, 1f / (bounds.height() / maskCharDiameter)); // difference between mask & character center textOffsetY = maskCenterY - bounds.exactCenterY(); }
Example 11
Source File: FontMetricsUtil.java From react-native-GPay with MIT License | 5 votes |
public static WritableArray getFontMetrics(CharSequence text, Layout layout, TextPaint paint, Context context) { DisplayMetrics dm = context.getResources().getDisplayMetrics(); WritableArray lines = Arguments.createArray(); // To calculate xHeight and capHeight we have to render an "x" and "T" and manually measure their height. // In order to get more precision than Android offers, we blow up the text size by 100 and measure it. // Luckily, text size affects rendering linearly, so we can do this trick. TextPaint paintCopy = new TextPaint(paint); paintCopy.setTextSize(paintCopy.getTextSize() * AMPLIFICATION_FACTOR); Rect capHeightBounds = new Rect(); paintCopy.getTextBounds(CAP_HEIGHT_MEASUREMENT_TEXT, 0, CAP_HEIGHT_MEASUREMENT_TEXT.length(), capHeightBounds); double capHeight = capHeightBounds.height() / AMPLIFICATION_FACTOR / dm.density; Rect xHeightBounds = new Rect(); paintCopy.getTextBounds(X_HEIGHT_MEASUREMENT_TEXT, 0, X_HEIGHT_MEASUREMENT_TEXT.length(), xHeightBounds); double xHeight = xHeightBounds.height() / AMPLIFICATION_FACTOR / dm.density; for (int i = 0; i < layout.getLineCount(); i++) { Rect bounds = new Rect(); layout.getLineBounds(i, bounds); WritableMap line = Arguments.createMap(); line.putDouble("x", layout.getLineLeft(i) / dm.density); line.putDouble("y", bounds.top / dm.density); line.putDouble("width", layout.getLineWidth(i) / dm.density); line.putDouble("height", bounds.height() / dm.density); line.putDouble("descender", layout.getLineDescent(i) / dm.density); line.putDouble("ascender", -layout.getLineAscent(i) / dm.density); line.putDouble("baseline", layout.getLineBaseline(i) / dm.density); line.putDouble("capHeight", capHeight); line.putDouble("xHeight", xHeight); line.putString( "text", text.subSequence(layout.getLineStart(i), layout.getLineEnd(i)).toString()); lines.pushMap(line); } return lines; }
Example 12
Source File: Clock.java From Clock-view with Apache License 2.0 | 5 votes |
private void drawMinutesValues(Canvas canvas) { if (!showMinutesValues) return; Rect rect = new Rect(); Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.proxima_nova_thin); TextPaint textPaint = new TextPaint(); textPaint.setColor(minutesProgressColor); textPaint.setTypeface(typeface); textPaint.setTextSize(size * MINUTES_TEXT_SIZE); int rText = (int) (centerX - ((1 - minutesValuesFactor - (2 * DEFAULT_BORDER_THICKNESS) - MINUTES_TEXT_SIZE) * radius)); for (int i = 0; i < FULL_ANGLE; i = i + QUARTER_DEGREE_STEPS) { int value = i / 6; String formatted; switch (valueType) { case arabic: formatted = ClockUtils.toArabic(value); break; case roman: formatted = ClockUtils.toRoman(value); break; default: formatted = String.format(Locale.getDefault(), "%02d", value); break; } int textX = (int) (centerX + rText * Math.cos(Math.toRadians(REGULAR_ANGLE - i))); int textY = (int) (centerX - rText * Math.sin(Math.toRadians(REGULAR_ANGLE - i))); textPaint.getTextBounds(formatted, 0, formatted.length(), rect); canvas.drawText(formatted, textX - rect.width() / formatted.length(), textY + rect.height() / formatted.length(), textPaint); } }
Example 13
Source File: BitmapUtils.java From AndroidWM with Apache License 2.0 | 4 votes |
/** * build a bitmap from a text. * * @return {@link Bitmap} the bitmap return. */ public static Bitmap textAsBitmap(Context context, WatermarkText watermarkText) { TextPaint watermarkPaint = new TextPaint(); watermarkPaint.setColor(watermarkText.getTextColor()); watermarkPaint.setStyle(watermarkText.getTextStyle()); if (watermarkText.getTextAlpha() >= 0 && watermarkText.getTextAlpha() <= 255) { watermarkPaint.setAlpha(watermarkText.getTextAlpha()); } float value = (float) watermarkText.getTextSize(); int pixel = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics()); watermarkPaint.setTextSize(pixel); if (watermarkText.getTextShadowBlurRadius() != 0 || watermarkText.getTextShadowXOffset() != 0 || watermarkText.getTextShadowYOffset() != 0) { watermarkPaint.setShadowLayer(watermarkText.getTextShadowBlurRadius(), watermarkText.getTextShadowXOffset(), watermarkText.getTextShadowYOffset(), watermarkText.getTextShadowColor()); } if (watermarkText.getTextFont() != 0) { Typeface typeface = ResourcesCompat.getFont(context, watermarkText.getTextFont()); watermarkPaint.setTypeface(typeface); } watermarkPaint.setAntiAlias(true); watermarkPaint.setTextAlign(Paint.Align.LEFT); watermarkPaint.setStrokeWidth(5); float baseline = (int) (-watermarkPaint.ascent() + 1f); Rect bounds = new Rect(); watermarkPaint.getTextBounds(watermarkText.getText(), 0, watermarkText.getText().length(), bounds); int boundWidth = bounds.width() + 20; int mTextMaxWidth = (int) watermarkPaint.measureText(watermarkText.getText()); if (boundWidth > mTextMaxWidth) { boundWidth = mTextMaxWidth; } StaticLayout staticLayout = new StaticLayout(watermarkText.getText(), 0, watermarkText.getText().length(), watermarkPaint, mTextMaxWidth, android.text.Layout.Alignment.ALIGN_NORMAL, 2.0f, 2.0f, false); int lineCount = staticLayout.getLineCount(); int height = (int) (baseline + watermarkPaint.descent() + 3) * lineCount; Bitmap image = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); if (boundWidth > 0 && height > 0) { image = Bitmap.createBitmap(boundWidth, height, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(image); canvas.drawColor(watermarkText.getBackgroundColor()); staticLayout.draw(canvas); return image; }
Example 14
Source File: WaterMarkCreator.java From WatermarkCreator with Apache License 2.0 | 4 votes |
public Bitmap convertToBitmapFromText() { if (mTextSize == 0.0F) { throw new WaterMarkCreatorException("Did not provide the text size"); } if (mTextColor == 0) { throw new WaterMarkCreatorException("Did not provide the text color"); } TextPaint paint = new TextPaint(); paint.setColor(mTextColor); paint.setTextSize(mTextSize); paint.setStrokeWidth(5); paint.setTypeface(Typeface.MONOSPACE); paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.LEFT); //ascent : The recommended distance above the baseline for singled spaced text float baseline = (int) (-paint.ascent() + 3f); // ascent() is negative Log.e("test", " " + paint.ascent() + " baseline: " + baseline); // First decode with Rect to check dimensions Rect bounds = new Rect(); paint.getTextBounds(mText.toString(), 0, mText.length(), bounds); int boundWidth = bounds.width() + MARGIN_RIGHT; // mRequestWidth must be in pixels if (boundWidth > mTextMaxWidth) { boundWidth = mTextMaxWidth; } StaticLayout staticLayout = new StaticLayout(mText, 0, mText.length(), paint, mTextMaxWidth, android.text.Layout.Alignment.ALIGN_NORMAL, 1.0f, 1.0f, false); int lineCount = staticLayout.getLineCount(); //Descent: The recommended distance below the baseline for singled spaced text int height = (int) (baseline + paint.descent() + 3) * lineCount + 10; Bitmap image = Bitmap.createBitmap(boundWidth, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(image); canvas.drawARGB(0xFF, 0xFF, 0xFF, 0xFF); staticLayout.draw(canvas); return image; }
Example 15
Source File: WaveTextView.java From WanAndroid with Apache License 2.0 | 4 votes |
private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaveTextView); mText = typedArray.getString(R.styleable.WaveTextView_text); mTextSize = typedArray.getDimension(R.styleable.WaveTextView_size, 26); mTextColor = typedArray.getColor(R.styleable.WaveTextView_color, Color.BLACK); A = typedArray.getInteger(R.styleable.WaveTextView_amplitude, 100); isBold = typedArray.getInteger(R.styleable.WaveTextView_style, 0); typedArray.recycle(); mTextPaint = new TextPaint(); mTextPaint.setAntiAlias(true); mTextPaint.setTextSize(mTextSize); mTextPaint.setColor(mTextColor); if(isBold == 1) mTextPaint.setFakeBoldText(true); //mTextPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); mTextPath = new Path(); mTextWidth = (int) mTextPaint.measureText(mText); Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt(); mTextHeight = fontMetrics.bottom - fontMetrics.top; Rect rect = new Rect(); mTextPaint.getTextBounds(mText, 0, mText.length(), rect); mTextCenter = rect.height() / 2; //φ的取值范围为正弦函数的一个周期:2π mValueAnimator = ValueAnimator.ofFloat(0, (float) (2 * Math.PI)); mValueAnimator.setInterpolator(new LinearInterpolator()); mValueAnimator.addUpdateListener(animation -> { float progress = (float) animation.getAnimatedValue(); φ = progress; a = (float) (1 - progress / (2 * Math.PI)) * A; LogUtil.d(TAG, "addUpdateListener(), a = " + a + ", φ = " + φ); postInvalidate(); }); mValueAnimator.setDuration(2500); postDelayed(() -> mValueAnimator.start(), 100); for(int i = 0; i <= SAMPLING_SIZE; i++) { float gap = (float) mTextWidth / SAMPLING_SIZE; mSamplingX[i] = i * gap; mMapX[i] = mSamplingX[i] / (float) mTextWidth * 4 - 2; } }
Example 16
Source File: Clock.java From Clock-view with Apache License 2.0 | 4 votes |
private void drawHoursValues(Canvas canvas) { if (!showHoursValues) return; Rect rect = new Rect(); TextPaint textPaint = new TextPaint(); textPaint.setFlags(Paint.ANTI_ALIAS_FLAG); textPaint.setAntiAlias(true); textPaint.setColor(this.valuesColor); textPaint.setTypeface(this.valuesFont); textPaint.setTextSize(size * DEFAULT_HOURS_VALUES_TEXT_SIZE); float degreeSpace = 0f; if (showDegrees) degreeSpace = DEFAULT_DEGREE_STROKE_WIDTH + 0.06f; int rText = (int) (centerX - (size * DEFAULT_HOURS_VALUES_TEXT_SIZE) - (size * degreeSpace)); for (int i = FULL_ANGLE; i > 0; i = i - valueStep.getId()) { int value = i / 30; String formatted; switch (valueType) { case roman: formatted = ClockUtils.toRoman(value); break; case arabic: formatted = ClockUtils.toArabic(value); break; default: formatted = String.format(Locale.getDefault(), "%02d", value); break; } if (valueDisposition.getId() == 0) { if ((i % REGULAR_ANGLE) == 0) { textPaint.setTextSize(size * DEFAULT_HOURS_VALUES_TEXT_SIZE); textPaint.setAlpha(FULL_ALPHA); } else { textPaint.setTextSize(size * (DEFAULT_HOURS_VALUES_TEXT_SIZE - 0.03f)); textPaint.setAlpha(CUSTOM_ALPHA); } } else { textPaint.setTextSize(size * DEFAULT_HOURS_VALUES_TEXT_SIZE); textPaint.setAlpha(FULL_ALPHA); } int textX = (int) (centerX + rText * Math.cos(Math.toRadians(REGULAR_ANGLE - i))); int textY = (int) (centerX - rText * Math.sin(Math.toRadians(REGULAR_ANGLE - i))); textPaint.getTextBounds(formatted, 0, formatted.length(), rect); canvas.drawText(formatted, textX - rect.width() / formatted.length(), textY + rect.height() / formatted.length(), textPaint); } }
Example 17
Source File: DimenUtils.java From Common with Apache License 2.0 | 4 votes |
public static int getTextWidth(String str, TextView tvText) { Rect bounds = new Rect(); TextPaint paint = tvText.getPaint(); paint.getTextBounds(str, 0, str.length(), bounds); return bounds.width(); }
Example 18
Source File: JustifiedTextView.java From RxAndroidBootstrap with Apache License 2.0 | 3 votes |
/*** * this method calculate height for a line of text according to defined TextPaint * @param textPaint */ private void setLineHeight(TextPaint textPaint) { Rect bounds=new Rect(); String sampleStr="این حسین کیست که عالم همه دیوانه اوست"; textPaint.getTextBounds(sampleStr, 0,sampleStr.length(), bounds); setLineHeight(bounds.height()); }
Example 19
Source File: JustifiedTextView.java From MVPAndroidBootstrap with Apache License 2.0 | 3 votes |
/*** * this method calculate height for a line of text according to defined TextPaint * @param textPaint */ private void setLineHeight(TextPaint textPaint) { Rect bounds=new Rect(); String sampleStr="این حسین کیست که عالم همه دیوانه اوست"; textPaint.getTextBounds(sampleStr, 0,sampleStr.length(), bounds); setLineHeight(bounds.height()); }
Example 20
Source File: Utils.java From InteractiveChart with Apache License 2.0 | 2 votes |
/** * 计算文字实际占用区域 * * @param measurePaint 计算用文字的画笔 * @param rect 计算后的矩形(此矩形位文字的实际占用区域) */ public static void measureTextArea(TextPaint measurePaint, Rect rect) { String test = "9.Y"; measurePaint.getTextBounds(test, 0, test.length(), rect); }