某些设备上的Surface.nativeLockCanvas上的ANR

时间:2016-03-11 08:07:28

标签: android surfaceview surfaceholder

我需要在加速度计的事件上绘制SurfaceView。 我创建了回调以检查表面准备好了。

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");
    }
});

在传感器监听器中,我锁定画布并绘制一些

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) {
    }
};

但是来自Transformer Pad (TF103CG) (K018)的一个客户端我收到描述等待的ANR,因为触摸的窗口尚未处理先前传递给它的输入事件。

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

ANR日志here.

的全文

哪里可能有问题?

1 个答案:

答案 0 :(得分:1)

作为一个解决方案,我创建另一个线程并将代码从onSensorChanged移动到它。 在一个线程中,我在画布上绘制并睡眠100毫秒。在onSensorChanged现在我只保存传感器值。