// Set the text size of the text paint object and use a static layout to render text off screen before measuring private int getTextWidth(CharSequence source, TextPaint paint, int width, float textSize) { // Update the text paint object paint.setTextSize(textSize); // Draw using a static layout StaticLayout layout = new StaticLayout(source, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true); layout.draw(sTextResizeCanvas); return layout.getWidth(); }
@Override public void measure(BaseDanmaku danmaku, TextPaint paint, boolean fromWorkerThread) { if (danmaku.text instanceof Spanned) { CharSequence text = danmaku.text; if (text != null) { StaticLayout staticLayout = new StaticLayout(text, paint, (int) Math.ceil(StaticLayout.getDesiredWidth(danmaku.text, paint)), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true); danmaku.paintWidth = staticLayout.getWidth(); danmaku.paintHeight = staticLayout.getHeight(); danmaku.obj = new SoftReference<>(staticLayout); return; } } super.measure(danmaku, paint, fromWorkerThread); }
public boolean testSize(float suggestedSize, RectF availableSpace) { paint.setTextSize(suggestedSize); paint.setTypeface(getTypeface()); String text = getText().toString(); if (maxLines == 1) { textRect.bottom = paint.getFontSpacing(); textRect.right = paint.measureText(text); return availableSpace.width() >= textRect.right && availableSpace.height() >= textRect.bottom; } else { StaticLayout layout = new StaticLayout(text, paint, (int) availableSpace.right, Layout.Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, true); if (maxLines != -1 && layout.getLineCount() > maxLines) return false; return availableSpace.width() >= layout.getWidth() && availableSpace.height() >= layout.getHeight(); } }
drawWidth = textLayout.getWidth(); drawHeight = textLayout.getLineCount() * lineHeight;
public boolean testSize(float suggestedSize, RectF availableSpace) { paint.setTextSize(suggestedSize); paint.setTypeface(getTypeface()); String text = getText().toString(); if (maxLines == 1) { textRect.bottom = paint.getFontSpacing(); textRect.right = paint.measureText(text); return availableSpace.width() >= textRect.right && availableSpace.height() >= textRect.bottom; } else { StaticLayout layout = new StaticLayout(text, paint, (int) availableSpace.right, Layout.Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, true); if (maxLines != -1 && layout.getLineCount() > maxLines) return false; return availableSpace.width() >= layout.getWidth() && availableSpace.height() >= layout.getHeight(); } }
public boolean testSize(float suggestedSize, RectF availableSpace) { paint.setTextSize(suggestedSize); paint.setTypeface(getTypeface()); String text = getText().toString(); if (maxLines == 1) { textRect.bottom = paint.getFontSpacing(); textRect.right = paint.measureText(text); return availableSpace.width() >= textRect.right && availableSpace.height() >= textRect.bottom; } else { StaticLayout layout = new StaticLayout(text, paint, (int) availableSpace.right, Layout.Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, true); if (maxLines != -1 && layout.getLineCount() > maxLines) return false; return availableSpace.width() >= layout.getWidth() && availableSpace.height() >= layout.getHeight(); } }
if (requestRemeasure) { staticLayout = new StaticLayout(text, paint, (int) Math.ceil(StaticLayout.getDesiredWidth(danmaku.text, paint)), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true); danmaku.paintWidth = staticLayout.getWidth(); danmaku.paintHeight = staticLayout.getHeight(); danmaku.requestFlags &= ~BaseDanmaku.FLAG_REQUEST_REMEASURE;
canvas.drawRect(-textPaddingX, 0, layout.getWidth() + textPaddingX, layout.getHeight(), paint);
@Test public void testCreateStaticTextLayout() { final TextPaint paint = new TextPaint(); final StaticLayout layout = PromptUtils.createStaticTextLayout("test", paint, 300, Layout.Alignment.ALIGN_CENTER, 0.5f); assertNotNull(layout); assertEquals("test", layout.getText().toString()); assertEquals(paint, layout.getPaint()); assertEquals(Layout.Alignment.ALIGN_CENTER, layout.getAlignment()); assertEquals(300, layout.getWidth()); }
// Set the text size of the text paint object and use a static layout to render text off screen before measuring private int getTextWidth(CharSequence source, TextPaint paint, int width, float textSize) { // Update the text paint object paint.setTextSize(textSize); // Draw using a static layout StaticLayout layout = new StaticLayout(source, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true); layout.draw(sTextResizeCanvas); return layout.getWidth(); }
StaticLayout mTextLayout = new StaticLayout(text, mTextPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); canvas.translate((canvas.getWidth() / 2) - (mTextLayout.getWidth() / 2), (canvas.getHeight() / 2) - ((mTextLayout.getHeight() / 2)));
private void resetSize() { textLayout = new StaticLayout(text, textPaint, textLayoutWidth, Layout.Alignment.ALIGN_CENTER, 1.0F, 0.0F, true); textWidth = minWidth; textHeight = minHeight; if (textWidth < textLayout.getWidth()) { textWidth = textLayout.getWidth(); } if (textHeight < textLayout.getHeight()) { textHeight = textLayout.getHeight(); } minScale = minWidth / textWidth; }
private void drawStopWatch(Canvas canvas) { Log.i(TAG, "draw stop watch"); TextPaint textPaint = new TextPaint(); textPaint.setTextSize(mWidth * 0.35f); textPaint.setColor(Color.WHITE); Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.proxima_nova_thin); textPaint.setTypeface(typeface); mMillisecondsTime = SystemClock.uptimeMillis() - mStartTime; mUpdateTime = mTimeBuffer + mMillisecondsTime; mSeconds = (int) (mUpdateTime / 1000); mMinutes = mSeconds / 60; mSeconds = mSeconds % 60; mMilliseconds = (int) (mUpdateTime % 1000); String value = String.format(Locale.getDefault(), "%02d", mMinutes) + ":" + String.format(Locale.getDefault(), "%02d", mSeconds); SpannableStringBuilder spannableString = new SpannableStringBuilder(value); StaticLayout layout = new StaticLayout(spannableString, textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER, 1, 1, true); canvas.translate(mCenterX - layout.getWidth() / 2, mCenterY - layout.getHeight() / 2); layout.draw(canvas); if (mStopwatchState == StopwatchState.paused) mTimeBuffer += mMillisecondsTime; }
/** * @param canvas */ private void drawNumbers(Canvas canvas) { TextPaint textPaint = new TextPaint(); textPaint.setTextSize(mWidth * 0.3f); textPaint.setColor(mNumbersColor); textPaint.setColor(mNumbersColor); Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.proxima_nova_thin); textPaint.setTypeface(typeface); Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int amPm = calendar.get(Calendar.AM_PM); String time = String.format("%s:%s%s", String.format(Locale.getDefault(), "%02d", hour), String.format(Locale.getDefault(), "%02d", minute), amPm == AM ? "AM" : "PM"); SpannableStringBuilder spannableString = new SpannableStringBuilder(time); spannableString.setSpan(new RelativeSizeSpan(0.3f), spannableString.toString().length() - 2, spannableString.toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // se superscript percent StaticLayout layout = new StaticLayout(spannableString, textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER, 1, 1, true); canvas.translate(mCenterX - layout.getWidth() / 2, mCenterY - layout.getHeight() / 2); layout.draw(canvas); }
private void configureTextLayouts(final int availableWidth) { if (!textLayoutsConfigured) { final int totalNeededPadding = getPaddingLeft() + getPaddingRight(); // Create new static layout only if needed! if ((titleLayout.getWidth() + totalNeededPadding) > availableWidth) { this.titleLayout = new StaticLayout(title, titlePaint, availableWidth - totalNeededPadding, Layout.Alignment.ALIGN_NORMAL, 1.15f, 0, false); } // Create new static layout only if needed! if ((subtitleLayout.getWidth() + totalNeededPadding) > availableWidth) { this.subtitleLayout = new StaticLayout(subtitle, subtitlePaint, availableWidth - totalNeededPadding, Layout.Alignment.ALIGN_NORMAL, 1.15f, 0, false); } textLayoutsConfigured = true; } } }
private void ensureExpandedTexture() { if (mExpandedTitleTexture != null || mExpandedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) { return; } calculateOffsets(0f); // BEGIN MODIFICATION: Calculate width and height using mTextLayout and remove // mTextureAscent and mTextureDescent assignment final int w = mTextLayout.getWidth(); final int h = mTextLayout.getHeight(); // END MODIFICATION if (w <= 0 || h <= 0) { return; // If the width or height are 0, return } mExpandedTitleTexture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); // BEGIN MODIFICATION: Draw text using mTextLayout Canvas c = new Canvas(mExpandedTitleTexture); mTextLayout.draw(c); // END MODIFICATION if (mTexturePaint == null) { // Make sure we have a paint mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); } }
private float getDx(final int width, final int horizontalGravity, final Paint paint, final StaticLayout layout) { final boolean centered = paint.getTextAlign() == Paint.Align.CENTER; final float dx; switch (horizontalGravity) { // No support for GravityCompat.END case Gravity.CENTER_HORIZONTAL: dx = (width >> 1) - (centered ? 0 : (layout.getWidth() >> 1) - getPaddingLeft()); break; default: case GravityCompat.START: dx = getPaddingLeft(); break; } return dx; }
/** * @param x * @param y * @param canvas * @param progress */ private void drawProgressValue(int x, int y, Canvas canvas, float progress) { TextPaint textPaint = new TextPaint(); textPaint.setColor(mTextValueColor); textPaint.setStyle(Paint.Style.FILL); textPaint.setTextSize(mWidth * TEXT_FACTOR); Typeface typeface = ResourcesCompat.getFont(getContext(), mTextValueFont); textPaint.setTypeface(typeface); SpannableStringBuilder spannableString = getSpannableValue(progress); StaticLayout layout = new StaticLayout(spannableString, textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER, 1, 1, true); canvas.translate(x - layout.getWidth() / 2, y - layout.getHeight() / 2); layout.draw(canvas); }