代码之家  ›  专栏  ›  技术社区  ›  Eyad Mufti

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

  •  0
  • Eyad Mufti  · 技术社区  · 7 年前

    我的问题是,我的代码可以从LISTVIEW打印一部分,而不能显示其余内容。这是线性布局中的一个LITSVIEW。当我在XML中给LINEARLAYOUT一个大数字时,我可以看到完整的内容。 否则无法显示全部内容。printmanager和打印适配器工作正常,我想我的问题是如何设置画布的大小 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);
    

    }

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

    }

    1 回复  |  直到 7 年前
        1
  •  0
  •   Eyad Mufti    7 年前

    我成功地将所有LISTVIEW放在一个PDF文件中,只有一个页面。 只需将布局的高度设置为LISTVIEW适配器的高度即可。 我希望我能在收据打印机上打印出来!

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