繁体   English   中英

无法将画布绘制到Android位图中

[英]Can't draw canvas into android bitmap

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

        final Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
                arg0.length);
        surfaceView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (source coordinates of rect1 are not set) {
                        setSourceCoordinatesForRect1FromArg1();
                    } else {
                        setSourceCoordinatesForRect2FromArg1();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (end coordinates of rect1 are not set) {
                        setEndCoordinatesForRect1FromArg1();
                    } else {
                        setEndCoordinatesForRect2FromArg1();
                    }
                    break;
                default:
                    break;
                }
                if (coordinates for rect1 are set) {
                    Paint paint = new Paint();
                    Bitmap bmOverlay = Bitmap.createBitmap(bitmapPicture.getWidth(), bitmapPicture.getHeight(), bitmapPicture.getConfig());
                    Canvas canvas = new Canvas(bmOverlay);
                    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();
                        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);
                    }
                }
                return true;
            }
        });

我没有任何例外,但是没有绘制矩形,因此,如果有人可以告诉我我做错了什么,我将不胜感激。 另外,对于我的特定情况,是否适合使用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));

好了,经过很长时间,我可以绘制一个位图。 碰巧我遇到了几个错误:首先,我试图在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(请看一下答案)。
这段代码最终绘制了一个矩形,但是幸运的是,图像变得越来越大:(,但这是另一个问题,因此如果您要遵循它,请点击这里的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM