Java Code Examples for android.text.StaticLayout#getLineRight()

The following examples show how to use android.text.StaticLayout#getLineRight() . 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: OC_Wordcontroller.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
public static RectF boundingBoxForText(String tx, Typeface ty, float textsize)
{
    TextPaint tp = new TextPaint();
    tp.setTextSize(textsize);
    tp.setTypeface(ty);
    tp.setColor(Color.BLACK);
    SpannableString ss = new SpannableString(tx);
    StaticLayout sl = new StaticLayout(ss,tp,4000, Layout.Alignment.ALIGN_NORMAL,1,0,false);
    return new RectF(0,0,sl.getLineRight(0),sl.getLineBottom(0));
}
 
Example 2
Source File: SubtitlePainter.java    From no-player with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("PMD.NPathComplexity")  // TODO break this method up
private void drawTextLayout(Canvas canvas) {
    StaticLayout layout = textLayout;
    if (layout == null) {
        // Nothing to draw.
        return;
    }

    int saveCount = canvas.save();
    canvas.translate(textLeft, textTop);

    if (Color.alpha(windowColor) > 0) {
        paint.setColor(windowColor);
        canvas.drawRect(-textPaddingX, 0, layout.getWidth() + textPaddingX, layout.getHeight(),
                paint);
    }

    if (Color.alpha(backgroundColor) > 0) {
        paint.setColor(backgroundColor);
        float previousBottom = layout.getLineTop(0);
        int lineCount = layout.getLineCount();
        for (int i = 0; i < lineCount; i++) {
            lineBounds.left = layout.getLineLeft(i) - textPaddingX;
            lineBounds.right = layout.getLineRight(i) + textPaddingX;
            lineBounds.top = previousBottom;
            lineBounds.bottom = layout.getLineBottom(i);
            previousBottom = lineBounds.bottom;
            canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint);
        }
    }

    if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
        textPaint.setStrokeJoin(Join.ROUND);
        textPaint.setStrokeWidth(outlineWidth);
        textPaint.setColor(edgeColor);
        textPaint.setStyle(Style.FILL_AND_STROKE);
        layout.draw(canvas);
    } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
        textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
    } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
            || edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
        boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
        int colorUp = raised ? Color.WHITE : edgeColor;
        int colorDown = raised ? edgeColor : Color.WHITE;
        float offset = shadowRadius / 2;
        textPaint.setColor(foregroundColor);
        textPaint.setStyle(Style.FILL);
        textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
        layout.draw(canvas);
        textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
    }

    textPaint.setColor(foregroundColor);
    textPaint.setStyle(Style.FILL);
    layout.draw(canvas);
    textPaint.setShadowLayer(0, 0, 0, 0);

    canvas.restoreToCount(saveCount);
}
 
Example 3
Source File: SubtitlePainter.java    From K-Sonic with MIT License 4 votes vote down vote up
private void drawTextLayout(Canvas canvas) {
  StaticLayout layout = textLayout;
  if (layout == null) {
    // Nothing to draw.
    return;
  }

  int saveCount = canvas.save();
  canvas.translate(textLeft, textTop);

  if (Color.alpha(windowColor) > 0) {
    paint.setColor(windowColor);
    canvas.drawRect(-textPaddingX, 0, layout.getWidth() + textPaddingX, layout.getHeight(),
        paint);
  }

  if (Color.alpha(backgroundColor) > 0) {
    paint.setColor(backgroundColor);
    float previousBottom = layout.getLineTop(0);
    int lineCount = layout.getLineCount();
    for (int i = 0; i < lineCount; i++) {
      lineBounds.left = layout.getLineLeft(i) - textPaddingX;
      lineBounds.right = layout.getLineRight(i) + textPaddingX;
      lineBounds.top = previousBottom;
      lineBounds.bottom = layout.getLineBottom(i);
      previousBottom = lineBounds.bottom;
      canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint);
    }
  }

  if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
    textPaint.setStrokeJoin(Join.ROUND);
    textPaint.setStrokeWidth(outlineWidth);
    textPaint.setColor(edgeColor);
    textPaint.setStyle(Style.FILL_AND_STROKE);
    layout.draw(canvas);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
    textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
      || edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
    boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
    int colorUp = raised ? Color.WHITE : edgeColor;
    int colorDown = raised ? edgeColor : Color.WHITE;
    float offset = shadowRadius / 2f;
    textPaint.setColor(foregroundColor);
    textPaint.setStyle(Style.FILL);
    textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
    layout.draw(canvas);
    textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
  }

  textPaint.setColor(foregroundColor);
  textPaint.setStyle(Style.FILL);
  layout.draw(canvas);
  textPaint.setShadowLayer(0, 0, 0, 0);

  canvas.restoreToCount(saveCount);
}
 
Example 4
Source File: SubtitleView.java    From Exoplayer_VLC with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas c) {
  final StaticLayout layout = this.layout;
  if (layout == null) {
    return;
  }

  final int saveCount = c.save();
  final int innerPaddingX = this.innerPaddingX;
  c.translate(getPaddingLeft() + innerPaddingX, getPaddingTop());

  final int lineCount = layout.getLineCount();
  final Paint textPaint = this.textPaint;
  final Paint paint = this.paint;
  final RectF bounds = lineBounds;

  if (Color.alpha(backgroundColor) > 0) {
    final float cornerRadius = this.cornerRadius;
    float previousBottom = layout.getLineTop(0);

    paint.setColor(backgroundColor);
    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);
    }
  }

  if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
    textPaint.setStrokeJoin(Join.ROUND);
    textPaint.setStrokeWidth(outlineWidth);
    textPaint.setColor(edgeColor);
    textPaint.setStyle(Style.FILL_AND_STROKE);
    layout.draw(c);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
    textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
      || edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
    boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
    int colorUp = raised ? Color.WHITE : edgeColor;
    int colorDown = raised ? edgeColor : Color.WHITE;
    float offset = shadowRadius / 2f;
    textPaint.setColor(foregroundColor);
    textPaint.setStyle(Style.FILL);
    textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
    layout.draw(c);
    textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
  }

  textPaint.setColor(foregroundColor);
  textPaint.setStyle(Style.FILL);
  layout.draw(c);
  textPaint.setShadowLayer(0, 0, 0, 0);
  c.restoreToCount(saveCount);
}