使用PixelCopy在DialogFragment中复制缩放视图 [英] Using PixelCopy to copy a scaled View within a DialogFragment
问题描述
我有一个 DialogFragment
,它使用 onCreateDialog
中的自定义视图创建了一个 AlertDialog
.自定义视图包括旋转进度条和提示,以及通过 scaleX
和 scaleY
值适合自定义内容的大视图(大于屏幕尺寸).
I have a DialogFragment
that creates an AlertDialog
with a custom view in onCreateDialog
. The custom view includes a spinning progress bar and a prompt as well as a large view (larger than screen dimensions) that is fit inside the custom content via scaleX
and scaleY
values.
我正在使用 PixelCopy
API将大视图仅复制到位图中.效果很好,但有一个令人讨厌的警告:
I am using the PixelCopy
API to copy only the large view into a bitmap. This works well, but with a rather annoying caveat:
我这样称呼 PixelCopy
:
val winloc = intArrayOf(0, 0)
view.getLocationOnScreen(winloc)
val offset = 0
val left = winloc[0] + offset
val top = winloc[1] + offset
val rect = Rect(left, top, left + view.measuredWidth, top + view.measuredHeight)
PixelCopy.request(getDialog().getWindow(), rect, bitmap, listener, view.handler)
view.getLocationOnScreen(winloc)
返回(84,84)
当我检查生成的位图时, PixelCopy
捕获了很多帧,填充,阴影和实际视图内容(右下角有些缺失).实际内容中缺少的部分与我在图像左上角得到的帧,填充和阴影的数量完全相同.
When I check the generated bitmap, PixelCopy
has captured a whole lot of frame, padding, shadows, and the actual view content (with a bit missing from bottom right). The part of the actual content that is missing is exactly the same amount as the frame, padding, and shadow that I get at the top left of the image.
尝试了我可以想到的任何其他方法来获取要保存的内容的正确界限,我开始向坐标添加随机值( offset
值以上).在 Nexus 6P 上, 112
的偏移量是完美的.现在,我不能无缘无故地在其中扔 112
,因为1.这是一个魔术数字,而2.它只能在一个设备上使用.
Having tried anything else I could think of to get the correct bounds of this content I want to save, I started adding random values to the coordinates (the offset
value above). On a Nexus 6P an offset of 112
was perfect. Now I can't just throw 112
in there without reason because 1. it's a magic number and 2. it only works on one device.
关于如何获取该视图的正确边界,或者在哪里可以找到此 112
值,以便我可以正确地抵消自己的财产,我已经没有想法了.
I have ran out of ideas as to how I can get the correct bounds for this view OR where I can find this 112
value so that I can properly offset what I have.
由于该应用尚未发布,因此我无法提供实际的屏幕截图,但是这里有一些经过编辑的屏幕截图(忽略黑条).
As the app is unreleased, I can't include actual screenshots, but here are some redacted screenshots (ignore the black bars).
有关屏幕截图的一些注意事项:
Some notes about the screenshots:
- 绿色部分来自
dialog !!.window !!.setBackgroundDrawable(ColorDrawable(0xff00ff00.toInt()))
,所以我可以勾勒出窗口边界.屏幕边缘和绿色矩形之间的透明/阴影位...这是边距还是填充? - 带有红色(内容1)和蓝色(内容2)矩形的白色矩形是前往位图的部分
- 内容中的红色和蓝色矩形是根据相同数据生成的不同视图.
- 蓝色部分包含一些
SurfaceView
,我怀疑我必须分别提取.
- The green part comes from
dialog!!.window!!.setBackgroundDrawable(ColorDrawable(0xff00ff00.toInt()))
so I could outline the window bounds. The transparent/shadow bit between the screen edges and the green rectangle ... is this a margin or padding? - The white rectangle with the red (content 1) and the blue (content 2) rectangles is the portion going to the bitmap
- The red and the blue rectangles within the content are different views generated from the same data.
- The blue portion contains a few
SurfaceView
s that I suspect I'd have to extract separately.
设备屏幕截图
捕获的位图(没有 112
幻数偏移量)
Captured Bitmap (without the 112
magic number offset)
推荐答案
到目前为止,解决方案是抛弃 DialogFragment
并使用常规的 Fragment
.检查布局后,有两个 56
像素填充,没有任何访问器来获取其值.我怀疑在私有装饰视图中某处存在一个错误,该错误在计算窗口中视图的位置时并未考虑这些填充.我会将此标记为答案,直到出现更好的答案为止.
The solution so far has been to ditch the DialogFragment
and use a regular Fragment
. After inspecting the layouts, there were two paddings of 56
pixels, without any accessors to get their values. I suspect there's a bug somewhere in the private decor views that doesn't account for these paddings when calculating the location of the view in window.
I'll mark this as the answer until a better answer comes along.
这篇关于使用PixelCopy在DialogFragment中复制缩放视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!