How to scale Canvas and draw thin border around the edge?

177 Views Asked by At

I have been struggling to get the preview of a PDF in the Android Print Framework to match the print output. However, I am able to get the output to match as a bitmap. However, if I scale the bitmap and put whitespace around the bitmap, the Print Framework crops out the whitespace. So, to prevent that from happening, I would like to draw a thin border around the edge of the entire Canvas.

This is the code that I have to scale the bitmap:

public static Bitmap captureScreen(View v) {

    Bitmap screenshot = null;
    Bitmap output = null;
    try {

        if (v != null) {
            screenshot = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

            int width = screenshot.getWidth();
            int height = screenshot.getHeight();
            int scaledWidth = (int) (width * 0.5);
            int scaledHeight = (int) (height * 0.5);
            output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Bitmap scaled = Bitmap.createScaledBitmap(screenshot, scaledWidth, scaledHeight, false);
            Canvas canvas = new Canvas(output);
            Paint paint = new Paint();
            int distX = (width - scaledWidth) / 2;
            int distY = (height - scaledHeight) / 2;
            canvas.drawBitmap(scaled, distX, distY, paint);

            v.draw(canvas);
        }

    } catch (Exception e) {
        Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage());
    }
    return output;
}

I would like to draw a thin black border around the entire canvas for I can prevent the whitespace from being cropped out by the Print Framework.

1

There are 1 best solutions below

0
On

Well, you have the width and height of the canvas, just call Canvas.drawRect(0, 0, width - 1, height - 1, strokePaint)?