Search in sources :

Example 56 with StaticLayout

use of android.text.StaticLayout in project Android-Week-View by alamkanak.

the class WeekView method drawEventTitle.

/**
     * Draw the name of the event on top of the event rectangle.
     * @param event The event of which the title (and location) should be drawn.
     * @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 drawEventTitle(WeekViewEvent event, RectF rect, Canvas canvas, float originalTop, float originalLeft) {
    if (rect.right - rect.left - mEventPadding * 2 < 0)
        return;
    if (rect.bottom - rect.top - mEventPadding * 2 < 0)
        return;
    // Prepare the name of the event.
    SpannableStringBuilder bob = new SpannableStringBuilder();
    if (event.getName() != null) {
        bob.append(event.getName());
        bob.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, bob.length(), 0);
        bob.append(' ');
    }
    // Prepare the location of the event.
    if (event.getLocation() != null) {
        bob.append(event.getLocation());
    }
    int availableHeight = (int) (rect.bottom - originalTop - mEventPadding * 2);
    int availableWidth = (int) (rect.right - originalLeft - mEventPadding * 2);
    // Get text dimensions.
    StaticLayout textLayout = new StaticLayout(bob, mEventTextPaint, availableWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    int lineHeight = textLayout.getHeight() / textLayout.getLineCount();
    if (availableHeight >= lineHeight) {
        // Calculate available number of line counts.
        int availableLineCount = availableHeight / lineHeight;
        do {
            // Ellipsize text to fit into event rect.
            textLayout = new StaticLayout(TextUtils.ellipsize(bob, mEventTextPaint, availableLineCount * availableWidth, TextUtils.TruncateAt.END), mEventTextPaint, (int) (rect.right - originalLeft - mEventPadding * 2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            // Reduce line count.
            availableLineCount--;
        // Repeat until text is short enough.
        } while (textLayout.getHeight() > availableHeight);
        // Draw text.
        canvas.save();
        canvas.translate(originalLeft + mEventPadding, originalTop + mEventPadding);
        textLayout.draw(canvas);
        canvas.restore();
    }
}
Also used : StyleSpan(android.text.style.StyleSpan) StaticLayout(android.text.StaticLayout) SpannableStringBuilder(android.text.SpannableStringBuilder) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 57 with StaticLayout

use of android.text.StaticLayout in project Signal-Android by WhisperSystems.

the class TextEntity method createBitmap.

/**
   * If reuseBmp is not null, and size of the new bitmap matches the size of the reuseBmp,
   * new bitmap won't be created, reuseBmp it will be reused instead
   *
   * @param textLayer text to draw
   * @param reuseBmp  the bitmap that will be reused
   * @return bitmap with the text
   */
@NonNull
private Bitmap createBitmap(@NonNull TextLayer textLayer, @Nullable Bitmap reuseBmp) {
    int boundsWidth = canvasWidth;
    // init params - size, color, typeface
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTextSize(textLayer.getFont().getSize() * canvasWidth);
    textPaint.setColor(textLayer.getFont().getColor());
    //        textPaint.setTypeface(fontProvider.getTypeface(textLayer.getFont().getTypeface()));
    // drawing text guide : http://ivankocijan.xyz/android-drawing-multiline-text-on-canvas/
    // Static layout which will be drawn on canvas
    StaticLayout sl = new StaticLayout(// - text which will be drawn
    textLayer.getText(), textPaint, // - width of the layout
    boundsWidth, // - layout alignment
    Layout.Alignment.ALIGN_CENTER, // 1 - text spacing multiply
    1, // 1 - text spacing add
    1, // true - include padding
    true);
    // calculate height for the entity, min - Limits.MIN_BITMAP_HEIGHT
    int boundsHeight = sl.getHeight();
    // create bitmap not smaller than TextLayer.Limits.MIN_BITMAP_HEIGHT
    int bmpHeight = (int) (canvasHeight * Math.max(TextLayer.Limits.MIN_BITMAP_HEIGHT, 1.0F * boundsHeight / canvasHeight));
    // create bitmap where text will be drawn
    Bitmap bmp;
    if (reuseBmp != null && reuseBmp.getWidth() == boundsWidth && reuseBmp.getHeight() == bmpHeight) {
        // if previous bitmap exists, and it's width/height is the same - reuse it
        bmp = reuseBmp;
        // erase color when reusing
        bmp.eraseColor(Color.TRANSPARENT);
    } else {
        bmp = Bitmap.createBitmap(boundsWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bmp);
    canvas.save();
    // move text to center if bitmap is bigger that text
    if (boundsHeight < bmpHeight) {
        //calculate Y coordinate - In this case we want to draw the text in the
        //center of the canvas so we move Y coordinate to center.
        float textYCoordinate = (bmpHeight - boundsHeight) / 2;
        canvas.translate(0, textYCoordinate);
    }
    //draws static layout on canvas
    sl.draw(canvas);
    canvas.restore();
    return bmp;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) NonNull(android.support.annotation.NonNull)

Example 58 with StaticLayout

use of android.text.StaticLayout in project LuaViewSDK by alibaba.

the class TextUtil method getLineCount.

private static int getLineCount(CharSequence text, TextPaint paint, float size, float width, DisplayMetrics displayMetrics) {
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size, displayMetrics));
    StaticLayout layout = new StaticLayout(text, paint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
    return layout.getLineCount();
}
Also used : StaticLayout(android.text.StaticLayout)

Example 59 with StaticLayout

use of android.text.StaticLayout in project platform_frameworks_base by android.

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 60 with StaticLayout

use of android.text.StaticLayout in project bilibili-android-client by HotBitmapGG.

the class OutlineTextView method onDraw.

@Override
protected void onDraw(Canvas canvas) {
    Layout layout = new StaticLayout(getText(), mTextPaintOutline, getWidth(), Layout.Alignment.ALIGN_CENTER, mSpacingMult, mSpacingAdd, mIncludePad);
    layout.draw(canvas);
    layout = new StaticLayout(getText(), mTextPaint, getWidth(), Layout.Alignment.ALIGN_CENTER, mSpacingMult, mSpacingAdd, mIncludePad);
    layout.draw(canvas);
}
Also used : StaticLayout(android.text.StaticLayout) Layout(android.text.Layout) StaticLayout(android.text.StaticLayout)

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