From 95f23c837c9110fb1e3157f6995143c3fcdd723e Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 26 Jan 2020 00:36:06 +0100 Subject: [PATCH 1/4] Display fancy speed indicator --- .../activity/AudioplayerActivity.java | 5 +- .../activity/MediaplayerInfoActivity.java | 3 +- .../view/PlaybackSpeedIndicatorView.java | 101 ++++++++++++++++++ .../res/layout/mediaplayerinfo_activity.xml | 4 +- 4 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java diff --git a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java index 7f8c14b03..f24b13a9f 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java @@ -73,7 +73,7 @@ public class AudioplayerActivity extends MediaplayerInfoActivity { @Override protected void updatePlaybackSpeedButtonText() { - if(butPlaybackSpeed == null) { + if (butPlaybackSpeed == null) { return; } if (controller == null) { @@ -82,11 +82,12 @@ public class AudioplayerActivity extends MediaplayerInfoActivity { return; } float speed = 1.0f; - if(controller.canSetPlaybackSpeed()) { + if (controller.canSetPlaybackSpeed()) { speed = PlaybackSpeedUtils.getCurrentPlaybackSpeed(controller.getMedia()); } String speedStr = new DecimalFormat("0.00").format(speed); txtvPlaybackSpeed.setText(speedStr); + butPlaybackSpeed.setSpeed(speed); } @Override diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java index 21f4ad5be..43423f4a5 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java @@ -59,6 +59,7 @@ import de.danoeh.antennapod.fragment.PlaybackHistoryFragment; import de.danoeh.antennapod.fragment.QueueFragment; import de.danoeh.antennapod.fragment.SubscriptionFragment; import de.danoeh.antennapod.menuhandler.NavDrawerActivity; +import de.danoeh.antennapod.view.PlaybackSpeedIndicatorView; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; @@ -91,7 +92,7 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem NavListAdapter.SUBSCRIPTION_LIST_TAG }; - ImageButton butPlaybackSpeed; + PlaybackSpeedIndicatorView butPlaybackSpeed; TextView txtvPlaybackSpeed; ImageButton butCastDisconnect; private DrawerLayout drawerLayout; diff --git a/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java b/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java new file mode 100644 index 000000000..da750cf35 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java @@ -0,0 +1,101 @@ +package de.danoeh.antennapod.view; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Path; +import android.graphics.RectF; +import android.util.AttributeSet; +import android.view.View; +import androidx.annotation.Nullable; + +public class PlaybackSpeedIndicatorView extends View { + private final Paint arcPaint = new Paint(); + private final Paint indicatorPaint = new Paint(); + private final Path trianglePath = new Path(); + float angle = 0; + float targetAngle = 0.5f; + float degreePerFrame = 2; + float paddingArc = 20; + float paddingIndicator = 10; + double deg2rad = Math.PI / 180; + float paddingAngle = 30; + + public PlaybackSpeedIndicatorView(Context context) { + super(context); + setup(); + } + + public PlaybackSpeedIndicatorView(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + setup(); + } + + public PlaybackSpeedIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setup(); + } + + private void setup() { + arcPaint.setAntiAlias(true); + arcPaint.setColor(Color.GRAY); + arcPaint.setStyle(Paint.Style.STROKE); + arcPaint.setStrokeCap(Paint.Cap.ROUND); + arcPaint.setStrokeWidth(10); + + indicatorPaint.setAntiAlias(true); + indicatorPaint.setColor(Color.GRAY); + indicatorPaint.setStyle(Paint.Style.FILL); + + trianglePath.setFillType(Path.FillType.EVEN_ODD); + } + + public void setSpeed(float value) { + float MAX_ANGLE_PER_DIRECTION = 90+45- 2*paddingArc; + if (value >= 1) { + targetAngle = MAX_ANGLE_PER_DIRECTION * ((value-1)/3); + } else { + targetAngle = -MAX_ANGLE_PER_DIRECTION * (1-((value - 0.5f)*2)); + } + degreePerFrame = Math.abs(targetAngle - angle) / 20; + invalidate(); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + paddingArc = getMeasuredHeight()/5; + paddingIndicator = getMeasuredHeight()/10; + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + float radiusInnerCircle = getWidth()/8; + canvas.drawCircle(getWidth() / 2, getHeight() / 2, radiusInnerCircle, indicatorPaint); + + trianglePath.rewind(); + float bigRadius = getHeight() / 2 - paddingIndicator; + trianglePath.moveTo(getWidth() / 2 + (float)(bigRadius * Math.sin((-angle + 180) * deg2rad)), + getHeight() / 2 + (float)(bigRadius * Math.cos((-angle + 180) * deg2rad))); + trianglePath.lineTo(getWidth() / 2 + (float)(radiusInnerCircle * Math.sin((-angle + 180 -90) * deg2rad)), + getHeight() / 2 + (float)(radiusInnerCircle * Math.cos((-angle + 180-90) * deg2rad))); + trianglePath.lineTo(getWidth() / 2 + (float)(radiusInnerCircle * Math.sin((-angle + 180+90) * deg2rad)), + getHeight() / 2 + (float)(radiusInnerCircle * Math.cos((-angle + 180+90) * deg2rad))); + trianglePath.close(); + canvas.drawPath(trianglePath, indicatorPaint); + + RectF arcBounds = new RectF(paddingArc, paddingArc, getWidth()-paddingArc, getHeight()-paddingArc); + canvas.drawArc(arcBounds, -180-45, 90+45+angle-paddingAngle, false, arcPaint); + canvas.drawArc(arcBounds, -90 + paddingAngle+angle, 90+45 - paddingAngle-angle, false, arcPaint); + + + if (Math.abs(angle - targetAngle) > 0.5) { + angle += Math.signum(targetAngle - angle) * Math.min(degreePerFrame, Math.abs(targetAngle - angle)); + invalidate(); + } + } +} diff --git a/app/src/main/res/layout/mediaplayerinfo_activity.xml b/app/src/main/res/layout/mediaplayerinfo_activity.xml index 636cbb015..61a832043 100644 --- a/app/src/main/res/layout/mediaplayerinfo_activity.xml +++ b/app/src/main/res/layout/mediaplayerinfo_activity.xml @@ -159,7 +159,7 @@ android:textColor="?android:attr/textColorSecondary" android:clickable="false"/> - From 7b5435082042dc77de6e3fb5f59bf55fc71d3aa8 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 2 Feb 2020 18:17:57 +0100 Subject: [PATCH 2/4] Make indicator work on small screens --- .../view/PlaybackSpeedIndicatorView.java | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java b/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java index da750cf35..4b20cc0de 100644 --- a/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java +++ b/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java @@ -11,16 +11,17 @@ import android.view.View; import androidx.annotation.Nullable; public class PlaybackSpeedIndicatorView extends View { + private static final float DEG_2_RAD = (float) (Math.PI / 180); + private static final float PADDING_ANGLE = 30; + private final Paint arcPaint = new Paint(); private final Paint indicatorPaint = new Paint(); private final Path trianglePath = new Path(); - float angle = 0; - float targetAngle = 0.5f; - float degreePerFrame = 2; - float paddingArc = 20; - float paddingIndicator = 10; - double deg2rad = Math.PI / 180; - float paddingAngle = 30; + private float angle = 0; + private float targetAngle = 0.5f; + private float degreePerFrame = 2; + private float paddingArc = 20; + private float paddingIndicator = 10; public PlaybackSpeedIndicatorView(Context context) { super(context); @@ -42,7 +43,6 @@ public class PlaybackSpeedIndicatorView extends View { arcPaint.setColor(Color.GRAY); arcPaint.setStyle(Paint.Style.STROKE); arcPaint.setStrokeCap(Paint.Cap.ROUND); - arcPaint.setStrokeWidth(10); indicatorPaint.setAntiAlias(true); indicatorPaint.setColor(Color.GRAY); @@ -52,11 +52,12 @@ public class PlaybackSpeedIndicatorView extends View { } public void setSpeed(float value) { - float MAX_ANGLE_PER_DIRECTION = 90+45- 2*paddingArc; + float maxAnglePerDirection = 90 + 45 - 2 * paddingArc; if (value >= 1) { - targetAngle = MAX_ANGLE_PER_DIRECTION * ((value-1)/3); + // Speed values above 3 are probably not too common. Cap at 3 for better differentiation + targetAngle = maxAnglePerDirection * ((Math.min(3, value) - 1) / 2); } else { - targetAngle = -MAX_ANGLE_PER_DIRECTION * (1-((value - 0.5f)*2)); + targetAngle = -maxAnglePerDirection * (1 - ((value - 0.5f) * 2)); } degreePerFrame = Math.abs(targetAngle - angle) / 20; invalidate(); @@ -66,32 +67,32 @@ public class PlaybackSpeedIndicatorView extends View { protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); - paddingArc = getMeasuredHeight()/5; - paddingIndicator = getMeasuredHeight()/10; + paddingArc = getMeasuredHeight() / 5f; + paddingIndicator = getMeasuredHeight() / 10f; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); - float radiusInnerCircle = getWidth()/8; - canvas.drawCircle(getWidth() / 2, getHeight() / 2, radiusInnerCircle, indicatorPaint); + float radiusInnerCircle = getWidth() / 8f; + canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radiusInnerCircle, indicatorPaint); trianglePath.rewind(); - float bigRadius = getHeight() / 2 - paddingIndicator; - trianglePath.moveTo(getWidth() / 2 + (float)(bigRadius * Math.sin((-angle + 180) * deg2rad)), - getHeight() / 2 + (float)(bigRadius * Math.cos((-angle + 180) * deg2rad))); - trianglePath.lineTo(getWidth() / 2 + (float)(radiusInnerCircle * Math.sin((-angle + 180 -90) * deg2rad)), - getHeight() / 2 + (float)(radiusInnerCircle * Math.cos((-angle + 180-90) * deg2rad))); - trianglePath.lineTo(getWidth() / 2 + (float)(radiusInnerCircle * Math.sin((-angle + 180+90) * deg2rad)), - getHeight() / 2 + (float)(radiusInnerCircle * Math.cos((-angle + 180+90) * deg2rad))); + float bigRadius = getHeight() / 2f - paddingIndicator; + trianglePath.moveTo(getWidth() / 2f + (float) (bigRadius * Math.sin((-angle + 180) * DEG_2_RAD)), + getHeight() / 2f + (float) (bigRadius * Math.cos((-angle + 180) * DEG_2_RAD))); + trianglePath.lineTo(getWidth() / 2f + (float) (radiusInnerCircle * Math.sin((-angle + 180 - 90) * DEG_2_RAD)), + getHeight() / 2f + (float) (radiusInnerCircle * Math.cos((-angle + 180 - 90) * DEG_2_RAD))); + trianglePath.lineTo(getWidth() / 2f + (float) (radiusInnerCircle * Math.sin((-angle + 180 + 90) * DEG_2_RAD)), + getHeight() / 2f + (float) (radiusInnerCircle * Math.cos((-angle + 180 + 90) * DEG_2_RAD))); trianglePath.close(); canvas.drawPath(trianglePath, indicatorPaint); - RectF arcBounds = new RectF(paddingArc, paddingArc, getWidth()-paddingArc, getHeight()-paddingArc); - canvas.drawArc(arcBounds, -180-45, 90+45+angle-paddingAngle, false, arcPaint); - canvas.drawArc(arcBounds, -90 + paddingAngle+angle, 90+45 - paddingAngle-angle, false, arcPaint); - + arcPaint.setStrokeWidth(getHeight() / 15f); + RectF arcBounds = new RectF(paddingArc, paddingArc, getWidth() - paddingArc, getHeight() - paddingArc); + canvas.drawArc(arcBounds, -180 - 45, 90 + 45 + angle - PADDING_ANGLE, false, arcPaint); + canvas.drawArc(arcBounds, -90 + PADDING_ANGLE + angle, 90 + 45 - PADDING_ANGLE - angle, false, arcPaint); if (Math.abs(angle - targetAngle) > 0.5) { angle += Math.signum(targetAngle - angle) * Math.min(degreePerFrame, Math.abs(targetAngle - angle)); From 9d31cdc32f63859a61ea1b63728a95a7ea546f6e Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 21 Feb 2020 19:16:00 +0100 Subject: [PATCH 3/4] Respect app theme --- .../view/PlaybackSpeedIndicatorView.java | 17 +++++++++++------ core/src/main/res/values/attrs.xml | 1 + core/src/main/res/values/styles.xml | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java b/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java index 4b20cc0de..54731a412 100644 --- a/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java +++ b/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedIndicatorView.java @@ -1,14 +1,15 @@ package de.danoeh.antennapod.view; import android.content.Context; +import android.content.res.TypedArray; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; import androidx.annotation.Nullable; +import de.danoeh.antennapod.R; public class PlaybackSpeedIndicatorView extends View { private static final float DEG_2_RAD = (float) (Math.PI / 180); @@ -39,13 +40,17 @@ public class PlaybackSpeedIndicatorView extends View { } private void setup() { + int[] colorAttrs = new int[] {R.attr.action_icon_color }; + TypedArray a = getContext().obtainStyledAttributes(colorAttrs); + arcPaint.setColor(a.getColor(0, 0xffffffff)); + indicatorPaint.setColor(a.getColor(0, 0xffffffff)); + a.recycle(); + arcPaint.setAntiAlias(true); - arcPaint.setColor(Color.GRAY); arcPaint.setStyle(Paint.Style.STROKE); arcPaint.setStrokeCap(Paint.Cap.ROUND); indicatorPaint.setAntiAlias(true); - indicatorPaint.setColor(Color.GRAY); indicatorPaint.setStyle(Paint.Style.FILL); trianglePath.setFillType(Path.FillType.EVEN_ODD); @@ -67,15 +72,15 @@ public class PlaybackSpeedIndicatorView extends View { protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); - paddingArc = getMeasuredHeight() / 5f; - paddingIndicator = getMeasuredHeight() / 10f; + paddingArc = getMeasuredHeight() / 4.5f; + paddingIndicator = getMeasuredHeight() / 6f; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); - float radiusInnerCircle = getWidth() / 8f; + float radiusInnerCircle = getWidth() / 10f; canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radiusInnerCircle, indicatorPaint); trianglePath.rewind(); diff --git a/core/src/main/res/values/attrs.xml b/core/src/main/res/values/attrs.xml index aa93398f5..deeb0fa10 100644 --- a/core/src/main/res/values/attrs.xml +++ b/core/src/main/res/values/attrs.xml @@ -61,6 +61,7 @@ + diff --git a/core/src/main/res/values/styles.xml b/core/src/main/res/values/styles.xml index 9f72c9f6f..03309b523 100644 --- a/core/src/main/res/values/styles.xml +++ b/core/src/main/res/values/styles.xml @@ -70,8 +70,8 @@ @drawable/ic_volume_adaption_grey @color/master_switch_background_light @color/highlight_light - @style/PreferenceThemeOverlay.v14.Material + #FF757575