如何缩放画布并在边缘周围绘制细边框?

时间:2018-05-16 18:13:58

标签: android android-canvas android-bitmap android-print-framework

我一直在努力在Android Print Framework中预览PDF以匹配打印输出。但是,我能够将输出作为位图进行匹配。但是,如果我缩放位图并在位图周围放置空格,则Print Framework会裁剪出空白区域。因此,为了防止这种情况发生,我想在整个画布的边缘画一个细边框。

这是我必须缩放位图的代码:

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

我想在整个画布周围画一个薄的黑色边框,以防止打印框架裁掉空白。

1 个答案:

答案 0 :(得分:0)

嗯,你有画布的宽度和高度,只需拨打Canvas.drawRect(0, 0, width - 1, height - 1, strokePaint)