Surfaceview Controls in Android

Source: Internet
Author: User

Surfaceview is inherited from the View class, has the function of drawing the dynamic picture, playing the video. View is responsible for updating the animation in the main thread, while Surfaceview is updating the animation in a new one, and it is a 2D canvas effect. Put yourself on the lowest level of the screen to display the picture,

How to use Surfaceview:

1. Span style= "FONT-SIZE:19PX; Font-family: Song Body "" When creating a surfaceview Project

The project needs to inherit the Surfaceview and interface to the surfaceholder Callback and Runnable which surfaceholder is used to monitor the canvas,Runnable is used to open a new process, Draw the picture in the process.

Public classGameviewokextendsSurfaceviewImplementsCallback, Runnable

when inheritance and interfaces are complete, there are three constructors and an overloaded function, respectively:surfacecreated,surfacechanged,surfacedestroyedas wellGameviewok(example), three of which Constructionfunctionrespectively indicateSurfaceview in the creation, alteration and destruction of the relevant processing to be done, the general drawing is placed in thesurfacechangedin the. Using code in overloaded functions

Surfaceholder  =  This . Getholder ();

Surfaceholder . Addcallback (this); //Registration callback method

To set Surfaceholer and add callback methods, do not add this callback will report null pointer exception,surfaceholder equivalent to the remote control Surfaceview.

Surfaceview is mainly used to draw pictures, through the thread to achieve the animation effect of the picture, in the process of drawing a picture, it takes three steps:

(1) get the canvas and lock the canvas

Canves = Surfaceholder.lockcanvas()

(2) then do the painting in the canvas

(3) when you are finished drawing, use the Surfaceholder.unlockcanvas method to unlock the canvas for subsequent operations.

2. mianactivity add surfaceview

also first set up a java surfaceview surfaceholder.callback,runnable mainactivity setcontext

Setcontentview (Gameviewok);

3. Adding controls on Surfaceview

When you need to add a control on Surfaceview, simply add the class that inherits from Surfaceview to the layout file as a control.

<controltool.surfaceview

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"/>

This allows you to add additional controls on the Surfaceview.

Surfaceview Controls in Android

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.