Search code examples
androidcanvasbitmapdrawingcustom-draw

Android Canvas not drawing more than screen dimensions


My App have a feature that let's the user capture photo and add drawing on that photo.

All the photo's are re-sized to exactly 900 x 900. To allow the user add drawing to the image. I keep a transparent image over the original image and do the drawing on the transparent image. Drawing is done using canvas.

But when drawing in a device that has 720 x 480 (height x width). If i create a 900 x 900 transparent image and draw a line from 0,0 to 900,900, canvas only draws line from 0,0 to 480,480.

Below is the respective portion of the code:

Preparing Canvas:

holder = getHolder();
if (holder.getSurface().isValid()) {

    Canvas canvas = holder.lockCanvas();
    canvas.drawColor(Color.TRANSPARENT,  PorterDuff.Mode.CLEAR);
    /* original image is 900 x 900 */
    overlayBitmap = Bitmap.createBitmap(originalImage.getWidth(), originalImage.getHeight(), originalImage.getConfig());
    canvas.setBitmap(overlayBitmap);
}

Drawing Line:

canvas.drawLine(0, 0, 900, 900, paint);

I have no idea why i am having this issue. It is because of using canvas?? Is there any work around? Any help is highly appreciated :-)


Solution

  • After some more reading about canvas and also help from this post i was able to fix the issue.

    The issue was in the canvas clip rectangle. It was (0,0,480,480) by default as device display was 720 x 480 i guess? So whatever was on the bitmap was always clipped down to 480 x 480.

    Later i modified my code like this:

    holder = getHolder();
    if (holder.getSurface().isValid()) {
    
        Canvas canvas = holder.lockCanvas();
        canvas.drawColor(Color.TRANSPARENT,  PorterDuff.Mode.CLEAR);
        /* original image is 900 x 900 */
        overlayBitmap = Bitmap.createBitmap(originalImage.getWidth(), originalImage.getHeight(), originalImage.getConfig());
        canvas.setBitmap(overlayBitmap);
    
        /* set extended clip rectangle for the larger target bitmap */
        Rect clipRect = canvas.getClipBounds();
        clipRect.set(0, 0, image.getWidth(), image.getHeight());
        canvas.clipRect(clipRect, Region.Op.REPLACE);
    
    }
    

    After replacing clip rectangle size with image size everything worked just fine.