com.google.android.exoplayer2.text.CaptionStyleCompat Java Examples

The following examples show how to use com.google.android.exoplayer2.text.CaptionStyleCompat. 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: GSYExoSubTitleVideoView.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
@Override
protected void init(Context context) {
    super.init(context);
    mSubtitleView = findViewById(R.id.sub_title_view);


    mSubtitleView.setStyle(new CaptionStyleCompat(Color.BLACK, Color.TRANSPARENT, Color.TRANSPARENT, CaptionStyleCompat.EDGE_TYPE_NONE, CaptionStyleCompat.EDGE_TYPE_NONE, null));
    mSubtitleView.setFixedTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
}
 
Example #2
Source File: SubtitleView.java    From K-Sonic with MIT License 5 votes vote down vote up
public SubtitleView(Context context, AttributeSet attrs) {
  super(context, attrs);
  painters = new ArrayList<>();
  textSizeType = FRACTIONAL;
  textSize = DEFAULT_TEXT_SIZE_FRACTION;
  applyEmbeddedStyles = true;
  style = CaptionStyleCompat.DEFAULT;
  bottomPaddingFraction = DEFAULT_BOTTOM_PADDING_FRACTION;
}
 
Example #3
Source File: SubtitleView.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Sets the caption style.
 *
 * @param style A style for the view.
 */
public void setStyle(CaptionStyleCompat style) {
  if (this.style == style) {
    return;
  }
  this.style = style;
  // Invalidate to trigger drawing.
  invalidate();
}
 
Example #4
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 #5
Source File: SubtitlePainter.java    From K-Sonic with MIT License 4 votes vote down vote up
/**
 * Draws the provided {@link Cue} into a canvas with the specified styling.
 * <p>
 * A call to this method is able to use cached results of calculations made during the previous
 * call, and so an instance of this class is able to optimize repeated calls to this method in
 * which the same parameters are passed.
 *
 * @param cue The cue to draw.
 * @param applyEmbeddedStyles Whether styling embedded within the cue should be applied.
 * @param style The style to use when drawing the cue text.
 * @param textSizePx The text size to use when drawing the cue text, in pixels.
 * @param bottomPaddingFraction The bottom padding fraction to apply when {@link Cue#line} is
 *     {@link Cue#DIMEN_UNSET}, as a fraction of the viewport height
 * @param canvas The canvas into which to draw.
 * @param cueBoxLeft The left position of the enclosing cue box.
 * @param cueBoxTop The top position of the enclosing cue box.
 * @param cueBoxRight The right position of the enclosing cue box.
 * @param cueBoxBottom The bottom position of the enclosing cue box.
 */
public void draw(Cue cue, boolean applyEmbeddedStyles, CaptionStyleCompat style, float textSizePx,
    float bottomPaddingFraction, Canvas canvas, int cueBoxLeft, int cueBoxTop, int cueBoxRight,
    int cueBoxBottom) {
  boolean isTextCue = cue.bitmap == null;
  CharSequence cueText = null;
  Bitmap cueBitmap = null;
  int windowColor = Color.BLACK;
  if (isTextCue) {
    cueText = cue.text;
    if (TextUtils.isEmpty(cueText)) {
      // Nothing to draw.
      return;
    }
    windowColor = cue.windowColorSet ? cue.windowColor : style.windowColor;
    if (!applyEmbeddedStyles) {
      // Strip out any embedded styling.
      cueText = cueText.toString();
      windowColor = style.windowColor;
    }
  } else {
    cueBitmap = cue.bitmap;
  }
  if (areCharSequencesEqual(this.cueText, cueText)
      && Util.areEqual(this.cueTextAlignment, cue.textAlignment)
      && this.cueBitmap == cueBitmap
      && this.cueLine == cue.line
      && this.cueLineType == cue.lineType
      && Util.areEqual(this.cueLineAnchor, cue.lineAnchor)
      && this.cuePosition == cue.position
      && Util.areEqual(this.cuePositionAnchor, cue.positionAnchor)
      && this.cueSize == cue.size
      && this.applyEmbeddedStyles == applyEmbeddedStyles
      && this.foregroundColor == style.foregroundColor
      && this.backgroundColor == style.backgroundColor
      && this.windowColor == windowColor
      && this.edgeType == style.edgeType
      && this.edgeColor == style.edgeColor
      && Util.areEqual(this.textPaint.getTypeface(), style.typeface)
      && this.textSizePx == textSizePx
      && this.bottomPaddingFraction == bottomPaddingFraction
      && this.parentLeft == cueBoxLeft
      && this.parentTop == cueBoxTop
      && this.parentRight == cueBoxRight
      && this.parentBottom == cueBoxBottom) {
    // We can use the cached layout.
    drawLayout(canvas, isTextCue);
    return;
  }

  this.cueText = cueText;
  this.cueTextAlignment = cue.textAlignment;
  this.cueBitmap = cueBitmap;
  this.cueLine = cue.line;
  this.cueLineType = cue.lineType;
  this.cueLineAnchor = cue.lineAnchor;
  this.cuePosition = cue.position;
  this.cuePositionAnchor = cue.positionAnchor;
  this.cueSize = cue.size;
  this.applyEmbeddedStyles = applyEmbeddedStyles;
  this.foregroundColor = style.foregroundColor;
  this.backgroundColor = style.backgroundColor;
  this.windowColor = windowColor;
  this.edgeType = style.edgeType;
  this.edgeColor = style.edgeColor;
  this.textPaint.setTypeface(style.typeface);
  this.textSizePx = textSizePx;
  this.bottomPaddingFraction = bottomPaddingFraction;
  this.parentLeft = cueBoxLeft;
  this.parentTop = cueBoxTop;
  this.parentRight = cueBoxRight;
  this.parentBottom = cueBoxBottom;

  if (isTextCue) {
    setupTextLayout();
  } else {
    setupBitmapLayout();
  }
  drawLayout(canvas, isTextCue);
}
 
Example #6
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 #7
Source File: SubtitleView.java    From K-Sonic with MIT License 4 votes vote down vote up
/**
 * Sets the caption style to be equivalent to the one returned by
 * {@link CaptioningManager#getUserStyle()}, or to a default style before API level 19.
 */
public void setUserDefaultStyle() {
  setStyle(Util.SDK_INT >= 19 && !isInEditMode()
      ? getUserCaptionStyleV19() : CaptionStyleCompat.DEFAULT);
}
 
Example #8
Source File: SubtitleView.java    From K-Sonic with MIT License 4 votes vote down vote up
@TargetApi(19)
private CaptionStyleCompat getUserCaptionStyleV19() {
  CaptioningManager captioningManager =
      (CaptioningManager) getContext().getSystemService(Context.CAPTIONING_SERVICE);
  return CaptionStyleCompat.createFromCaptionStyle(captioningManager.getUserStyle());
}
 
Example #9
Source File: SettingSubtitleView.java    From DanDanPlayForAndroid with MIT License votes vote down vote up
void setInterBackground(CaptionStyleCompat compat);