Android scrollto and scrollby methods use parsing

In a view, the system provides two methods, scrollto and scrollby, to change the position of a view. The difference between the two methods is very easy to understand. It is similar to the difference between to and by in English. Scrollto (x, y) identifies moving to a specific coordinate point (x, y), while scrollby (DX, Dy) indicates that the increment of moving is DX and dy.

After obtaining the offset, use scrollby to move the view. The code is as follows:

However, when we drag the view, you will find that the view does not move! Is our method wrong? In fact, the method is correct, and the view does move, but it doesn't move what we want to move. The scrollto and scrollby methods move the content of the view, that is, move the content of the view. If the scrollto and scrollby methods are used in the ViewGroup, all child views will be moved, but if they are used in the view, the content of the view will be moved, such as textview, and the content is its text; ImageView, content is its drawable object.

I believe that through the above analysis, we should know why we can no longer use these two methods to drag the view in the view. Then we should use the scrollby method in all viewgroups of the view to move its child views. The code is as follows:

However, when you drag the view again, you will find that the view is moving, but it is moving randomly, which is not what we want to follow the movement of the touch point. Here you need to know some knowledge about view movement. When you understand this problem, you might as well imagine that the mobile phone screen is a hollow cover plate, under which is a huge canvas, that is, the view we want to display. When the cover plate is covered at a certain place on the canvas, through the rectangle in the middle, we can see the view we want to display on the mobile phone screen, while the views elsewhere on the canvas are covered by the cover plate and cannot be seen. Our view is very similar to this example. If we don't see the view, it doesn't mean it doesn't exist. It may just be outside the screen. When you call the scrollby method, you can imagine that the outer cover plate is moving. This is more abstract. Let's take a concrete example:

As shown in the following figure: the middle rectangle is equivalent to the screen, that is, the visual area. The following content is equivalent to the canvas and represents the view. As you can see, only the middle part of the view is currently visible, and other parts are not visible. In the visible area, we set a button whose coordinates are (20,10).

Next, use the scrollby method to translate the cover plate (screen and visual area) 20 horizontally to the positive direction of X axis (right) and 10 vertically to the positive direction of Y axis (lower).

We can find that although scrollby (20, 10) is set and the offset is a positive number in the positive direction of X axis and Y axis, the button moves in the negative direction of X axis and Y axis in the visual area of the screen. This is the different effects caused by the different selection of reference system.

Through the above analysis, it can be found that if the parameters DX and dy in scrollby are set as certificates, the content will move in the negative direction of coordinates; If the parameters DX and dy in scrollby are set to negative numbers, the content will move in the positive direction of the coordinate axis. Therefore, returning to the previous example, to achieve the effect of sliding with the movement of the finger, you must change the offset to a negative value, and the code is as follows:

Try again, and you can find that the effect is the same as that of the previous methods. Similarly, when absolute coordinates are used, this effect can also be achieved by using the scrollto method.

summary

The above is the use analysis of Android scrollto and scrollby methods introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>