Search in sources :

Example 66 with StaticLayout

use of android.text.StaticLayout in project android_frameworks_base by DirtyUnicorns.

the class SubtitleView method onMeasure.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int widthSpec = MeasureSpec.getSize(widthMeasureSpec);
    if (computeMeasurements(widthSpec)) {
        final StaticLayout layout = mLayout;
        // Account for padding.
        final int paddingX = mPaddingLeft + mPaddingRight + mInnerPaddingX * 2;
        final int width = layout.getWidth() + paddingX;
        final int height = layout.getHeight() + mPaddingTop + mPaddingBottom;
        setMeasuredDimension(width, height);
    } else {
        setMeasuredDimension(MEASURED_STATE_TOO_SMALL, MEASURED_STATE_TOO_SMALL);
    }
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 67 with StaticLayout

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;
        }
    }
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint)

Example 68 with StaticLayout

use of android.text.StaticLayout in project android_frameworks_base by AOSPA.

the class SubtitleView method onDraw.

@Override
protected void onDraw(Canvas c) {
    final StaticLayout layout = mLayout;
    if (layout == null) {
        return;
    }
    final int saveCount = c.save();
    final int innerPaddingX = mInnerPaddingX;
    c.translate(mPaddingLeft + innerPaddingX, mPaddingTop);
    final int lineCount = layout.getLineCount();
    final Paint textPaint = mTextPaint;
    final Paint paint = mPaint;
    final RectF bounds = mLineBounds;
    if (Color.alpha(mBackgroundColor) > 0) {
        final float cornerRadius = mCornerRadius;
        float previousBottom = layout.getLineTop(0);
        paint.setColor(mBackgroundColor);
        paint.setStyle(Style.FILL);
        for (int i = 0; i < lineCount; i++) {
            bounds.left = layout.getLineLeft(i) - innerPaddingX;
            bounds.right = layout.getLineRight(i) + innerPaddingX;
            bounds.top = previousBottom;
            bounds.bottom = layout.getLineBottom(i);
            previousBottom = bounds.bottom;
            c.drawRoundRect(bounds, cornerRadius, cornerRadius, paint);
        }
    }
    final int edgeType = mEdgeType;
    if (edgeType == CaptionStyle.EDGE_TYPE_OUTLINE) {
        textPaint.setStrokeJoin(Join.ROUND);
        textPaint.setStrokeWidth(mOutlineWidth);
        textPaint.setColor(mEdgeColor);
        textPaint.setStyle(Style.FILL_AND_STROKE);
        for (int i = 0; i < lineCount; i++) {
            layout.drawText(c, i, i);
        }
    } else if (edgeType == CaptionStyle.EDGE_TYPE_DROP_SHADOW) {
        textPaint.setShadowLayer(mShadowRadius, mShadowOffsetX, mShadowOffsetY, mEdgeColor);
    } else if (edgeType == CaptionStyle.EDGE_TYPE_RAISED || edgeType == CaptionStyle.EDGE_TYPE_DEPRESSED) {
        final boolean raised = edgeType == CaptionStyle.EDGE_TYPE_RAISED;
        final int colorUp = raised ? Color.WHITE : mEdgeColor;
        final int colorDown = raised ? mEdgeColor : Color.WHITE;
        final float offset = mShadowRadius / 2f;
        textPaint.setColor(mForegroundColor);
        textPaint.setStyle(Style.FILL);
        textPaint.setShadowLayer(mShadowRadius, -offset, -offset, colorUp);
        for (int i = 0; i < lineCount; i++) {
            layout.drawText(c, i, i);
        }
        textPaint.setShadowLayer(mShadowRadius, offset, offset, colorDown);
    }
    textPaint.setColor(mForegroundColor);
    textPaint.setStyle(Style.FILL);
    for (int i = 0; i < lineCount; i++) {
        layout.drawText(c, i, i);
    }
    textPaint.setShadowLayer(0, 0, 0, 0);
    c.restoreToCount(saveCount);
}
Also used : RectF(android.graphics.RectF) StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 69 with StaticLayout

use of android.text.StaticLayout in project UltimateAndroid by cymcsg.

the class WeekView method drawText.

/**
     * Draw the name of the event on top of the event rectangle.
     * @param text The text to draw.
     * @param rect The rectangle on which the text is to be drawn.
     * @param canvas The canvas to draw upon.
     * @param originalTop The original top position of the rectangle. The rectangle may have some of its portion outside of the visible area.
     * @param originalLeft The original left position of the rectangle. The rectangle may have some of its portion outside of the visible area.
     */
private void drawText(String text, RectF rect, Canvas canvas, float originalTop, float originalLeft) {
    if (rect.right - rect.left - mEventPadding * 2 < 0)
        return;
    // Get text dimensions
    StaticLayout textLayout = new StaticLayout(text, mEventTextPaint, (int) (rect.right - originalLeft - mEventPadding * 2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    // Crop height
    int availableHeight = (int) (rect.bottom - originalTop - mEventPadding * 2);
    int lineHeight = textLayout.getHeight() / textLayout.getLineCount();
    if (lineHeight < availableHeight && textLayout.getHeight() > rect.height() - mEventPadding * 2) {
        int lineCount = textLayout.getLineCount();
        int availableLineCount = (int) Math.floor(lineCount * availableHeight / textLayout.getHeight());
        float widthAvailable = (rect.right - originalLeft - mEventPadding * 2) * availableLineCount;
        textLayout = new StaticLayout(TextUtils.ellipsize(text, mEventTextPaint, widthAvailable, TextUtils.TruncateAt.END), mEventTextPaint, (int) (rect.right - originalLeft - mEventPadding * 2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    } else if (lineHeight >= availableHeight) {
        int width = (int) (rect.right - originalLeft - mEventPadding * 2);
        textLayout = new StaticLayout(TextUtils.ellipsize(text, mEventTextPaint, width, TextUtils.TruncateAt.END), mEventTextPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 1.0f, false);
    }
    // Draw text
    canvas.save();
    canvas.translate(originalLeft + mEventPadding, originalTop + mEventPadding);
    textLayout.draw(canvas);
    canvas.restore();
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 70 with StaticLayout

use of android.text.StaticLayout in project DanmakuFlameMaster by Bilibili.

the class SpannedCacheStuffer method measure.

@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);
}
Also used : StaticLayout(android.text.StaticLayout) Spanned(android.text.Spanned)

Aggregations

StaticLayout (android.text.StaticLayout)80 TextPaint (android.text.TextPaint)56 Paint (android.graphics.Paint)45 Layout (android.text.Layout)21 DynamicLayout (android.text.DynamicLayout)9 RectF (android.graphics.RectF)6 Bitmap (android.graphics.Bitmap)3 NonNull (android.support.annotation.NonNull)3 BoringLayout (android.text.BoringLayout)3 SpannableStringBuilder (android.text.SpannableStringBuilder)3 KeyEvent (android.view.KeyEvent)3 MotionEvent (android.view.MotionEvent)3 AccessibilityEvent (android.view.accessibility.AccessibilityEvent)3 SuppressLint (android.annotation.SuppressLint)2 Canvas (android.graphics.Canvas)2 Rect (android.graphics.Rect)2 Spanned (android.text.Spanned)2 TruncateAt (android.text.TextUtils.TruncateAt)2 StyleSpan (android.text.style.StyleSpan)2 Animator (android.animation.Animator)1