简体   繁体   English

如何在不首先渲染它的情况下将 android 视图渲染到画布(即没有完整布局)

[英]How to render an android view to canvas WITHOUT rendering it first (i.e. without full layout)

I am trying to draw a view onto an external canvas using View.draw( Canvas canvas ) .我正在尝试使用View.draw( Canvas canvas )在外部画布上绘制视图。 For this to work, the view has to have performed a full layout beforehand.为此,视图必须事先执行完整的布局。

However, my reason for calling View.draw( Canvas canvas ) is so that I can generate a PdfDocument from the view, while leveraging existing widgets instead of custom-drawing everything in my code.但是,我调用View.draw( Canvas canvas )是我可以从视图生成 PdfDocument,同时利用现有的小部件而不是在我的代码中自定义绘制所有内容。 In other words, invoke a full layout of the view (and it's children) in memory rather than on screen... if that makes any sense.换句话说,在内存中而不是在屏幕上调用视图(及其子视图)的完整布局……如果这有意义的话。

I'm hoping to create a view (without rendering it) with fixed height and width, then dumping the view to the PdfDocument page's canvas.我希望创建一个具有固定高度和宽度的视图(不渲染它),然后将视图转储到 PdfDocument 页面的画布。

Can this be done and if so, what's the trick?这可以做到吗?如果可以,有什么诀窍?

If not, is there a better way?如果没有,有没有更好的方法?

Compared to drawing to screen you need to set Layout Parameters, measure and layout as @CommonsWare says.与绘制到屏幕相比,您需要像@CommonsWare 所说的那样设置布局参数、测量和布局。

In my App I draw a view to save to PNG and to print but you could at the Bitmap to a PDF document as well.在我的应用程序中,我绘制了一个视图以保存为 PNG 并进行打印,但您也可以将位图转换为 PDF 文档。

Note this technique does not seem to work well with things that scroll because you get only usually only get what the would be shown on screen.请注意,此技术似乎不适用于滚动的内容,因为您通常只能获得屏幕上显示的内容。

In my case I have a large TableLayout inside the scrollview, so I draw the TableLayout to the bitmap not the scrollView.在我的情况下,我在滚动视图中有一个很大的 TableLayout,所以我将 TableLayout 绘制到位图而不是滚动视图。

I generate the TableLayout called view then to save to PNG I do the following (this could easily be adapted for a pdf dcoument)我生成名为viewTableLayout然后保存到 PNG 我执行以下操作(这可以很容易地适用于 pdf 文件)

Extract of the code I use (Because I use the same code to actually draw to screen as well once it has been laid out I find by Id exactly what I want to save to PNG)我使用的代码的摘录(因为我使用相同的代码实际绘制到屏幕上,一旦布局好,我通过 Id 找到我想要保存到 PNG 的内容)


int tableLayoutId = 1;
float scaleFactor = 0.5f;

TableLayout tableLayout = new TableLayout(DetailedScoresActivity.this);
        tableLayout.setId(tableLayoutId);

// More code to fill out the TableLayout

// .....

// Then Need to full draw this view as it has not been shown in the gui
            tableLayout.setLayoutParams(new TableLayout.LayoutParams(TabLayout.LayoutParams.WRAP_CONTENT,
                    TabLayout.LayoutParams.WRAP_CONTENT));
            tableLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            tableLayout.layout(0, 0, tableLayout.getMeasuredWidth(), tableLayout.getMeasuredHeight());

            Canvas bitmapCanvas = new Canvas();
            Bitmap bitmap = Bitmap.createBitmap(Math.round(tableLayout.getWidth() * scaleFactor), Math.round(tableLayout.getHeight() * scaleFactor), Bitmap.Config.ARGB_8888);

            bitmapCanvas.setBitmap(bitmap);
            bitmapCanvas.scale(scaleFactor, scaleFactor);
            tableLayout.draw(bitmapCanvas);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android以编程方式包含布局(即没有XML) - Android programmatically include layout (i.e. without XML) 在平板电脑上自动更新 Android App(即没有市场!) - Automatically update Android App on Tablet (i.e. without Market!) 使用MyBatis,如何在不编写配置文件的情况下(即以编程方式)构建SqlSessionFactory? - With MyBatis, how can I build an SqlSessionFactory without writing a configuration file (i.e. programmatically)? 如何舍入浮点数但保持浮点数(即不将其转换为int)? - How do I round a float but keep it a float (i.e. without converting it to int)? 如何在没有多重继承的情况下避免代码重复(即在 Java 中)? - How do I avoid code duplication without multiple inheritance (i.e. in java)? 如何在不知道属性类型的情况下设置属性(即通过BeanUtils) - How to set a Property (i.e. through BeanUtils) without knowing it's type 如何在运行时定义JUnit测试超时(即没有注释)? - How to define JUnit test time-out at runtime (i.e. without annotations)? 如何在 Spring MVC 中捕获所有未处理的异常(即没有现有的 @ExceptionHandler)? - How to catch all unhandled exceptions (i.e. without existing @ExceptionHandler) in Spring MVC? 如何直接从另一个类访问变量(即不使用类名或对象)? - How to access variable from another class directly(i.e. without using class name or object)? 泽西岛1.x和Tomcat:如何以编程方式注册ContainerRequestFilter,即没有web.xml? - Jersey 1.x and Tomcat: how to register a ContainerRequestFilter programmatically, i.e. without web.xml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM