【问题标题】:Android Canvas onDraw problems when onResumeAndroid Canvas onDraw onDraw 时 onResume 问题
【发布时间】:2015-07-09 09:12:04
【问题描述】:

我对 android 中的画布 有一点小问题。 我正在为扫描仪编程。 扫描仪的功能完美运行。 但我的目标是在扫描仪中绘制一些矩形,让它看起来像一个真正的扫描仪。 这样就成功了一半。

如果我第一次启动应用程序,一切都很好。所有矩形和扫描线都绘制在中间。 但是如果我关闭应用程序并重新启动它,矩形和线条就会丢失

我有 onDraw 方法,并且我知道应用程序正在调用它(两次)。 我检查了所有变量(案例:首次启动,重新启动)。 如果我关闭我的设备并启动新的应用程序,它也可以工作。

也许我必须把元素放在前面?

谢谢。

@SuppressLint("DrawAllocation")
    @Override
    public void onDraw(Canvas canvas) {

        setWillNotDraw(false);
        super.onDraw(canvas);

        if (cameraManager == null) {
            return; // not ready yet, early draw before done configuring
        }
        Rect frame = cameraManager.getFramingRect();
        Rect previewFrame = cameraManager.getFramingRectInPreview();

        if (frame == null || previewFrame == null) {
            return;
        }
        int width = canvas.getWidth();
        int height = canvas.getHeight();

        // Draw the exterior (i.e. outside the framing rect) darkened
        paint.setColor(resultBitmap != null ? resultColor : maskColor);
        canvas.drawRect(0, 0, width, frame.top, paint);
        canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
        canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1,
                paint);
        canvas.drawRect(0, frame.bottom + 1, width, height, paint);


        if (resultBitmap != null) {
            // Draw the opaque result bitmap over the scanning rectangle
            paint.setAlpha(CURRENT_POINT_OPACITY);
            canvas.drawBitmap(resultBitmap, null, frame, paint);
        } else {

            // Draw a red "laser scanner" line through the middle to show
            // decoding is active
            paint.setColor(laserColor);
            paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
            scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
            int middle = frame.height() / 2 + frame.top;
            canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1,
                    middle + 2, paint);



            float scaleX = frame.width() / (float) previewFrame.width();
            float scaleY = frame.height() / (float) previewFrame.height();

            List<ResultPoint> currentPossible = possibleResultPoints;
            List<ResultPoint> currentLast = lastPossibleResultPoints;
            int frameLeft = frame.left;
            int frameTop = frame.top;
            if (currentPossible.isEmpty()) {
                lastPossibleResultPoints = null;
            } else {
                possibleResultPoints = new ArrayList<ResultPoint>(5);
                lastPossibleResultPoints = currentPossible;
                paint.setAlpha(CURRENT_POINT_OPACITY);
                paint.setColor(resultPointColor);
                synchronized (currentPossible) {
                    for (ResultPoint point : currentPossible) {
                        canvas.drawCircle(frameLeft
                                + (int) (point.getX() * scaleX), frameTop
                                + (int) (point.getY() * scaleY), POINT_SIZE,
                                paint);
                    }
                }
            }
            if (currentLast != null) {
                paint.setAlpha(CURRENT_POINT_OPACITY / 2);
                paint.setColor(resultPointColor);
                synchronized (currentLast) {
                    float radius = POINT_SIZE / 2.0f;
                    for (ResultPoint point : currentLast) {
                        canvas.drawCircle(frameLeft
                                + (int) (point.getX() * scaleX), frameTop
                                + (int) (point.getY() * scaleY), radius, paint);
                    }
                }
            }

            // Request another update at the animation interval, but only
            // repaint the laser line,
            // not the entire viewfinder mask.
            postInvalidateDelayed(ANIMATION_DELAY, frame.left - POINT_SIZE,
                    frame.top - POINT_SIZE, frame.right + POINT_SIZE,
                    frame.bottom + POINT_SIZE);
        }




    }

【问题讨论】:

  • 请出示代码。
  • 你的问题解决了吗?

标签: java android canvas oncreate onresume


【解决方案1】:

我猜你在用相机。

如果您关闭应用程序,您的应用程序/mainActivity 的生命周期会从头开始重复(如果您没有某些后台服务)。

新执行对前一个执行的唯一记忆是您没有在 onPause() 或 onStop() 或 OnDestroy() 等中正确释放相机资源...

你打过类似的电话

   if(camera!=null){
        camera.stopPreview();
        camera.setPreviewCallback(null);
        camera.release();
        camera = null;
    }

在持有相机的 Activity 的 OnDestroy() 方法中 如果您使用的是surfaceView,或者在surfaceDestroyed() 中?

【讨论】:

  • 代码已发布.. 是的,我使用相机
  • 好吧,我只有一个surfaceDestroyed方法,但是这个方法的实际调用是什么时候。当我离开方法?还是应用程序实际上被破坏了?我只有一个surfaceChanged。我是否必须将您的代码放入我的 MainActivity 或 CameraActivity 或 Surface Activity 中?我只有一个 SurfaceHolder?!这有用吗?我有一个 CameraManager。
  • 刚试过,但没有解决问题。而且我的 cameraManager 没有 setPreviewCallback 也没有 release()。
  • 可能是问题在于,方法surfaceCreated 或surfaceChanged 从未调用过?在日志中我可以看到调试消息“应用程序通过 NULL 表面”?
猜你喜欢
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多