有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在安卓中使用打印文档适配器打印版面

我想预览打印机的布局并打印该布局。我已经找了很多,但没有找到任何方法。我不想打印很多页的文档,我只想打印一页一个版面。 布局包含二维码的文本信息、一个列表视图和一个图像视图

我尝试了以下代码:

 private void doPrint() {


        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) getApplicationContext()
                .getSystemService(Context.PRINT_SERVICE);

        // Set job name, which will be displayed in the print queue
        String jobName = getApplicationContext().getString(R.string.app_name) + " Document";

        // Start a print job, passing in a PrintDocumentAdapter implementation
        // to handle the generation of a print document
        printManager.print(jobName, new MyPrintDocumentAdapter(getApplicationContext()),
                null);


    }

这是我的适配器类

package com.example.haier.arksolsfollow;

import 安卓.content.Context;
import 安卓.os.Bundle;
import 安卓.os.CancellationSignal;
import 安卓.os.ParcelFileDescriptor;
import 安卓.print.PageRange;
import 安卓.print.PrintAttributes;
import 安卓.print.PrintDocumentAdapter;
import 安卓.widget.Toast;

class MyPrintDocumentAdapter extends PrintDocumentAdapter {
    public MyPrintDocumentAdapter(Context applicationContext) {
    }

    @Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
        computePageCount(oldAttributes);
    }

    @Override
    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {

    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onFinish() {
        super.onFinish();
    }

    private int computePageCount(PrintAttributes printAttributes) {
        int itemsPerPage = 4; // default item count for portrait mode
         // default item count for portrait mode

        PrintAttributes.MediaSize pageSize = printAttributes.getMediaSize();
        if (!pageSize.isPortrait()) {
            // Six items per page in landscape orientation
            itemsPerPage = 6;
        }

        // Determine number of print items
        int printItemCount = 1;

        return (int) Math.ceil(printItemCount / itemsPerPage);
    }
}

我的应用程序在这方面也会崩溃

printManager.print(jobName, new MyPrintDocumentAdapter(getApplicationContext()),
                null);

请指导我如何使用适配器中的所有回调方法以及如何预览打印机的布局


共 (1) 个答案

  1. # 1 楼答案

    如果我理解正确,您只需将当前布局打印出来即可。假设我能为你提供一个替代方案。您可以做的是首先生成要打印的布局的位图,然后将其传递给Printer Helper。您甚至可以在打印前预览位图,方法是将其设置为ImageView

    您可以使用下面的代码将布局视图直接转换为位图

    View v = THE_LAYOUT_TO_BE_PRINTED;
    Bitmap bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmp);
    v.draw(c);
    

    然后可以使用Print Helper打印位图

    PrintHelper photoPrinter = new PrintHelper(getActivity());
    photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
    photoPrinter.printBitmap("layout.png", bmp);
    

    以下信息与Android Developer Documentation有关

    After the printBitmap() method is called, no further action from your application is required. The Android print user interface appears, allowing the user to select a printer and printing options. The user can then print the image or cancel the action. If the user chooses to print the image, a print job is created and a printing notification appears in the system bar.

    编辑: 上述方法可用于打印简单图像,而无需使用PrintDocumentAdapter本身。但是,如果有人仍然喜欢像原始问题中那样使用定制的PrintDocumentAdapter,那么在Github中构建良好的article将给出一个清晰的想法