0

我一直在努力在 Android Print Framework 中预览 PDF 以匹配打印输出。但是,我能够使输出匹配为位图。但是,如果我缩放位图并在位图周围放置空白,打印框架会裁剪掉空白。所以,为了防止这种情况发生,我想在整个 Canvas 的边缘画一个细边框。

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

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

我想在整个画布周围画一个细黑色边框,因为我可以防止打印框架裁剪掉空白。

4

1 回答 1

0

好吧,你有画布的宽度和高度,只需调用Canvas.drawRect(0, 0, width - 1, height - 1, strokePaint)?

于 2018-05-16T18:36:00.407 回答