8

I'm creating an app that does drawings and such under the users control and let's them save it. The way I'm trying to achieve this is by using a custom Bitmap on which the canvas draws then saving the resulting Bitmap.

Everything is working as expected, until Canvas.setBitmap(Bitmap) is called.

I get the following error.

03-24 13:47:50.741: E/AndroidRuntime(27888): FATAL EXCEPTION: main
03-24 13:47:50.741: E/AndroidRuntime(27888): Process: example.imageeditor, PID: 27888
03-24 13:47:50.741: E/AndroidRuntime(27888): java.lang.UnsupportedOperationException
03-24 13:47:50.741: E/AndroidRuntime(27888):    at android.view.HardwareCanvas.setBitmap(HardwareCanvas.java:39)

Code which is throwing the exception:

protected void onDraw(Canvas canvas) {
    mResultImage=Bitmap.createBitmap(width,height,mOriginalImage.getConfig());
    canvas.setBitmap(mResultImage);
    canvas.save();

    if(mOriginalImage!=null)
        canvas.drawBitmap(mOriginalImage, width, height, paint);
    else
        canvas.drawText("Image loading...", width/2f-20, height/2, paint);

    canvas.drawText(text, x, y-20, paint);

    canvas.restore();
    super.onDraw(canvas);
}

The android.view.HardwareCanvas isn't even on the reference of android. But I was able to find some information about it. It seems that it's setBitmap(Bitmap) isn't written yet, and that's ok.

My question is why is the onDraw(Canvas) returning a HardwareCanvas class? It isn't even a super class for Canvas.

Bonus question: Any way around this?

1 Answer 1

7

If you want to draw on a Bitmap you should create a new Canvas passing the bitmap to it. You should not be allowed to change the target of the canvas your view should be drawn on. So simply create a new canvas with the bitmap and then draw the resulting bitmap on your canvas in the onDraw method.

Sign up to request clarification or add additional context in comments.

3 Comments

Well this answers my bonus question. But why do we have this method if we're not supposed to use it? Also it isn't deprecated nor does it have any warnings in the reference. Anyway, thanks for the answer.
That method should be totally ok if you try to modify a canvas you created yourself. Try to imagine what you are trying to do there. You have the canvas of the view, which is the canvas your view uses to render itself on the screen, and you are trying to make that canvas draw on a Bitmap instead. That would break all the other views that are supposed to draw on the same canvas. Pretty clear it couldn't work right?
I have also run into the UnsupportedOperationException, after reading the documentation and determining that setBitmap sets internal drawing buffer of the Canvas that you can then work with. It is not so and you have to create an extra Canvas, but there is not sufficient warning in the doc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.