如何确定 ImageView 中图像的实际位置?
我以为这应该是世界上最简单的任务,但结果比我想象的要复杂得多。
我想从 ImageView 派生我自己的 View 类,扩展其 onDraw 方法,以便在 ImageView 本身显示的底层图像上覆盖更多元素。
问题是:图像将被缩放以保留其比例,因此有时图像旁边会有边距 - 我称之为偏移。为了将我自己的元素放置在图像上,我需要知道这些偏移量以及在图像上执行的缩放 - 但我在任何地方都没有得到任何线索。
我以为我只需要调用 ImageView 方法,如“getImageLeft()”、“getImageTop()”等 - 但没有这样的方法!
我在这里找到了一个解决方案,该解决方案涉及我自己的大量计算,我发现这种方法很复杂(而且它不起作用......)。
有人可以给我线索吗?
谢谢!
I thought it should be the easiest task in the world, but it turned out to be much more complicated than I thought.
I want to derive my own View class from an ImageView, extend its onDraw method just to overlay a few more elements on the underlying image displayed by the ImageView itself.
The problem is: the image will be scaled to preserve its ratio, so sometimes next to the image there will be margins - offsets, I call them. To place my own elements upon the image, I need to know these offsets and the scaling performed on the image - but I am not getting a clue anywhere.
I thought I just need to call ImageView methods like "getImageLeft()", "getImageTop()", etc - but there are no such methods!!
I found one solution here that involved a whole lot of calculation on my own, I find that way to complicated (and it did not work...).
Can anybody give me a clue??
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用衬垫。它们确定视图内的偏移量。还有一个属性scaleType,它决定当图像的大小与imageView的大小不匹配时图像应该如何缩放。
如果你想知道图像在 imageView 内部是如何缩放的,我可以说比例是保留的。例如,您有一个 200x50 的图像和 100x100 的 ImageView。那么imageView中图片的大小必须是100x25。缩放后,图像默认放置在 ImageView 的中心。因此,顶部和底部大约有 12 个额外空间。
Use paddings. They determine the offset inside a view. There is also an attribute scaleType which determines how the image should be scaled if its size doesn't match with the size of the imageView.
If you want to know how the image is scaled inside and imageView I can say that the proportions are kept. For example you have an image 200x50 and ImageView 100x100. Then the size of the image in the imageView must be 100x25. After scaling the image is placed in the center of the ImageView by default. So you have about 12 extra space from top and bottom.