[英]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)我生成名为view
的TableLayout
然后保存到 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.