Java Android PDF(位图)没有全尺寸显示

loading...


0

我正在尝试在我的应用程序中显示PDF文件但是当我显示文件时,它没有以完整尺寸显示,它只覆盖了屏幕的1/4左右 .

这是我的代码:

public void render() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {

        final ImageView pdfView = (ImageView) getActivity().findViewById(R.id.pdfView);
        pdfView.post(new Runnable() {
            @Override
            public void run() {
                try {
                    int width = pdfView.getWidth();
                    int height = pdfView.getHeight();

                    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
                    System.out.printf("Filename: " + pdfFile.getName());
                    PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY));

                    if (currentPage < 0) {
                        currentPage = 0;
                    } else if (currentPage > renderer.getPageCount()) {
                        currentPage = renderer.getPageCount() - 1;
                    }

                    Matrix m = pdfView.getImageMatrix();
                    Rect rect = new Rect(0, 0, width, height);
                    renderer.openPage(currentPage).render(bitmap, rect, m, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
                    pdfView.setImageMatrix(m);
                    pdfView.setImageBitmap(bitmap);
                    pdfView.invalidate();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

当我记录宽度和高度时,我得到结果:1440/1944

loading...

1回答

  • 1

    尝试将其设置为页面宽度和高度 . 使用下面的代码并检查

    PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
                    PdfRenderer.Page page = renderer.openPage(0);
                    int pageWidth = page.getWidth();
                    int pageHeight = page.getHeight();
                    float scale = Math.min((float) REQ_WIDTH / pageWidth, (float) REQ_HEIGHT / pageHeight);
                    Bitmap bitmap = Bitmap.createBitmap((int) (pageWidth * scale), (int) (pageHeight * scale), Bitmap.Config.ARGB_8888);
                    page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
                    imageView.setImageBitmap(bitmap);
    
评论

暂时没有评论!