Where does Android View.scrollTo(x, y) scroll to?

邮差的信 提交于 2019-11-26 09:25:56

问题


scrollTo(int x, int y) says:

x the x position to scroll to

y the y position to scroll to

onScrollChanged(int l, int t, int oldl, int oldt) says:

l Current horizontal scroll origin.

t Current vertical scroll origin.

What I would like to know and can\'t find anywhere, is where is x,y? Top left? Center? I tried a few tests and can\'t figure it out.


回答1:


After extensive research and testing I've finally understood how scrollTo() works.

(0,0) are the coordinates to the top left corner of the View container. When scrolling to any (x,y) point, the top left corner of the View will be placed at the (x,y) coordinates.

If the View is showing an image, Bitmap, larger than the View itself, scrolling to (0,0) will place the View in the center of the image. This way, the top left corner of the image will be located at (-dX/2, -dY/2) and the bottom right corner at (mW - dX/2, mH - dY/2). dX represents the difference between the widths of the image and the View. And dY represents the difference between the heights of the image and the View.

In order to see the bottom right corner and not go passed it (lower or further to the right), this is the proper call: scrollTo(mW - ivW - dX/2, mH - ivH - dY/2);

The attached image shows the graphical representation of the View and Bitmap image positioning.




回答2:


From getX():

The visual x position of this view, in pixels. This is equivalent to the translationX property plus the current left property.

For getY() it's from top. So (0,0) is top left.

I don't know if scrollTo() takes into account the translationX property, because it's "recent" and I haven't been coding for a while, but I'd bet it does.



来源:https://stackoverflow.com/questions/7773206/where-does-android-view-scrolltox-y-scroll-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!