Summary of three ways of Android view Mobile

preface

In Android development, view has always been a worry for Android developers. On the one hand, they want to advance, but on the other hand, they are afraid to advance. It can be said that Android view is the biggest stumbling block on the way to advance, because it involves too many things. For example, the view mobile we want to write this time also includes the transmission of view touch events and the creation of custom views, These are extremely important and have to face problems. But in any case, the difficulties that are not overcome now will be overcome in the future.

Before that, let's first understand the definition rules of Android coordinate system and some position parameters of view.

Android coordinate system

The position and size of the view are determined by four parameters: left, top, right and bottom, and these four parameters are relative to its parent view.

After the layout in activity is completed, we can obtain these parameter information through view methods:

In addition, x, y, translationx, translationy and other parameters will be added after Android 3.0. (x, y) represents the value of X and Y in the upper left corner of the view in the ViewGroup. Translationx and translationy are used to translate a view. The default is 0, which changes after calling settranslationx() / settranslationy() of view.

PS: calling settranslationx() and settranslationy() methods of view can make the view translate a specified distance, but this process is completed in an instant. In order to make the view move more smoothly, you can use the view's attribute animation to specify translationx and translationy.

In addition, if settranslationx() and settranslationy() are set for the view, if the set value does not change, it will only move once, that is, the first specified moving distance. After checking the source code, we found the reason: after setting the value, it will compare the set value with the current translationx and translationy, and move it only when it is inconsistent.

After understanding some basic parameters of view, let's look at three ways to move view.

1、 Use the scrollto() / scrollby() method provided by the Android system to move the view.

Whether it is scrollto() or scrollby(), the essence of its movement is the content in the view / ViewGroup. And the moving process is completed instantly. Therefore, in order to achieve better moving effect, it needs to be used in combination with the scroller class. In addition, unlike the above translation, it moves the view itself, which needs to be well understood.

Both scrollto() and scrollby() are methods in view, not in scroller, but controlling the smooth movement of view is inseparable from the scroller class.

Scrollto(): refers to the absolute position of the movement. If the position does not change, multiple calls will not work.

Schematic diagram of scrollto movement process

Scrollby(): its essence is still called scrollto(), which refers to the relative distance to move the current position (each time you call scrollto() by adding the current position and the set distance, so that if you call it many times, you will find that it will move a distance each time, which is the essential difference from scrollto())

Schematic diagram of scrollby movement process

PS: as for the above two pictures, in fact, I haven't fully understood what is relatively absolute all the time, so the two hand pictures may be easier to understand. There is also the problem of the moving direction of scrollto() and scrollby(). We have drawn the Android coordinate system above. The x-axis is positive from left to right, and the y-axis is positive from top to bottom. But this does not apply to scrollto and scrollby. Scrollto and scrollby are just the opposite, that is, the x-axis is negative from left to right and the y-axis is negative from top to bottom. It's a bit of a pit father.

Scroller class analysis: why can the contents of view / ViewGroup be moved by using the methods in the scroller class? Let's try to analyze it.

first

We create an object of the scroller class, mscoller.

then

To make the view move to the specified position within the specified time, we will call the startscroll () method. Startscroll () is a method in the scroller class. In addition, there is a filling () method in the scroller class, which is also very common. It mainly deals with smooth movement and generally creates the inertial effect after sliding to make the view move more realistic. Let's look at the source code of startscroll():

In general, after calling this method, we need to call the invalidate () method of view, which can trigger the draw () method of view. In draw (), we call computeScroll (), and we find computeScroll () is an empty method in source code. That's what we need to rewrite computeScroll () method. Because the moving operation is performed in computescroll().

As we saw above, there is also a computescrolloffset () method in the scroller class. What does it do? Its main function is to judge whether mcurrx and mcurry have changed. If yes, it returns true and if not, it returns false. Through the judgment of this method, you can point out whether you need to continuously call scrollto () to move the view. Here is another example. Use scrollto() to make the view move with your finger:

2、 Use animation to move the view.

This includes tween animation / frame animation of view and property animation added after 3.0. It moves an image of the view, and the position and size of the view itself have not changed.

3、 Set the layoutparams of the view to move the view

summary

The above is a summary of all the contents of the three ways of Android view mobile. I hope the contents of this article can be helpful when you develop Android. If you have any questions, you can leave a message for communication.

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
分享
二维码
< <上一篇
下一篇>>