Make indicator work on small screens

This commit is contained in:
ByteHamster 2020-02-02 18:17:57 +01:00
parent 95f23c837c
commit 7b54350820
1 changed files with 27 additions and 26 deletions

View File

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