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

android-layout listview pdf printing adapter

468 观看

1回复

8 作者的声誉

我的问题是我的代码可以打印LISTVIEW中的一部分,并且无法显示其余的内容。它是LINEARLAYOUT中的LITSVIEW。当我在XML中给LINEARLAYOUT一个大数字时,我可以看到完整的内容。否则它无法显示完整的内容。printmanager和打印适配器工作正常,我认为我的问题是如何设置画布的大小PLZ看到这个图像

这是一个非常有用的代码,我相信很多人都会发现它非常有用。代码是

//连接到Print Manager实例

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

}

公共类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)});
}

}

作者: Eyad Mufti 的来源 发布者: 2018 年 1 月 10 日

回应 1


0

8 作者的声誉

决定

我成功将所有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);
作者: Eyad Mufti 发布者: 2018 年 1 月 10 日
32x32