I need to draw over SurfaceView on events from accelerometer.
I create callback for checking that surface ready.
overlaySurfaceView = (SurfaceView) findViewById(R.id.overlay_surface);
overlaySurfaceView.setZOrderMediaOverlay(true);
overlayHolder = overlaySurfaceView.getHolder();
overlayHolder.setFormat(PixelFormat.TRANSPARENT);
overlayHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
overlayReady = true;
Log.d(TAG, "SET overlayReady = true");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
overlayReady = false;
Log.d(TAG, "SET overlayReady = false");
}
});
In sensor listener I lock canvas and draw somethng
private final SensorEventListener orientationListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
Log.d(TAG, "onSensorChanged: overlayReady = " + overlayReady);
if (!overlayReady)
return;
if (overlayHolder == null || overlayHolder.isCreating() || !overlayHolder.getSurface().isValid()) {
return;
}
Canvas canvas = overlayHolder.lockCanvas();
if (canvas == null) {
return;
}
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
// Some drawing
overlayHolder.unlockCanvasAndPost(canvas);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
But from one client with Transformer Pad (TF103CG) (K018) I get ANR with description Waiting because the touched window has not finished processing the input events that were previously delivered to it.
----- pid 10710 at 2016-03-02 17:25:43 -----
Cmd line: com.####.#####
JNI: CheckJNI is off; workarounds are off; pins=0; globals=295
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0)
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x430f7340 self=0x430e4620
| sysTid=10710 nice=0 sched=0/0 cgrp=apps handle=1073954880
| state=S schedstat=( 11049431919 2136334229 17921 ) utm=902 stm=202 core=1
(native backtrace unavailable)
at android.view.Surface.nativeLockCanvas(Native Method)
at android.view.Surface.lockCanvas(Surface.java:243)
at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:822)
at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:790)
at com.####.#####.CustomCameraActivity$6.onSensorChanged(CustomCameraActivity.java:342)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:418)
Full text of ANR log here.
Where there can be a problem?