mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Fixed overlapping captions.
This commit is contained in:
parent
9231520ee8
commit
7bf1d89168
2 changed files with 36 additions and 11 deletions
|
|
@ -51,12 +51,6 @@ import android.util.Log;
|
||||||
*/
|
*/
|
||||||
private static final float LINE_HEIGHT_FRACTION = 0.0533f;
|
private static final float LINE_HEIGHT_FRACTION = 0.0533f;
|
||||||
|
|
||||||
/**
|
|
||||||
* The default bottom padding to apply when {@link Cue#line} is {@link Cue#UNSET_VALUE}, as a
|
|
||||||
* fraction of the viewport height.
|
|
||||||
*/
|
|
||||||
private static final float DEFAULT_BOTTOM_PADDING_FRACTION = 0.08f;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Temporary rectangle used for computing line bounds.
|
* Temporary rectangle used for computing line bounds.
|
||||||
*/
|
*/
|
||||||
|
|
@ -82,6 +76,8 @@ import android.util.Log;
|
||||||
private int windowColor;
|
private int windowColor;
|
||||||
private int edgeColor;
|
private int edgeColor;
|
||||||
private int edgeType;
|
private int edgeType;
|
||||||
|
private float fontScale;
|
||||||
|
private float bottomPaddingFraction;
|
||||||
private int parentLeft;
|
private int parentLeft;
|
||||||
private int parentTop;
|
private int parentTop;
|
||||||
private int parentRight;
|
private int parentRight;
|
||||||
|
|
@ -127,14 +123,16 @@ import android.util.Log;
|
||||||
* @param cue The cue to draw.
|
* @param cue The cue to draw.
|
||||||
* @param style The style to use when drawing the cue text.
|
* @param style The style to use when drawing the cue text.
|
||||||
* @param fontScale The font scale.
|
* @param fontScale The font scale.
|
||||||
|
* @param bottomPaddingFraction The bottom padding fraction to apply when {@link Cue#line} is
|
||||||
|
* {@link Cue#UNSET_VALUE}, as a fraction of the viewport height
|
||||||
* @param canvas The canvas into which to draw.
|
* @param canvas The canvas into which to draw.
|
||||||
* @param cueBoxLeft The left position of the enclosing cue box.
|
* @param cueBoxLeft The left position of the enclosing cue box.
|
||||||
* @param cueBoxTop The top 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 cueBoxRight The right position of the enclosing cue box.
|
||||||
* @param cueBoxBottom The bottom position of the enclosing cue box.
|
* @param cueBoxBottom The bottom position of the enclosing cue box.
|
||||||
*/
|
*/
|
||||||
public void draw(Cue cue, CaptionStyleCompat style, float fontScale, Canvas canvas,
|
public void draw(Cue cue, CaptionStyleCompat style, float fontScale, float bottomPaddingFraction,
|
||||||
int cueBoxLeft, int cueBoxTop, int cueBoxRight, int cueBoxBottom) {
|
Canvas canvas, int cueBoxLeft, int cueBoxTop, int cueBoxRight, int cueBoxBottom) {
|
||||||
if (TextUtils.isEmpty(cue.text)) {
|
if (TextUtils.isEmpty(cue.text)) {
|
||||||
// Nothing to draw.
|
// Nothing to draw.
|
||||||
return;
|
return;
|
||||||
|
|
@ -149,6 +147,8 @@ import android.util.Log;
|
||||||
&& edgeType == style.edgeType
|
&& edgeType == style.edgeType
|
||||||
&& edgeColor == style.edgeColor
|
&& edgeColor == style.edgeColor
|
||||||
&& Util.areEqual(textPaint.getTypeface(), style.typeface)
|
&& Util.areEqual(textPaint.getTypeface(), style.typeface)
|
||||||
|
&& this.fontScale == fontScale
|
||||||
|
&& this.bottomPaddingFraction == bottomPaddingFraction
|
||||||
&& parentLeft == cueBoxLeft
|
&& parentLeft == cueBoxLeft
|
||||||
&& parentTop == cueBoxTop
|
&& parentTop == cueBoxTop
|
||||||
&& parentRight == cueBoxRight
|
&& parentRight == cueBoxRight
|
||||||
|
|
@ -167,6 +167,8 @@ import android.util.Log;
|
||||||
edgeType = style.edgeType;
|
edgeType = style.edgeType;
|
||||||
edgeColor = style.edgeColor;
|
edgeColor = style.edgeColor;
|
||||||
textPaint.setTypeface(style.typeface);
|
textPaint.setTypeface(style.typeface);
|
||||||
|
this.fontScale = fontScale;
|
||||||
|
this.bottomPaddingFraction = bottomPaddingFraction;
|
||||||
parentLeft = cueBoxLeft;
|
parentLeft = cueBoxLeft;
|
||||||
parentTop = cueBoxTop;
|
parentTop = cueBoxTop;
|
||||||
parentRight = cueBoxRight;
|
parentRight = cueBoxRight;
|
||||||
|
|
@ -199,7 +201,7 @@ import android.util.Log;
|
||||||
int textLeft = (parentWidth - textWidth) / 2;
|
int textLeft = (parentWidth - textWidth) / 2;
|
||||||
int textRight = textLeft + textWidth;
|
int textRight = textLeft + textWidth;
|
||||||
int textTop = parentBottom - textHeight
|
int textTop = parentBottom - textHeight
|
||||||
- (int) (parentHeight * DEFAULT_BOTTOM_PADDING_FRACTION);
|
- (int) (parentHeight * bottomPaddingFraction);
|
||||||
int textBottom = textTop + textHeight;
|
int textBottom = textTop + textHeight;
|
||||||
|
|
||||||
if (cue.position != Cue.UNSET_VALUE) {
|
if (cue.position != Cue.UNSET_VALUE) {
|
||||||
|
|
|
||||||
|
|
@ -28,11 +28,18 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public final class SubtitleLayout extends View {
|
public final class SubtitleLayout extends View {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default bottom padding to apply when {@link Cue#line} is {@link Cue#UNSET_VALUE}, as a
|
||||||
|
* fraction of the viewport height.
|
||||||
|
*/
|
||||||
|
public static final float DEFAULT_BOTTOM_PADDING_FRACTION = 0.08f;
|
||||||
|
|
||||||
private final List<CuePainter> painters;
|
private final List<CuePainter> painters;
|
||||||
|
|
||||||
private List<Cue> cues;
|
private List<Cue> cues;
|
||||||
private float fontScale;
|
private float fontScale;
|
||||||
private CaptionStyleCompat style;
|
private CaptionStyleCompat style;
|
||||||
|
private float bottomPaddingFraction;
|
||||||
|
|
||||||
public SubtitleLayout(Context context) {
|
public SubtitleLayout(Context context) {
|
||||||
this(context, null);
|
this(context, null);
|
||||||
|
|
@ -43,6 +50,7 @@ public final class SubtitleLayout extends View {
|
||||||
painters = new ArrayList<>();
|
painters = new ArrayList<>();
|
||||||
fontScale = 1;
|
fontScale = 1;
|
||||||
style = CaptionStyleCompat.DEFAULT;
|
style = CaptionStyleCompat.DEFAULT;
|
||||||
|
bottomPaddingFraction = DEFAULT_BOTTOM_PADDING_FRACTION;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -92,12 +100,27 @@ public final class SubtitleLayout extends View {
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the bottom padding fraction to apply when {@link Cue#line} is {@link Cue#UNSET_VALUE},
|
||||||
|
* as a fraction of the viewport height.
|
||||||
|
*
|
||||||
|
* @param bottomPaddingFraction The bottom padding fraction.
|
||||||
|
*/
|
||||||
|
public void setBottomPaddingFraction(float bottomPaddingFraction) {
|
||||||
|
if (this.bottomPaddingFraction == bottomPaddingFraction) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.bottomPaddingFraction = bottomPaddingFraction;
|
||||||
|
// Invalidate to trigger drawing.
|
||||||
|
invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispatchDraw(Canvas canvas) {
|
public void dispatchDraw(Canvas canvas) {
|
||||||
int cueCount = (cues == null) ? 0 : cues.size();
|
int cueCount = (cues == null) ? 0 : cues.size();
|
||||||
for (int i = 0; i < cueCount; i++) {
|
for (int i = 0; i < cueCount; i++) {
|
||||||
painters.get(i).draw(cues.get(i), style, fontScale, canvas, getLeft(), getTop(), getRight(),
|
painters.get(i).draw(cues.get(i), style, fontScale, bottomPaddingFraction, canvas, getLeft(),
|
||||||
getBottom());
|
getTop(), getRight(), getBottom());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue