From 7bf1d891688020e32eb53453d0a4d9362b7b6aaf Mon Sep 17 00:00:00 2001 From: Oliver Woodman Date: Tue, 1 Sep 2015 13:58:22 +0100 Subject: [PATCH] Fixed overlapping captions. --- .../android/exoplayer/text/CuePainter.java | 20 +++++++------- .../exoplayer/text/SubtitleLayout.java | 27 +++++++++++++++++-- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/library/src/main/java/com/google/android/exoplayer/text/CuePainter.java b/library/src/main/java/com/google/android/exoplayer/text/CuePainter.java index bbeee94f51..66b41a811a 100644 --- a/library/src/main/java/com/google/android/exoplayer/text/CuePainter.java +++ b/library/src/main/java/com/google/android/exoplayer/text/CuePainter.java @@ -51,12 +51,6 @@ import android.util.Log; */ 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. */ @@ -82,6 +76,8 @@ import android.util.Log; private int windowColor; private int edgeColor; private int edgeType; + private float fontScale; + private float bottomPaddingFraction; private int parentLeft; private int parentTop; private int parentRight; @@ -127,14 +123,16 @@ import android.util.Log; * @param cue The cue to draw. * @param style The style to use when drawing the cue text. * @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 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, CaptionStyleCompat style, float fontScale, Canvas canvas, - int cueBoxLeft, int cueBoxTop, int cueBoxRight, int cueBoxBottom) { + public void draw(Cue cue, CaptionStyleCompat style, float fontScale, float bottomPaddingFraction, + Canvas canvas, int cueBoxLeft, int cueBoxTop, int cueBoxRight, int cueBoxBottom) { if (TextUtils.isEmpty(cue.text)) { // Nothing to draw. return; @@ -149,6 +147,8 @@ import android.util.Log; && edgeType == style.edgeType && edgeColor == style.edgeColor && Util.areEqual(textPaint.getTypeface(), style.typeface) + && this.fontScale == fontScale + && this.bottomPaddingFraction == bottomPaddingFraction && parentLeft == cueBoxLeft && parentTop == cueBoxTop && parentRight == cueBoxRight @@ -167,6 +167,8 @@ import android.util.Log; edgeType = style.edgeType; edgeColor = style.edgeColor; textPaint.setTypeface(style.typeface); + this.fontScale = fontScale; + this.bottomPaddingFraction = bottomPaddingFraction; parentLeft = cueBoxLeft; parentTop = cueBoxTop; parentRight = cueBoxRight; @@ -199,7 +201,7 @@ import android.util.Log; int textLeft = (parentWidth - textWidth) / 2; int textRight = textLeft + textWidth; int textTop = parentBottom - textHeight - - (int) (parentHeight * DEFAULT_BOTTOM_PADDING_FRACTION); + - (int) (parentHeight * bottomPaddingFraction); int textBottom = textTop + textHeight; if (cue.position != Cue.UNSET_VALUE) { diff --git a/library/src/main/java/com/google/android/exoplayer/text/SubtitleLayout.java b/library/src/main/java/com/google/android/exoplayer/text/SubtitleLayout.java index 690e7056e7..e0818a2d86 100644 --- a/library/src/main/java/com/google/android/exoplayer/text/SubtitleLayout.java +++ b/library/src/main/java/com/google/android/exoplayer/text/SubtitleLayout.java @@ -28,11 +28,18 @@ import java.util.List; */ 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 painters; private List cues; private float fontScale; private CaptionStyleCompat style; + private float bottomPaddingFraction; public SubtitleLayout(Context context) { this(context, null); @@ -43,6 +50,7 @@ public final class SubtitleLayout extends View { painters = new ArrayList<>(); fontScale = 1; style = CaptionStyleCompat.DEFAULT; + bottomPaddingFraction = DEFAULT_BOTTOM_PADDING_FRACTION; } /** @@ -92,12 +100,27 @@ public final class SubtitleLayout extends View { 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 public void dispatchDraw(Canvas canvas) { int cueCount = (cues == null) ? 0 : cues.size(); for (int i = 0; i < cueCount; i++) { - painters.get(i).draw(cues.get(i), style, fontScale, canvas, getLeft(), getTop(), getRight(), - getBottom()); + painters.get(i).draw(cues.get(i), style, fontScale, bottomPaddingFraction, canvas, getLeft(), + getTop(), getRight(), getBottom()); } }