commit dceb0e1d6e560bd82e86d6e27db4f7387c3c65fe
parent a38b7b6bd9d23bebfdd435fe63988d10d7e7eba8
Author: gstraube <gstraube@mailbox.org>
Date: Wed, 5 Jul 2017 18:04:17 +0200
Introduce custom view for tuner
Diffstat:
4 files changed, 101 insertions(+), 24 deletions(-)
diff --git a/app/src/main/java/com/github/cythara/MainActivity.java b/app/src/main/java/com/github/cythara/MainActivity.java
@@ -5,7 +5,6 @@ import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
-import android.widget.TextView;
import java.lang.ref.WeakReference;
import java.util.Locale;
@@ -60,21 +59,22 @@ public class MainActivity extends AppCompatActivity {
if (pitch != -1) {
PitchDifference pitchDifference = PitchComparator.retrieveNote(pitch);
- String msg = "";
+ String msg;
if (Math.abs(pitchDifference.deviation) < MAX_DEVIATION) {
msg = String.format(Locale.US, "Closest: %s Diff: %f Freq: %f",
pitchDifference.closest.getGuitarString(),
pitchDifference.deviation, pitch);
- Log.d("com.github.cythara", msg);
- }
- Message message = new Message();
- Bundle bundle = new Bundle();
- bundle.putString("pitch", msg);
- message.setData(bundle);
+ Message message = new Message();
+ Bundle bundle = new Bundle();
+ bundle.putParcelable("pitchDiff", pitchDifference);
+ message.setData(bundle);
+
+ activity.updateHandler.sendMessage(message);
- activity.updateHandler.sendMessage(message);
+ Log.d("com.github.cythara", msg);
+ }
}
}
};
@@ -104,9 +104,12 @@ public class MainActivity extends AppCompatActivity {
MainActivity activity = mainActivity.get();
if (activity != null) {
- TextView pitchText = (TextView) activity.findViewById(R.id.pitch);
+ TunerView tunerView = (TunerView) activity.findViewById(R.id.pitch);
+
+ PitchDifference pitchDiff = msg.getData().getParcelable("pitchDiff");
- pitchText.setText(msg.getData().getString("pitch"));
+ tunerView.setPitchDifference(pitchDiff);
+ tunerView.invalidate();
}
}
}
diff --git a/app/src/main/java/com/github/cythara/PitchDifference.java b/app/src/main/java/com/github/cythara/PitchDifference.java
@@ -1,6 +1,9 @@
package com.github.cythara;
-class PitchDifference {
+import android.os.Parcel;
+import android.os.Parcelable;
+
+class PitchDifference implements Parcelable {
final Note closest;
final double deviation;
@@ -9,4 +12,31 @@ class PitchDifference {
this.closest = closest;
this.deviation = deviation;
}
-}
+
+ private PitchDifference(Parcel in) {
+ closest = Note.valueOf(in.readString());
+ deviation = in.readDouble();
+ }
+
+ @Override
+ public int describeContents() {
+ return 0;
+ }
+
+ @Override
+ public void writeToParcel(Parcel dest, int flags) {
+ dest.writeString(closest.name());
+ dest.writeDouble(deviation);
+ }
+
+ public static final Parcelable.Creator<PitchDifference> CREATOR
+ = new Parcelable.Creator<PitchDifference>() {
+ public PitchDifference createFromParcel(Parcel in) {
+ return new PitchDifference(in);
+ }
+
+ public PitchDifference[] newArray(int size) {
+ return new PitchDifference[size];
+ }
+ };
+}
+\ No newline at end of file
diff --git a/app/src/main/java/com/github/cythara/TunerView.java b/app/src/main/java/com/github/cythara/TunerView.java
@@ -0,0 +1,42 @@
+package com.github.cythara;
+
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.util.AttributeSet;
+import android.view.View;
+
+public class TunerView extends View {
+
+ private Paint paint = new Paint();
+ private PitchDifference pitchDifference;
+
+ public TunerView(Context context) {
+ super(context);
+ }
+
+ public TunerView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ @Override
+ public void onDraw(Canvas canvas) {
+ super.onDraw(canvas);
+
+ paint.setColor(Color.BLACK);
+ paint.setTextSize(60F);
+
+ float x = canvas.getWidth() / 2F;
+ float y = canvas.getHeight() / 2F;
+
+ if (pitchDifference != null) {
+ canvas.drawText(pitchDifference.closest.getGuitarString(), x, y, paint);
+ }
+ }
+
+ public void setPitchDifference(PitchDifference pitchDifference) {
+ this.pitchDifference = pitchDifference;
+ }
+}
+\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
@@ -6,16 +6,15 @@
android:layout_height="match_parent"
tools:context="com.github.cythara.MainActivity">
- <TextView
+ <com.github.cythara.TunerView
android:id="@+id/pitch"
- android:layout_width="187dp"
- android:layout_height="85dp"
- android:layout_marginRight="112dp"
- android:layout_marginTop="245dp"
- android:lineSpacingExtra="8sp"
- android:text="Listening…"
- android:textAlignment="center"
- android:textSize="24sp"
app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-</android.support.constraint.ConstraintLayout>
+ app:layout_constraintTop_toTopOf="parent"
+ android:layout_height="587dp"
+ android:layout_width="395dp"
+ android:layout_marginTop="0dp"
+ android:layout_marginRight="8dp"
+ app:layout_constraintBottom_toBottomOf="parent"
+ android:layout_marginBottom="8dp" />
+
+</android.support.constraint.ConstraintLayout>
+\ No newline at end of file