Fixed overlapping captions.

This commit is contained in:
Oliver Woodman 2015-09-01 13:58:22 +01:00
parent 9231520ee8
commit 7bf1d89168
2 changed files with 36 additions and 11 deletions

View file

@ -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) {

View file

@ -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<CuePainter> painters;
private List<Cue> 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());
}
}