how to print the content of an android activity on paper using a printer

时间:2015-06-30 13:37:07

标签: android printing screenshot android-print-framework

I have an activity which contains a photo (taken by the camera using start activity for result) and some text in editboxes. I want to print the content of this complete activity on paper. Saw the google docs which say that i will need to convert the content into a pdf document before i print it. It also has the example how to convert text into pdf doc but what about the image ? The print can be even the screen shot of the activity (in larger size than the phone screen though)

Can anyone please guide me to any tutorial example of such a work or a sample code to understand how to do it... thanks for sharing knowledge...!

1 个答案:

答案 0 :(得分:1)

我想你看到了tutorial。 其中有这种方法。

private void drawPage(PdfDocument.Page page) {
    Canvas canvas = page.getCanvas();

    // units are in points (1/72 of an inch)
    int titleBaseLine = 72;
    int leftMargin = 54;

    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(36);
    canvas.drawText("Test Title", leftMargin, titleBaseLine, paint);

    paint.setTextSize(11);
    canvas.drawText("Test paragraph", leftMargin, titleBaseLine + 25,          paint);

    paint.setColor(Color.BLUE);
    canvas.drawRect(100, 100, 172, 172, paint);
}

使用CanvasdrawBitmap方法绘制图片。 首先从ImageView获取位图。

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

然后画出来:

canvas.drawBitmap ( bitmap, null, new Rect(10, 10, 100, 100), null );

另外,请查看此link