Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top