Android uses surfaceview to realize simple timer

I have taught myself Android for several months. With the rhythm of the Internet, I should write some blogs early to improve my level. However, due to the lack of self-confidence in the technical level (which is also the result of not passing the test), they have only written their own problems and important knowledge points in their own notes and documents.

However, I always feel that a person lacks a sense of achievement in writing, and it is possible to get more good feedback and solutions if some questions and solutions are thrown out. Therefore, with the mentality of not doing and not dying, we move forward on the road of technological growth step by step - > this blog is one of the steps!

If some technical knowledge points in the blog are wrong or there are more optimized solutions, I hope you can point out more.

Here's the point:

Objective: to implement a simple timer using surfaceview

Illustration:

Description: 1. Use surfaceview to realize the timing function, and constantly fill the arc at the same time; 2. Click the button to stop timing;

a key:

1. Override the three methods of surfaceholder.callback in user-defined surfaceview;

2. Get the canvas object in the newly opened thread through surfaceholder. Lockcanvas(), so as to draw graphics and time text;

3. Control the execution of logic code in the thread run () method by setting the flag value;

Important part code:

The above is the constructor of customized surfaceview (testview), and do the corresponding initialization. (the second construction method is not overwritten in the implementation process. As a result, if it is introduced into the activity through the layout file, the view - > will not be displayed, that is, the construction method that general custom views must be overwritten). The custom internal thread class countthread is initialized in the constructor to perform drawing.

The following is the execution logic of the run() method of the countthread class:

It is mainly to pause for one second through thread. Sleep (1000), so as to count through endangle.

So far, the basic program logic has been realized. Again, the custom view can be displayed by importing it into the layout file specified by the activity.

However, this will also involve corresponding problems. For example:

1. The buttons in the figure cannot be displayed, only the customized surfaceview can be seen;

My approach is to set the layout to FrameLayout. Because surfaceview is a layer floating in the window, it can be regarded as a layer.

2.java.lang.IllegalThreadStateException:Thread already started

This error indicates that the thread already exists. The recurrence of this error is: pressing the home key or menu key will cause the customized surfaceview to be destroyed, but the thread has not been destroyed. Restart the thread when starting the custom surfaceview again. The solution is:

Judge whether the thread is alive in the surfacecreated () method.

Of course, there is also the most important question:

When you press the home key or menu key, the program is not timing. In this case, one of my solutions is to start a service to receive the value that has been timed when the program stops (onstop ()), and then pass it to the service to record and time it. When the application interface returns to the screen again (onrestart ()), the data will be taken out and returned to the logical run () method of the customized surfaceview to continue timing.

The code of the whole applet can be downloaded through the following link:

Click to enter the download page: http://xiazai.jb51.net/201701/yuanma/AndroidSurfaceView (jb51.net).rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>