Skip to content

Commit

Permalink
Allow specifying a Drawable for the TimeBar scrubber
Browse files Browse the repository at this point in the history
Issue: #3337

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=173411321
  • Loading branch information
andrewlewis authored and ojw28 committed Oct 27, 2017
1 parent 69e43b6 commit 3602a9a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
Expand Down Expand Up @@ -86,6 +87,10 @@
* <li>Default: {@link #DEFAULT_SCRUBBER_DRAGGED_SIZE_DP}</li>
* </ul>
* </li>
* <li><b>{@code scrubber_drawable}</b> - Optional reference to a drawable to draw for the
* scrubber handle. If set, this overrides the default behavior, which is to draw a circle for
* the scrubber handle.
* </li>
* <li><b>{@code played_color}</b> - Color for the portion of the time bar representing media
* before the current playback position.
* <ul>
Expand Down Expand Up @@ -181,6 +186,7 @@ public class DefaultTimeBar extends View implements TimeBar {
private final Paint adMarkerPaint;
private final Paint playedAdMarkerPaint;
private final Paint scrubberPaint;
private final Drawable scrubberDrawable;
private final int barHeight;
private final int touchTargetHeight;
private final int adMarkerWidth;
Expand Down Expand Up @@ -240,6 +246,14 @@ public DefaultTimeBar(Context context, AttributeSet attrs) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DefaultTimeBar, 0,
0);
try {
scrubberDrawable = a.getDrawable(R.styleable.DefaultTimeBar_scrubber_drawable);
if (scrubberDrawable != null) {
if (Util.SDK_INT >= 23) {
scrubberDrawable.setLayoutDirection(getLayoutDirection());
}
defaultTouchTargetHeight =
Math.max(scrubberDrawable.getMinimumHeight(), defaultTouchTargetHeight);
}
barHeight = a.getDimensionPixelSize(R.styleable.DefaultTimeBar_bar_height,
defaultBarHeight);
touchTargetHeight = a.getDimensionPixelSize(R.styleable.DefaultTimeBar_touch_target_height,
Expand Down Expand Up @@ -284,6 +298,7 @@ public DefaultTimeBar(Context context, AttributeSet attrs) {
bufferedPaint.setColor(getDefaultBufferedColor(DEFAULT_PLAYED_COLOR));
unplayedPaint.setColor(getDefaultUnplayedColor(DEFAULT_PLAYED_COLOR));
adMarkerPaint.setColor(DEFAULT_AD_MARKER_COLOR);
scrubberDrawable = null;
}
formatBuilder = new StringBuilder();
formatter = new Formatter(formatBuilder, Locale.getDefault());
Expand All @@ -293,9 +308,13 @@ public void run() {
stopScrubbing(false);
}
};
scrubberPadding =
(Math.max(scrubberDisabledSize, Math.max(scrubberEnabledSize, scrubberDraggedSize)) + 1)
/ 2;
if (scrubberDrawable != null) {
scrubberPadding = (scrubberDrawable.getMinimumWidth() + 1) / 2;
} else {
scrubberPadding =
(Math.max(scrubberDisabledSize, Math.max(scrubberEnabledSize, scrubberDraggedSize)) + 1)
/ 2;
}
duration = C.TIME_UNSET;
keyTimeIncrement = C.TIME_UNSET;
keyCountIncrement = DEFAULT_INCREMENT_COUNT;
Expand Down Expand Up @@ -453,13 +472,28 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}

@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
updateDrawableState();
}

@Override
public void jumpDrawablesToCurrentState() {
super.jumpDrawablesToCurrentState();
if (scrubberDrawable != null) {
scrubberDrawable.jumpToCurrentState();
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int height = heightMode == MeasureSpec.UNSPECIFIED ? touchTargetHeight
: heightMode == MeasureSpec.EXACTLY ? heightSize : Math.min(touchTargetHeight, heightSize);
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), height);
updateDrawableState();
}

@Override
Expand All @@ -477,8 +511,10 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
}

@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
super.onSizeChanged(width, height, oldWidth, oldHeight);
public void onRtlPropertiesChanged(int layoutDirection) {
if (scrubberDrawable != null && scrubberDrawable.setLayoutDirection(layoutDirection)) {
invalidate();
}
}

@Override
Expand Down Expand Up @@ -543,6 +579,7 @@ private void maybeSetImportantForAccessibilityV16() {

private void startScrubbing() {
scrubbing = true;
setPressed(true);
ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
Expand All @@ -554,6 +591,7 @@ private void startScrubbing() {

private void stopScrubbing(boolean canceled) {
scrubbing = false;
setPressed(false);
ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(false);
Expand Down Expand Up @@ -644,12 +682,30 @@ private void drawPlayhead(Canvas canvas) {
if (duration <= 0) {
return;
}
int scrubberSize = (scrubbing || isFocused()) ? scrubberDraggedSize
: (isEnabled() ? scrubberEnabledSize : scrubberDisabledSize);
int playheadRadius = scrubberSize / 2;
int playheadCenter = Util.constrainValue(scrubberBar.right, scrubberBar.left,
progressBar.right);
canvas.drawCircle(playheadCenter, scrubberBar.centerY(), playheadRadius, scrubberPaint);
int playheadX = Util.constrainValue(scrubberBar.right, scrubberBar.left, progressBar.right);
int playheadY = scrubberBar.centerY();
if (scrubberDrawable == null) {
int scrubberSize = (scrubbing || isFocused()) ? scrubberDraggedSize
: (isEnabled() ? scrubberEnabledSize : scrubberDisabledSize);
int playheadRadius = scrubberSize / 2;
canvas.drawCircle(playheadX, playheadY, playheadRadius, scrubberPaint);
} else {
int scrubberDrawableWidth = scrubberDrawable.getIntrinsicWidth();
int scrubberDrawableHeight = scrubberDrawable.getIntrinsicHeight();
scrubberDrawable.setBounds(
playheadX - scrubberDrawableWidth / 2,
playheadY - scrubberDrawableHeight / 2,
playheadX + scrubberDrawableWidth / 2,
playheadY + scrubberDrawableHeight / 2);
scrubberDrawable.draw(canvas);
}
}

private void updateDrawableState() {
if (scrubberDrawable != null && scrubberDrawable.isStateful()
&& scrubberDrawable.setState(getDrawableState())) {
invalidate();
}
}

private String getProgressText() {
Expand Down Expand Up @@ -690,19 +746,19 @@ private static int dpToPx(DisplayMetrics displayMetrics, int dps) {
return (int) (dps * displayMetrics.density + 0.5f);
}

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

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

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

private static int getDefaultPlayedAdMarkerColor(int adMarkerColor) {
public static int getDefaultPlayedAdMarkerColor(int adMarkerColor) {
return 0x33000000 | (adMarkerColor & 0x00FFFFFF);
}

Expand Down
1 change: 1 addition & 0 deletions library/ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<attr name="scrubber_enabled_size" format="dimension"/>
<attr name="scrubber_disabled_size" format="dimension"/>
<attr name="scrubber_dragged_size" format="dimension"/>
<attr name="scrubber_drawable" format="reference"/>
<attr name="played_color" format="color"/>
<attr name="scrubber_color" format="color"/>
<attr name="buffered_color" format="color"/>
Expand Down

0 comments on commit 3602a9a

Please sign in to comment.