Android uses surfaceview to realize the animation effect of rainy weather

The first is the final rendering:

First analyze the implementation of a raindrop:

After analysis, can you write a class directly, inherit view directly, and then override ondraw()? It can be seen that the raindrops in the renderings fall very fast, which means that ondraw() must be called once in every frame to redraw them. If the rendering code in your ondraw() method is a little time-consuming, and the ondraw() method of view is called in the UI thread, the drawn effect will not be so smooth, and even block the UI thread, Therefore, in order to achieve smoother effects and not block UI threads, we use surfaceview here.

Get to know surfaceview

Surfaceview directly inherits from view. View must be drawn in the UI thread. Unlike view, surfaceview can be drawn in non UI threads and displayed on the interface, which means you can open a new thread and put the drawing and rendering code in the thread.

Surface is sorted by Z axis. The Z axis position of surfaceview is smaller than its host window, which means it is always behind its own window. Since it is behind, how is it displayed? Surfaceview makes a "hole" in its window (in fact, a transparent area is set on its host window to enable it to be displayed), which means that its brother node's view will overwrite it. For example, you can place buttons, text and other controls above surfaceview.

To access the following surface, you can use the surfaceholder interface provided to us by Android. You can call getholder () of surfaceview to get.

Surfaceview has a life cycle. We must execute rendering code during its life cycle, so we need to monitor the status of surfaceview (such as creation and destruction). Here, Android provides us with surfaceholder.callback interface to enable us to conveniently monitor the status of surfaceview.

Let's take a look at the surfaceholder. Callback interface

Our drawing code needs to be executed between surfacecreated and surfacetested, otherwise it is invalid. The callback method of surfaceholder.callback is executed in the UI thread, and the drawing thread needs to be manually created by ourselves.

See the official documents for details: https://developer.android.google.cn/reference/android/view/SurfaceView.html

Usage scenarios for view and surfaceview

Using surfaceview (Implementation)

Here, similar to the custom view, we write a class dynamicweatherview that inherits from the surfaceview. Then, in order to monitor the state of the surfaceview, we also need to implement the surfaceholder.callback interface to monitor the state of the surfaceview. The specific callback time of the interface has also been introduced above.

As mentioned above, to control the surface, we need a surfaceholder object, which can be obtained by calling getholder () of surfaceview, and then add a surfaceholder.callback callback for this surfaceholder. Here is the current object of dynamicweatherview

Then implement our drawing thread:

From the above code, we can see that the update process of surfaceview is as follows:

The amount of drawing thread code is not large, because the specific drawing code is in mtype.ondraw (canvas). Mtype is an interface defined by ourselves and represents a weather type:

In order to implement different weather types, we only need to implement this interface and override OnDraw and onsizechanged methods. Here we implement the effect of rain, so we implement a raintypeimpl class:

The code is not difficult. There are basically comments. The rainholder object represents a raindrop. Each time you draw it, change the position of the raindrop, and then prepare for the next drawing to realize the movement of the raindrop.

Basetype class is an abstract base class that implements the dynamicweatherview. Weathertype interface. There are some public methods inside. See the code in demo for details.

Finally, our activity Code:

In the future, if you want to implement different weather types, you only need to inherit the basetype class and rewrite the relevant methods.

Source code download: click here

summary

The above is the whole content of this article. I hope the content of this article can bring some help to Android developers. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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