我对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);
}
}
答案 0 :(得分:0)
我猜你正在使用相机。
如果您关闭应用,app / mainActivity的生命周期将从头开始重复(如果您没有后台服务)。
如果你没有在onPause()或onStop()或OnDestroy()等中正确释放相机资源,那么新执行可以拥有前一个的唯一记忆......
你打电话给之类的东西吗? if(camera!=null){
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
在持有相机的活动的OnDestroy()方法中 或者在surfaceDestroyed()中使用SurfaceView?