如何打印完整列表视图 (android)

how to print a full listview (android)

我的问题是我的代码可以打印 LISTVIEW 的一部分,而无法显示其余内容。它是 LINEARLAYOUT 内的 LITSVIEW。当我在 XML 中给 LINEARLAYOUT 一个大数字时,我就可以看到全部内容。 否则无法显示全部内容。打印管理器和打印适配器工作正常,我想我的问题是如何设置 canvas PLZ SEE THIS IMAGE

的大小

这是一个非常有用的代码,相信很多人会觉得它很有帮助。 代码是

//连接到打印管理器实例

public void printPDF(View view) {

    PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
    printManager.print("print_any_view_job_name", new ViewPrintAdapter(this,
    findViewById(R.id.nameOfLayout)), null);

}

public class ViewPrintAdapter 扩展了 PrintDocumentAdapter {

private PrintedPdfDocument mDocument;
private Context mContext;
private View mView;

public ViewPrintAdapter(Context context, View view) {
    mContext = context;
    mView = view;
}

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

    mDocument = new PrintedPdfDocument(mContext, newAttributes);

    if (cancellationSignal.isCanceled()) {
        callback.onLayoutCancelled();
        return;
    }

    PrintDocumentInfo.Builder builder = new PrintDocumentInfo
            .Builder("print_output.pdf")
            .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
            .setPageCount(1);

    PrintDocumentInfo info = builder.build();
    callback.onLayoutFinished(info, true);
}

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

    // Start the page
    PdfDocument.Page page = mDocument.startPage(0);
    // Create a bitmap and put it a canvas for the view to draw to. Make it the size of the view
    Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight (),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    mView.draw(canvas);
    // create a Rect with the view's dimensions.
    Rect src = new Rect(0, 0, mView.getWidth(), mView.getHeight ());
    // get the page canvas and measure it.
    Canvas pageCanvas = page.getCanvas();
    float pageWidth = pageCanvas.getWidth();
    float pageHeight = pageCanvas.getHeight();
    // how can we fit the Rect src onto this page while maintaining aspect ratio?
    float scale = Math.min(pageWidth/src.width(), pageHeight/src.height());
    float left = pageWidth / 2 - src.width() * scale / 2;
    float top = pageHeight / 2 - src.height() * scale / 2;
    float right = pageWidth / 2 + src.width() * scale / 2;
    float bottom = pageHeight / 2 + src.height() * scale / 2;
    RectF dst = new RectF(left, top, right, bottom);

    pageCanvas.drawBitmap(bitmap, src, dst, null);
    mDocument.finishPage(page);

    try {
        mDocument.writeTo(new FileOutputStream(
                destination.getFileDescriptor()));
    } catch (IOException e) {
        callback.onWriteFailed(e.toString());
        return;
    } finally {
        mDocument.close();
        mDocument = null;
    }
    callback.onWriteFinished(new PageRange[]{new PageRange(0, 0)});
}

}

我成功地将所有 LISTVIEW 放入一个单页 PDF 文件中。 只需将 LAYOUT 的高度设置为等于 LISTVIEW ADAPTER 的高度。 我希望我可以在收据打印机上打印它!

// Gets linearlayout
        LinearLayout layout = (LinearLayout) findViewById(R.id.relativeLayout);
// Gets the layout params that will allow you to resize the layout
        ViewGroup.LayoutParams params1 = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*

  layout.setLayoutParams(params);