Skip to content

Commit a74caad

Browse files
authored
Render PianoCanvas as a regular View, not as a Surface (#101)
SurfaceView is overkill for this, since we are rendering using normal Canvas APIs.
1 parent 5ed8fdb commit a74caad

1 file changed

Lines changed: 10 additions & 37 deletions

File tree

app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
import android.util.Log;
1616
import android.view.Display;
1717
import android.view.MotionEvent;
18-
import android.view.SurfaceHolder;
19-
import android.view.SurfaceView;
18+
import android.view.View;
2019

2120
import androidx.annotation.NonNull;
2221
import androidx.appcompat.app.AppCompatActivity;
@@ -36,7 +35,7 @@
3635
/**
3736
* Renderer/View for our {@link Piano}.
3837
*/
39-
class PianoCanvas extends SurfaceView implements SurfaceHolder.Callback, PianoListener {
38+
class PianoCanvas extends View implements PianoListener {
4039
/** Relative draw-size of gear icon on next-expected config key */
4140
public static final float CONFIG_ICON_SIZE_TO_FLAT_KEY_RATIO = 0.5f;
4241
/** Relative draw-size of gear icon on already-held config keys */
@@ -63,7 +62,6 @@ public PianoCanvas(Context context, AttributeSet as) {
6362
public PianoCanvas(Context context, AttributeSet as, int defStyle) {
6463
super(context, as, defStyle);
6564
this.setFocusable(true);
66-
this.getHolder().addCallback(this);
6765

6866
final Point screen_size = new Point();
6967
final AppCompatActivity ctx;
@@ -151,13 +149,6 @@ public void setConfigRequestCallback(@NonNull AppConfigTrigger.AppConfigCallback
151149
this.appConfigTrigger.setConfigRequestCallback(cb);
152150
}
153151

154-
/** Resets the canvas to all-black*/
155-
private static void resetCanvas(Canvas canvas) {
156-
Paint p = new Paint();
157-
p.setColor(Color.BLACK);
158-
canvas.drawPaint(p);
159-
}
160-
161152
/**
162153
* Overlays gear icons onto the currently-held and next expected flat keys.
163154
*/
@@ -276,23 +267,18 @@ void draw_icon_on_black_key(final Canvas canvas, final Drawable icon, int key_id
276267
icon.draw(canvas);
277268
}
278269

279-
@Override
280-
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
281-
Log.i("PianOli::PianoCanvas", "surfaceCreated");
282-
redraw(surfaceHolder);
283-
}
284-
285270
public void redraw() {
286-
redraw(getHolder());
271+
invalidate();
287272
}
288273

289-
public void redraw(SurfaceHolder surfaceHolder) {
290-
if (surfaceHolder == null) return;
291-
292-
Canvas canvas = surfaceHolder.lockCanvas();
293-
if (canvas == null) return;
274+
private final Paint backgroundPaint = new Paint();
275+
{
276+
backgroundPaint.setColor(Color.BLACK);
277+
}
294278

295-
resetCanvas(canvas);
279+
@Override
280+
protected void onDraw(@NonNull Canvas canvas) {
281+
canvas.drawPaint(backgroundPaint);
296282

297283
// draw main, big keys (even key index)
298284
for (int i = 0; i < piano.get_keys_count(); i += 2) {
@@ -306,8 +292,6 @@ public void redraw(SurfaceHolder surfaceHolder) {
306292

307293
// Gear icons drawn after small keys, since they go on top of those.
308294
drawConfigGears(canvas);
309-
310-
surfaceHolder.unlockCanvasAndPost(canvas);
311295
}
312296

313297
@Override
@@ -412,15 +396,4 @@ public boolean onTouchEvent(MotionEvent event) {
412396
return super.onTouchEvent(event);
413397
}
414398
}
415-
416-
417-
@Override
418-
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
419-
Log.i("PianOli::PianoCanvas", "surfaceChanged: ignoring!");
420-
}
421-
422-
@Override
423-
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
424-
Log.i("PianOli::PianoCanvas", "surfaceDestroyed: ignoring!");
425-
}
426399
}

0 commit comments

Comments
 (0)