如何将整个视图(水平和垂直滚动)另存为图像或pdf?

时间:2018-08-31 16:46:30

标签: java android

我想知道如何将整个视图(水平和垂直滚动)另存为图像或pdf?有没有办法做...? 我的布局(下面)包含一个表,我想保存! 我已经搜索过谷歌,但我无法弄清楚这样做的方式

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DetailedAttendance"
    android:background="@drawable/animation_back_grad1"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical" >

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:fadeScrollbars="false">

            <TableLayout
                android:id="@+id/detailedAttendanceTable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </HorizontalScrollView>
    </ScrollView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/exitApp1"
            android:layout_weight=".5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/reg_button_back"
            android:text="Exit"
            android:textColor="#F3677C" />
        <Button
            android:id="@+id/back"
            android:layout_weight=".5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/reg_button_back"
            android:text="Back"
            android:textColor="#F3677C" />


    </LinearLayout>

</RelativeLayout>

任何小小的帮助都将受到赞赏! 谢谢:)

1 个答案:

答案 0 :(得分:0)

我不确定这是否是您需要的,但是在这里,您可以将布局另存为pdf文档。 我没有包括将文件保存到外部文件中所需的所有权限部分,而只是针对pdf生成。

这个想法是创建一个布局,对其进行充气,然后将数据设置为活动视图。然后,不将布局绘制到屏幕上,而是将其绘制到Canvas一部分的PdfDocument上。

R.layout.pdf_resultado是要用于pdf的布局(xml)。

private static final int PAGE_WIDTH = 612;
private static final int PAGE_HEIGHT = 792;

public void createPdf(File pdfFile, SomeDataSource dataSourceObject){

    LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View content = inflater.inflate(R.layout.pdf_resultado, null);

    // Get references to views on the layout
    tvResultRef = content.findViewById(R.id.tvResultRef);
    ...

    // Set the data for the views;
    tvResultRef.setText(dataSourceObject.getTheDataForResultRefView());
    ...

    // Make the pdf document
    PdfDocument pdfDocument = new PdfDocument();
    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo
            .Builder(PAGE_WIDTH,PAGE_HEIGHT,1).create();
    PdfDocument.Page page = pdfDocument.startPage(pageInfo);

    Canvas canvas = page.getCanvas();

    // I came up with the next lines to scale the view to fit the page.
    // Perhaps there is a better way of doing it but at the time I wasn't able to find much information
    // on this.

    int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.UNSPECIFIED);
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.UNSPECIFIED);

    content.measure(measureWidth, measuredHeight);
    content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());

    //String msg = String.format("canvas w %d canvas h %d content w %d content h %d",page.getCanvas().getWidth(), page.getCanvas().getHeight(),
    //        content.getMeasuredWidth(), content.getMeasuredHeight());
    //Log.d(TAG,msg);

    float xRatio = ((float) canvas.getWidth()) /( (float) content.getMeasuredWidth());
    float yRatio = ((float) canvas.getHeight()) /( (float) content.getMeasuredHeight());

    float scale;
    if(xRatio < yRatio){
        scale = xRatio;
    }else{
        scale = yRatio;
    }

    canvas.scale(scale,    scale);
    content.draw(canvas);

    pdfDocument.finishPage(page);

    try {
        pdfDocument.writeTo(new FileOutputStream(pdfFile));
        Toast.makeText(context, context.getString(R.string.pdfSavedInDocs), Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, context.getString(R.string.errorGuardandoPDF),
                Toast.LENGTH_LONG).show();
    }

    // close the document
    pdfDocument.close();
}

编辑我

someDataSource对象只是一个携带信息以填充视图的对象。

例如:

public class SomeDataSource{
    private String name;
    private String lastName;
    private String resultRef;

    // Getters & Setters
}

要使用数据设置布局,请实例化此类的对象,在其中填充要显示的信息,然后使用其调用createPdf()方法。

SomeDataSource dataSourceObject= new SomeDataSource();
dataSourceObject.setName("John"); 
dataSourceObject.setLastName("Doe");
dataSourceObject.setResultRef("ResultReference: X123-1");

createPdf(new File("John_Doe_Result.pdf", dataSourceObject); 

tvResultRef对象是布局的视图,例如TextView。

首先像在Activity的{​​{1}}方法中一样,您获得对布局中视图的引用:

onCreate()

然后为它设置一个值以显示:

tvResultRef = content.findViewById(R.id.tvResultRef);

并对布局中的其他视图执行相同操作。