【问题标题】:Can't draw canvas into android bitmap无法将画布绘制到android位图中
【发布时间】:2013-05-01 00:41:33
【问题描述】:

大家好,我正在为位图和画布而苦苦挣扎。我想要做的是用相机拍照,然后允许用户创建两个矩形(通过滑动手指)并在图像中标记它们(矩形应该被标记,直到按下按钮,照片没有保存,它总是在内存中)。因此,基于相机示例,我使用 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? 提前致谢。

【问题讨论】:

    标签: android bitmap android-canvas


    【解决方案1】:

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

    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));
    

    【讨论】:

    • 嗨@HaIR,谢谢你的回答,但它没有用:我认为你对未连接的位图和画布是正确的,但是当我运行应用程序时出现异常“IllegalStateException:当我执行“new Canvas(bitmapPicture);”时,不可变位图传递给 Canvas 构造函数(我读过一次,我不应该使用原始位图)。
    • 这是有道理的。您是否尝试制作新位图并将其视图 setImageView 附加到 imageView?无论哪种方式,您都需要将其连接到您的视图层次结构,而不仅仅是漂浮在边缘。
    • 是的@HaIR,我尝试将与画布关联的位图设置为 ImageView,但什么也没发生。关于这一点我有一个问题,在我的第一个实现中,我只有一个 SurfaceView,但现在由于您的回答,我添加了一个 ImageView,这是正确的(我的意思是 SurfaceView 和 ImageView 在相同的布局中)还是应该使用 ImageView 而不是 SurfaceView,还是应该在另一个布局中?
    • 如果您有两个视图,请确保它们都在您的布局中(在布局 xml 中设置,或在您的代码中添加)并且顶部的视图具有清晰的颜色背景。跨度>
    【解决方案2】:

    好吧,过了很长时间我可以画成位图了。碰巧我有几个错误:首先,我试图在 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(请查看this 答案)。
    这段代码终于画了一个矩形,但幸运的是图像越来越大:(,但这是另一个问题,所以如果你想关注它here is the link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-14
      • 2012-11-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多