Hybrid instance of Android improved surfaceview and multithreading

The previous article briefly introduced the basic use of surfaceview in Android. This article will introduce the mashup of surfaceview and multithreading. Surfaceview is a multi-threaded application to prevent animation flicker. The multithreading usage of Android is exactly the same as that of Java. This article will not introduce multithreading. Directly explain the mixed use of surfaceview and multithreading, that is, one thread is dedicated to reading pictures and the other thread is dedicated to drawing.

The screenshot of this program is as follows. On the left is to open a single thread to read and draw, and on the right is to open two threads, one for reading pictures and the other for drawing:

It can be seen from the comparison that the frame rate of the animation on the right is significantly faster than that on the left, and thread. Sleep() is not used for both. Why open two threads, one reading and one painting, instead of "reading and painting" as on the left? Because the canvas is locked every time surfaceview draws, that is, the same area cannot be drawn next time before it is finished this time. Therefore, to improve the efficiency of animation playback, one thread must be opened for drawing and another thread for preprocessing.

The source code of main.xml is as follows:

The source code of Java program is as follows:

I hope the examples described in this article can be helpful to you in the hybrid programming of Android surfaceview and multithreading.

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