Can';不要将画布绘制成android位图

Can';不要将画布绘制成android位图,android,bitmap,android-canvas,Android,Bitmap,Android Canvas,嗨,伙计们,我正在努力使用位图和画布。我试图做的是用相机拍摄一张照片,然后允许用户创建两个矩形(通过滑动手指),并在图像中标记它们(矩形应该标记,直到按下按钮,照片才保存,它总是在内存中)。因此,在一个相机示例中,我制作了一个带有SurfaceView的布局来包含相机预览,然后我添加了代码来在onPictureTaken方法中绘制矩形。我已经搜索了一些关于如何实现它的示例,但当然没有效果。到目前为止,我有以下代码(在onPictureTaken中): 我没有任何例外,但是没有画矩形,所以如果有人

嗨,伙计们,我正在努力使用位图和画布。我试图做的是用相机拍摄一张照片,然后允许用户创建两个矩形(通过滑动手指),并在图像中标记它们(矩形应该标记,直到按下按钮,照片才保存,它总是在内存中)。因此,在一个相机示例中,我制作了一个带有SurfaceView的布局来包含相机预览,然后我添加了代码来在onPictureTaken方法中绘制矩形。我已经搜索了一些关于如何实现它的示例,但当然没有效果。到目前为止,我有以下代码(在onPictureTaken中):

我没有任何例外,但是没有画矩形,所以如果有人能告诉我我做错了什么,我会非常感激。另外,对于我的特定场景,是否适合使用GestureDetector而不是创建自定义OnTouchListener?
提前感谢。

您创建了画布和位图,但从未将它们连接到视图:

Bitmap bmOverlay = Bitmap.createBitmap(bitmapPicture.getWidth(), bitmapPicture.getHeight(), bitmapPicture.getConfig());
Canvas canvas = new Canvas(bmOverlay);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(3);
canvas.drawRect(/*all of my end coordinates*/, paint);
if (coordinates for rect1 are set) {
    Paint paint = new Paint();
    Canvas canvas = new Canvas(bitmapPicture);
    paint.setColor(Color.GREEN);
    paint.setStrokeWidth(3);
    canvas.drawRect(/*all of my source coordinates*/, paint);
    } else {
        if (coordinates for rect2 are set) {
            Paint paint = new Paint();
            Canvas canvas = new Canvas(bitmapPicture);
            paint.setColor(Color.YELLOW);
            paint.setStrokeWidth(3);
            canvas.drawRect(/*all of my end coordinates*/, paint);
        }
    }
}
yourImageView.setImageDrawable(new BitmapDrawable(getResources(), bitmapPicture));
您需要创建一个位图(在顶部创建),然后在上面绘制两个矩形,然后将该位图连接到视图:

Bitmap bmOverlay = Bitmap.createBitmap(bitmapPicture.getWidth(), bitmapPicture.getHeight(), bitmapPicture.getConfig());
Canvas canvas = new Canvas(bmOverlay);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(3);
canvas.drawRect(/*all of my end coordinates*/, paint);
if (coordinates for rect1 are set) {
    Paint paint = new Paint();
    Canvas canvas = new Canvas(bitmapPicture);
    paint.setColor(Color.GREEN);
    paint.setStrokeWidth(3);
    canvas.drawRect(/*all of my source coordinates*/, paint);
    } else {
        if (coordinates for rect2 are set) {
            Paint paint = new Paint();
            Canvas canvas = new Canvas(bitmapPicture);
            paint.setColor(Color.YELLOW);
            paint.setStrokeWidth(3);
            canvas.drawRect(/*all of my end coordinates*/, paint);
        }
    }
}
yourImageView.setImageDrawable(new BitmapDrawable(getResources(), bitmapPicture));

好吧,过了很长时间,我可以画成位图了。碰巧我遇到了几个错误:首先,我试图在SurfaceView中绘制,这是不可能的,其次,我添加了一个ImageView以包含位图和画布,因此,现在我的代码如下所示:

Bitmap tempBitmap = Bitmap.createScaledBitmap(bt, bt.getWidth(), bt.getHeight(), true);
Canvas canvas = new Canvas(tempBitmap);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
canvas.drawLine(x1, y1, x2, y1, paint);//up
canvas.drawLine(x1, y1, x1, y2, paint);//left
canvas.drawLine(x1, y2, x2, y2, paint);//down
canvas.drawLine(x2, y1, x2, y2, paint);

ImageView iView = (ImageView)findViewById(R.id.imageViewPreview);
iView.setImageBitmap(tempBitmap);
iView.draw(canvas);
这段代码位于一个单独的活动中,在该活动中,我实现了一个
OnTouchListener
,以便读取绘制矩形的坐标。读取坐标后,执行代码。
作为参考,该布局是一个包含ImageView的FrameLayout(查看答案)。

这段代码终于画出了一个矩形,但幸运的是,图像越来越大:(,但这是另一个问题,所以如果你想跟着它走。

Hi@HaIR,谢谢你的回答,但它没有起作用:我认为你对未连接的位图和画布的看法是正确的,但当我运行应用程序时,我有一个例外“IllegalStateException:在执行“new Canvas(bitmapPicture);”时传递给画布构造函数的不可变位图”(我读过一次不应该使用原始位图)。这是有道理的。您是否尝试创建一个新位图并将其附加到视图setImageView到imageView?无论哪种方式,您都需要将其连接到视图层次结构,而不仅仅是浮动在边缘。是的@HaIR,我尝试将与画布关联的位图设置为imageView,但没有发生任何事情。关于这一点,在我的第一封im中,我有一个问题实施我只有一个SurfaceView,但是现在由于你的回答,我添加了一个ImageView,这是正确的(我的意思是让SurfaceView和ImageView在同一个布局中)还是应该使用ImageView而不是SurfaceView,或者它应该在另一个布局中?如果你有两个视图,请确保它们都在你的布局中(可以在布局xml中进行设置,也可以添加到代码中),并且顶部有一个清晰的颜色背景。