The learning journey of surfaceView

Recently, the company has come up with a new requirement. In the requirement, you need to turn the turntable to manually select the target. There are many turntable resources on gitHub, but the requirement is to rotate a picture (similar to the background picture) to select the content above. When you
get this requirement, the first thing you think about is to Define the view and then onTouchEvent to monitor the angle of rotation and then use the rotation animation to rotate, but this effect is not as good as imagined in development. When MotionEvent.ACTION_MOVE: is listening, it feels like one step at a time. On the one hand, setting the time used for rotation causes On the other hand, if the finger keeps moving on the screen, the animation will be called all the time. The previous animation has not ended and the next animation will cause more memory consumption, so it is not the best solution.
Since the custom view effect is not very good Ok, then try surfaceView, this is the view of programmer Qingnai, he will not say much about its benefits, open a thread to draw pictures, the effect is very smooth, but the problem is, using surfaceView will encounter loading The view will have a short black screen effect. Anyone who has used it should know it. The reason is that it will be unloaded after loading the surfaceView to reduce the memory overhead. At this time, it is also clear that the color of the canvas is black by default (this is also usually we See the reason for the black.) The solution is to draw a background in the surfaceCreated method (you can use a boolean to control the number of paintings). After this problem is solved, another problem is encountered. When the host activity of the surfaceview loses focus and then gains the focus, the surfaceView disappears again, but the background of the surfaceCreated painting is still there, and the only thing that disappears is the disappearance of the sub-thread painting. Suddenly found that when the host life cycle changes, the corresponding view will also change (suddenly think of the bar chart made last time, the activity loses focus, and the next time it gets the focus, the number of the highest point of the bar chart is gone, the reason is that The custom view executes the onDraw method again, but the previous global variable has not changed, so the maximum value is gone. It is much more convenient to understand the problem. The solution to this problem is to notify the sub-thread to draw graphics in surfaceCreated, which is solved
The drawing of the graphics is solved, now it is to rotate and monitor different positions.
Rotation : just use the Matrix.postRotate method, only to get the rotation angle and rotation center, the rotation center is based on the demand, and the angle is obtained in the MotionEvent.ACTION_MOVE method. The coordinates of the sliding on the screen and then use the function provided by Math (you will be surprised to find that the Math function is not written in java but calls the native method, as for why it is not written in java, it is not clear) to obtain the rotation angle
Position monitoring: this simple , record the initial position, and the rotation angle, add them up (but there are positives and negatives, haha)
to complete the demand, but they feel that it is better for the turntable to stay in a certain position after each rotation (you have to change the demand when you change , this is the hard-pressed programmer), this requirement is actually very easy to achieve, that is, every time the rotation angle is processed, it is judged whether the rotation angle is within the specified range, if it is not processed, if not, do some Corresponding adjustment, this processing should be processed at the end of the event, that is, under MotionEvent.ACTION_UP.
Project source code Dome address: https://github.com/suhuMM/DianDianDome.git

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326000772&siteId=291194637