commit f6567bd53dc94ea2634ed28f90c8e189e9b0b4cc
parent 34e5616e54c603da97b42942fdff2aec5d3a1af6
Author: gstraube <gstraube@mailbox.org>
Date: Thu, 13 Jul 2017 20:27:33 +0200
Display gauge to indicate the pitch deviation
Diffstat:
2 files changed, 52 insertions(+), 1 deletion(-)
diff --git a/app/src/main/java/com/github/cythara/TunerView.java b/app/src/main/java/com/github/cythara/TunerView.java
@@ -1,6 +1,5 @@
package com.github.cythara;
-
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
@@ -14,6 +13,8 @@ public class TunerView extends View {
private static final double MAX_ALLOWED_DEVIATION = 3D;
private TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
+ private TextPaint numbersPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
+ private Paint gaugePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint background = new Paint();
private PitchDifference pitchDifference;
@@ -36,10 +37,58 @@ public class TunerView extends View {
if (pitchDifference != null) {
setBackground(canvas);
+ drawGauge(canvas);
+
drawText(canvas);
}
}
+ private void drawGauge(Canvas canvas) {
+ float x = canvas.getWidth() / 2F;
+ float y = canvas.getHeight() / 2F;
+
+ gaugePaint.setColor(Color.BLACK);
+
+ int gaugeSize = getResources().getDimensionPixelSize(R.dimen.gaugeSize);
+ gaugePaint.setStrokeWidth(gaugeSize);
+
+ int textSize = getResources().getDimensionPixelSize(R.dimen.numbersTextSize);
+ numbersPaint.setTextSize(textSize);
+
+ float gaugeWidth = 0.45F * canvas.getWidth();
+
+ canvas.drawLine(x - gaugeWidth, y, x + gaugeWidth, y, gaugePaint);
+
+ float spaceWidth = gaugeWidth / 3F;
+
+ for (int i = 0; i <= 30; i = i + 10) {
+ if (i % 10 == 0) {
+ float xPos = x + (i / 10F) * spaceWidth;
+ canvas.drawLine(xPos, y - 10, xPos, y + 10, gaugePaint);
+ String text = String.valueOf(i);
+ canvas.drawText(text, xPos - numbersPaint.measureText(text) / 2F, y - 30,
+ numbersPaint);
+ }
+ }
+
+ for (int i = 30; i >= 0; i = i - 10) {
+ if (i % 10 == 0) {
+ float xPos = x - (i / 10F) * spaceWidth;
+ canvas.drawLine(xPos, y - 10, xPos, y + 10, gaugePaint);
+ String text = String.valueOf(i);
+ canvas.drawText(text, xPos - numbersPaint.measureText(text) / 2F, y - 30,
+ numbersPaint);
+ }
+ }
+
+ float deviation = (float) pitchDifference.deviation;
+
+ float xPos = x + (deviation * gaugeWidth / 30F);
+ String text = "|";
+ canvas.drawText(text, xPos - numbersPaint.measureText(text) / 2F, y + 30,
+ numbersPaint);
+ }
+
private void drawText(Canvas canvas) {
float x = canvas.getWidth() / 2F;
float y = canvas.getHeight() - canvas.getHeight() / 3F;
diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="noteTextSize">40sp</dimen>
+ <dimen name="numbersTextSize">20sp</dimen>
+ <dimen name="gaugeSize">2sp</dimen>
</resources>
\ No newline at end of file