Do not rotate speed indicator if setSpeed is never called

This commit is contained in:
ByteHamster 2020-03-22 23:14:59 +01:00
parent 3d7f93771a
commit 72b85f2472

View File

@ -20,7 +20,7 @@ public class PlaybackSpeedIndicatorView extends View {
private final Paint indicatorPaint = new Paint();
private final Path trianglePath = new Path();
private float angle = VALUE_UNSET;
private float targetAngle = 0.5f;
private float targetAngle = VALUE_UNSET;
private float degreePerFrame = 2;
private float paddingArc = 20;
private float paddingIndicator = 10;
@ -41,6 +41,9 @@ public class PlaybackSpeedIndicatorView extends View {
}
private void setup() {
setSpeed(1.0f); // Set default angle to 1.0
targetAngle = VALUE_UNSET; // Do not move to first value that is set externally
int[] colorAttrs = new int[] {R.attr.action_icon_color };
TypedArray a = getContext().obtainStyledAttributes(colorAttrs);
arcPaint.setColor(a.getColor(0, 0xffffffff));
@ -61,10 +64,11 @@ public class PlaybackSpeedIndicatorView extends View {
float maxAnglePerDirection = 90 + 45 - 2 * paddingArc;
// Speed values above 3 are probably not too common. Cap at 3 for better differentiation
float normalizedValue = Math.min(2.5f, value - 0.5f) / 2.5f; // Linear between 0 and 1
targetAngle = -maxAnglePerDirection + 2 * maxAnglePerDirection * normalizedValue;
if (angle == VALUE_UNSET) {
angle = targetAngle;
float target = -maxAnglePerDirection + 2 * maxAnglePerDirection * normalizedValue;
if (targetAngle == VALUE_UNSET) {
angle = target;
}
targetAngle = target;
degreePerFrame = Math.abs(targetAngle - angle) / 20;
invalidate();
}
@ -100,7 +104,7 @@ public class PlaybackSpeedIndicatorView extends View {
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) {
if (Math.abs(angle - targetAngle) > 0.5 && targetAngle != VALUE_UNSET) {
angle += Math.signum(targetAngle - angle) * Math.min(degreePerFrame, Math.abs(targetAngle - angle));
invalidate();
}