Java 恢复时,Android Canvas会出现问题

Java 恢复时,Android Canvas会出现问题,java,android,canvas,oncreate,onresume,Java,Android,Canvas,Oncreate,Onresume,我对android中的画布有一个小问题。 我正在为扫描仪编程。 扫描器的功能非常好。 但我的目标是在扫描仪中绘制一些矩形,使其看起来像真正的扫描仪。 一半的时间都是这样 如果我第一次启动应用程序,一切正常。所有矩形和扫描仪线都画在中间。 但是如果我关闭应用程序并重新启动,矩形和线条就会丢失 我有onDraw方法,我知道应用程序正在调用它(两次)。 我已经检查了所有变量(案例:首次启动,重新启动)。 如果我关闭设备并启动新的应用程序,它也会工作 也许我必须把这些元素带到最前面 谢谢 @Suppre

我对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);
        }




    }
@SuppressLint(“DrawAllocation”)
@凌驾
公共空白onDraw(画布){
setWillNotDraw(假);
super.onDraw(帆布);
如果(cameraManager==null){
return;//尚未准备好,配置完成前提前绘制
}
Rect frame=cameraManager.getFramingRect();
Rect previewFrame=cameraManager.getFramingRectInPreview();
如果(帧==null | |预览帧==null){
返回;
}
int width=canvas.getWidth();
int height=canvas.getHeight();
//绘制变暗的外部(即框架矩形外部)
setColor(resultBitmap!=null?resultColor:maskColor);
canvas.drawRect(0,0,宽度,frame.top,油漆);
canvas.drawRect(0,frame.top,frame.left,frame.bottom+1,paint);
canvas.drawRect(frame.right+1,frame.top,width,frame.bottom+1,
油漆);
canvas.drawRect(0,frame.bottom+1,宽度、高度、油漆);
if(resultBitmap!=null){
//在扫描矩形上绘制不透明结果位图
paint.setAlpha(当前点不透明度);
drawBitmap(resultBitmap,null,frame,paint);
}否则{
//在中间画一条红色的“激光扫描仪”线以显示
//解码处于活动状态
油漆。设置颜色(激光颜色);
paint.setAlpha(扫描仪α[scannerAlpha]);
scannerAlpha=(scannerAlpha+1)%SCANNER_α长度;
int middle=frame.height()/2+frame.top;
canvas.drawRect(frame.left+2,middle-1,frame.right-1,
中间+2,油漆);
float scaleX=frame.width()/(float)previewFrame.width();
float scaleY=frame.height()/(float)previewFrame.height();
列出当前可能=可能的结果点;
List currentLast=lastPossibleResultPoints;
int frameLeft=frame.left;
int frameTop=frame.top;
if(currentAvailable.isEmpty()){
lastPossibleResultPoints=null;
}否则{
possibleResultPoints=新的ArrayList(5);
lastPossibleResultPoints=当前可能;
paint.setAlpha(当前点不透明度);
paint.setColor(结果点颜色);
已同步(可能){
for(结果点:当前可能){
画布.画圈(左框)
+(int)(point.getX()*scaleX),框架顶部
+(int)(point.getY()*scaleY),point_大小,
油漆);
}
}
}
如果(currentLast!=null){
paint.setAlpha(当前点不透明度/2);
paint.setColor(结果点颜色);
已同步(currentLast){
浮动半径=点尺寸/2.0f;
for(结果点:currentLast){
画布.画圈(左框)
+(int)(point.getX()*scaleX),框架顶部
+(int)(point.getY()*scaleY)、半径、绘制);
}
}
}
//在动画间隔请求另一个更新,但仅限于
//重新粉刷激光线,
//不是整个取景器遮罩。
postInvalidateDelayed(动画延迟,frame.left-点大小,
frame.top-点大小,frame.right+点大小,
框架(底部+点尺寸);
}
}

我猜你在用相机

如果关闭应用程序,应用程序/main活动的生命周期将从一开始就重复(如果您没有后台服务)

新执行对上一次执行的唯一记忆是,如果您没有在onPause()或onStop()或OnDestroy()等中正确释放相机资源

你打过类似的电话吗

   if(camera!=null){
        camera.stopPreview();
        camera.setPreviewCallback(null);
        camera.release();
        camera = null;
    }
在OnDestroy()中,用于固定相机的活动的方法
或者在surfaceDestroyed()中,如果您正在使用surfaceView?

我猜您正在使用相机

如果关闭应用程序,应用程序/main活动的生命周期将从一开始就重复(如果您没有后台服务)

新执行对上一次执行的唯一记忆是,如果您没有在onPause()或onStop()或OnDestroy()等中正确释放相机资源

你打过类似的电话吗

   if(camera!=null){
        camera.stopPreview();
        camera.setPreviewCallback(null);
        camera.release();
        camera = null;
    }
在OnDestroy()中,用于固定相机的活动的方法
或者在surfaceDestroyed()中,如果您正在使用surfaceView?

请显示代码。您解决了问题吗?请显示代码。您解决了问题吗?代码已发布。。是的,我使用cameraWell,我只有一个surfaceDestroyed方法,但是这个方法的实际调用是什么时候。我什么时候离开这个方法?或者应用程序真的被破坏了?我只是换了一张表。我必须输入你的密码吗