Surfaceview plays an important role in game development. It has a greater degree of freedom for Screen Control (unlike view update with handler, about view ), however, there are not many references in this regard. The examples that can be found are a bit overwhelming and cannot be clearly displayed. The following is a simple example, strive to clearly display the process, while others can be simplified.
Program effect: Draw a blue rectangle with a thread.Java code {
DP. Sh. toolbar. copytoclipb
changes.
Surface is ordered in depth (Z-ordered), which indicates that it is always behind its own window. Surfaceview provides a visible area where only the surface content in the visible area is visible and the area outside the visible area is invisible. The layout display of the surface is affected by the hierarchical view relationship, and its sibling view node is displayed at the top. This means that the content of the surface is blocked by its sibling view, which can be used to place over
drawing, and this surface contains a canvas.With canvas, how do we get it?Surfaceview There is a Getholder method, we can get a surfaceholder. With Surfaceholder, you can listen to the Surfaceview life cycle and get the canvas object.2, the general wordingIn summary, the general Surfaceview class we will write this code:public class Surfaceviewtemplate extends Surfaceview implements Callback, Runnable{priv
object.
3) Call the Camera. Parameters object method to set the Camera Parameters.
4) Call the startPreview () method of Camera to start previewing the scene. before previewing the scene, call the setPreviewDisplay (SurfaceHolder holder) method of Camera to set which Surface View is used to display the photo.
5) Call the takePicture () method of Camera to take a photo.
6) when the program ends, call the stopPreview () method of Camera to end the scen
image to SurfaceView and automatically focuses it at intervals. After the focus is successful, the obtained image is displayed in the ImageView control and parsed. If the resolution is successful, the result is displayed in a dialog box. In the constructor of this class, first obtain the activity, imageView, and SurfaceView controls of the main window, and then set some other parameters.
This class implements the SurfaceHolder. Callback interface. Th
class for Camera.
void Setparameters (camera.parameters params): writes Camera parameters to Camera by params.
final void setdisplayorientation (int degrees): the degree of rotation of the camera preview.
final void Setpreviewdisplay (Surfaceholder holder): Sets Surfaceholder for camera preview.
final void Starpreview (): Begins a preview of camera.
final void Stoppreview (): Stop
drawn in the main thread of the application. Because the main thread of the application needs to respond to user input in a timely manner, in addition to drawing the UI, the system will assume that the application is unresponsive, so a anr dialog box pops up. For some game screens, or for camera previews and video playback, their UIs are complex and require efficient drawing, so their UI is not suitable for drawing in the main thread of the application. This is when you have to generate a separ
In fact, each view has a canvas can be used to draw animation, only need to overload the OnDraw () method in this view can be, but the Surfaceview class is a dedicated to brake animation class.Canvas (called "canvas" in Chinese) is the same as the canvas label in HTML5, which allows you to freely draw graphics within a certain area. Canvas+surfaceview animation is more suitable for a large number of centrally played animations than the View animation and property animation animations, such as ga
The steps for calling camera preview in Android are as follows:
1. surfaceview Problems
//Define objectPrivateSurfaceview photoview;//Surfaceview object: (view component) Video DisplayPrivateSurfaceholder photoholder;//Surfaceholder object: (abstract interface) surfaceview support ClassPrivateCamera camera;//Camera object, camera Preview
//Surfaceview object settingsPhotoholder = photoview. getholder ();//Bind surfaceview to obtain the
manage a SurfaceView and execute the painting action to the Canvas as quickly as possible (you do not need to execute invalidate ()).
Painting with CanvasWhen you are writing an application where you want to perform special painting and/or control graphic animation, you should use Canvas to paint. one is actually only an intermediate layer, but an interface, which represents the actual drawing to the surface-it bears all the painting calls. using Canvas, your rendering is actually executed to a
Surface, so SurfaceView is embedded with its own Surface. We can think that SurfaceView is used to control the position and size of the Surface. As we all know, updates of traditional views and Their Derived classes can only be performed on the UI thread. However, the UI thread also processes other interaction logic, which cannot guarantee the View update speed and frame rate, surfaceView can be drawn using independent threads, so it can provide a higher frame rate. For example, SurfaceView is
drawing operations of a view on the Surface, such as drawing a point or draw a line.
Top layer.(2) ImplementationFirst, we inherit SurfaceView and implement the SurfaceHolder. Callback interface.Reason for using the interface: Because SurfaceView has a principle, all the drawing work must start after the Surface is created (the Surface can be used as a ing of the display to write the content to the Surface, can be directly copied to the video memory
the operation of view on surface.When you use Surfaceview to develop a game, it is important to note that when you use it to draw, it usually appears at the top level, and it needs to be created, destroyed, and monitored when the situation changes. This requires the implementation of the Surfaceholder.callback interface, and if you want to crop the drawn canvas, control its size by Surfaceholder to complete the processing. In the program, the
logon process ipname = iptext. gettext (). tostring (). trim (); bundle DATA = new bundle (); data. putstring ("ipname", ipname); intent = new intent (getip. this, cameratest. class); intent. putextras (data); startactivity (intent) ;}}); // set a "cancel" button builder for the dialog box. setnegativebutton ("cancel", new onclicklistener () {@ override public void onclic K (dialoginterface dialog, int which) {// cancel logon and do not do anything. System. Exit (1) ;}}); // create and display
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.