Skip to content

Commit

Permalink
Improve DefaultTimeBar color customization
Browse files Browse the repository at this point in the history
Add attributes for the scrubber handle color and unplayed color. If attributes
are missing, derive defaults from the played color.

Issue: #2740

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=154825736
  • Loading branch information
andrewlewis authored and ojw28 committed May 4, 2017
1 parent d33a6b4 commit c3158d3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,21 @@ public class DefaultTimeBar extends View implements TimeBar {
private static final int DEFAULT_INCREMENT_COUNT = 20;
private static final int DEFAULT_BAR_HEIGHT = 4;
private static final int DEFAULT_TOUCH_TARGET_HEIGHT = 26;
private static final int DEFAULT_PLAYED_COLOR = 0x33FFFFFF;
private static final int DEFAULT_BUFFERED_COLOR = 0xCCFFFFFF;
private static final int DEFAULT_PLAYED_COLOR = 0xFFFFFFFF;
private static final int DEFAULT_AD_MARKER_COLOR = 0xB2FFFF00;
private static final int DEFAULT_AD_MARKER_WIDTH = 4;
private static final int DEFAULT_SCRUBBER_ENABLED_SIZE = 12;
private static final int DEFAULT_SCRUBBER_DISABLED_SIZE = 0;
private static final int DEFAULT_SCRUBBER_DRAGGED_SIZE = 16;
private static final int OPAQUE_COLOR = 0xFF000000;

private final Rect seekBounds;
private final Rect progressBar;
private final Rect bufferedBar;
private final Rect scrubberBar;
private final Paint progressPaint;
private final Paint bufferedPaint;
private final Paint playedPaint;
private final Paint scrubberPaint;
private final Paint bufferedPaint;
private final Paint unplayedPaint;
private final Paint adMarkerPaint;
private final int barHeight;
private final int touchTargetHeight;
Expand Down Expand Up @@ -115,9 +114,10 @@ public DefaultTimeBar(Context context, AttributeSet attrs) {
progressBar = new Rect();
bufferedBar = new Rect();
scrubberBar = new Rect();
progressPaint = new Paint();
bufferedPaint = new Paint();
playedPaint = new Paint();
scrubberPaint = new Paint();
bufferedPaint = new Paint();
unplayedPaint = new Paint();
adMarkerPaint = new Paint();

// Calculate the dimensions and paints for drawn elements.
Expand Down Expand Up @@ -147,13 +147,18 @@ public DefaultTimeBar(Context context, AttributeSet attrs) {
scrubberDraggedSize = a.getDimensionPixelSize(
R.styleable.DefaultTimeBar_scrubber_dragged_size, defaultScrubberDraggedSize);
int playedColor = a.getInt(R.styleable.DefaultTimeBar_played_color, DEFAULT_PLAYED_COLOR);
int scrubberColor = a.getInt(R.styleable.DefaultTimeBar_scrubber_color,
getDefaultScrubberColor(playedColor));
int bufferedColor = a.getInt(R.styleable.DefaultTimeBar_buffered_color,
DEFAULT_BUFFERED_COLOR);
getDefaultBufferedColor(playedColor));
int unplayedColor = a.getInt(R.styleable.DefaultTimeBar_unplayed_color,
getDefaultUnplayedColor(playedColor));
int adMarkerColor = a.getInt(R.styleable.DefaultTimeBar_ad_marker_color,
DEFAULT_AD_MARKER_COLOR);
progressPaint.setColor(playedColor);
scrubberPaint.setColor(OPAQUE_COLOR | playedColor);
playedPaint.setColor(playedColor);
scrubberPaint.setColor(scrubberColor);
bufferedPaint.setColor(bufferedColor);
unplayedPaint.setColor(unplayedColor);
adMarkerPaint.setColor(adMarkerColor);
} finally {
a.recycle();
Expand All @@ -165,9 +170,10 @@ public DefaultTimeBar(Context context, AttributeSet attrs) {
scrubberEnabledSize = defaultScrubberEnabledSize;
scrubberDisabledSize = defaultScrubberDisabledSize;
scrubberDraggedSize = defaultScrubberDraggedSize;
scrubberPaint.setColor(OPAQUE_COLOR | DEFAULT_PLAYED_COLOR);
progressPaint.setColor(DEFAULT_PLAYED_COLOR);
bufferedPaint.setColor(DEFAULT_BUFFERED_COLOR);
playedPaint.setColor(DEFAULT_PLAYED_COLOR);
scrubberPaint.setColor(getDefaultScrubberColor(DEFAULT_PLAYED_COLOR));
bufferedPaint.setColor(getDefaultBufferedColor(DEFAULT_PLAYED_COLOR));
unplayedPaint.setColor(getDefaultUnplayedColor(DEFAULT_PLAYED_COLOR));
adMarkerPaint.setColor(DEFAULT_AD_MARKER_COLOR);
}
formatBuilder = new StringBuilder();
Expand Down Expand Up @@ -502,21 +508,21 @@ private void drawTimeBar(Canvas canvas) {
int barTop = progressBar.centerY() - progressBarHeight / 2;
int barBottom = barTop + progressBarHeight;
if (duration <= 0) {
canvas.drawRect(progressBar.left, barTop, progressBar.right, barBottom, progressPaint);
canvas.drawRect(progressBar.left, barTop, progressBar.right, barBottom, unplayedPaint);
return;
}
int bufferedLeft = bufferedBar.left;
int bufferedRight = bufferedBar.right;
int progressLeft = Math.max(Math.max(progressBar.left, bufferedRight), scrubberBar.right);
if (progressLeft < progressBar.right) {
canvas.drawRect(progressLeft, barTop, progressBar.right, barBottom, progressPaint);
canvas.drawRect(progressLeft, barTop, progressBar.right, barBottom, unplayedPaint);
}
bufferedLeft = Math.max(bufferedLeft, scrubberBar.right);
if (bufferedRight > bufferedLeft) {
canvas.drawRect(bufferedLeft, barTop, bufferedRight, barBottom, bufferedPaint);
}
if (scrubberBar.width() > 0) {
canvas.drawRect(scrubberBar.left, barTop, scrubberBar.right, barBottom, scrubberPaint);
canvas.drawRect(scrubberBar.left, barTop, scrubberBar.right, barBottom, playedPaint);
}
int adMarkerOffset = adMarkerWidth / 2;
for (int i = 0; i < adBreakCount; i++) {
Expand Down Expand Up @@ -577,4 +583,16 @@ private static int dpToPx(DisplayMetrics displayMetrics, int dps) {
return (int) (dps * displayMetrics.density + 0.5f);
}

private static int getDefaultScrubberColor(int playedColor) {
return 0xFF000000 | playedColor;
}

private static int getDefaultUnplayedColor(int playedColor) {
return 0x33000000 | (playedColor & 0x00FFFFFF);
}

private static int getDefaultBufferedColor(int playedColor) {
return 0xCC000000 | (playedColor & 0x00FFFFFF);
}

}
2 changes: 2 additions & 0 deletions library/ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
<attr name="scrubber_disabled_size" format="dimension"/>
<attr name="scrubber_dragged_size" format="dimension"/>
<attr name="played_color" format="color"/>
<attr name="scrubber_color" format="color"/>
<attr name="buffered_color" format="color"/>
<attr name="unplayed_color" format="color"/>
<attr name="ad_marker_color" format="color"/>
</declare-styleable>

Expand Down

0 comments on commit c3158d3

Please sign in to comment.