Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to carry on the example Analysis of SurfaceView

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to carry on the example analysis of SurfaceView, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Introduction to SurfaceView

SurfaceView itself is a View, in line with all the characteristics of View, need to be drawn through the Canvas canvas. The interface can be updated in another thread without hindering its interaction.

SurfaceView is efficient in drawing and is used for programs with frequent interface updates, such as camera preview. The animation effects of some games are generally realized by SurfaceView.

Realize the effect

What we want to achieve this time is that what we say can be directly displayed in the pop-up voice box.

Code demonstration

The implementation of speech reality mainly depends on the introduction in the article "Android integrates iFLYTEK SDK to achieve speech recognition". Our code this time mainly looks at SurfaceView.

(1) create a SurfaceView

Create a custom SurfaceView inherited from SurfaceView and implement two interfaces: the SurfaceHolder.Callback code is as follows:

By implementing this interface, you need to implement the method of the interface in the custom SurfaceView. For the SurfaceHolder.Callback method, you need to implement the following methods, which is actually the lifecycle of SurfaceView:

The DrawText above is our core method, which will be introduced later.

(2) initialize SurfaceView

In the construction method of custom SurfaceView, it is necessary to initialize SurfaceView, including initialization of SurfaceHolder, such as supporting transparency, to prevent it from being obscured by other controls. The code is as follows:

(3) use SurfaceView

Through the lockCanvans () method of the SurfaceHolder object, we can get the current Canvas drawing object. The next action is the same as the drawing operation in the custom View. It is important to note that the Canvas object obtained here continues the last Canvas object, not a new object. Therefore, previous drawing operations are preserved, and if you need to erase, you can clear the screen by defining a Paint and then the drawPaint () method before drawing.

Core method DrawText

As you can see in the above paragraph, we first get the canvas through the lockCanvans () of the SurfaceHolder object, and then use a Paint to clear the screen to prevent the last character overlap, and then use TextPaint to draw the text.

At this point, our SurfaceView has been defined.

Call SurfaceView

Let's build a new IattextActivity.

Then add our TextSurfaceview and an ImageView to the corresponding layout file

The effect is as follows:

Call SurfaceView

The code for the entire Activity

This is the end of the sample analysis on how to carry out SurfaceView. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report