English | Site Directory

Android - An Open Handset Alliance Project

android.view
public interface

android.view.SurfaceHolder

android.view.SurfaceHolder

Abstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface. This interface is typically available through the SurfaceView class.

When using this interface from a thread different than the one running its SurfaceView, you will want to carefully read the lockCanvas() and Callback.surfaceCreated.

Nested Classes
SurfaceHolder.Callback A client may implement this interface to receive information about changes to the surface. 

Summary

Public Methods

        void  addCallback(Callback callback)
Add a Callback interface for this holder.
        Surface  getSurface()
Direct access to the surface object.
        Rect  getSurfaceFrame()
Retrieve the current size of the surface.
        Canvas  lockCanvas(Rect dirty)
Just like lockCanvas() but allows to specify a dirty rectangle.
        Canvas  lockCanvas()
Start editing the pixels in the surface.
        void  removeCallback(Callback callback)
Removes a previously added Callback interface from this holder.
        void  setFixedSize(int width, int height)
Make the surface a fixed size.
        void  setFormat(int format)
Set the desired PixelFormat of the surface.
        void  setSizeFromLayout()
Allow the surface to resized based on layout of its container (this is the default).
        void  unlockCanvasAndPost(Canvas canvas)
Finish editing pixels in the surface.

Details

Public Methods

public void addCallback(Callback callback)

Add a Callback interface for this holder. There can several Callback interfaces associated to a holder.

Parameters

callback The new Callback interface.

public Surface getSurface()

Direct access to the surface object. The Surface may not always be available -- for example when using a SurfaceView the holder's Surface is not created until the view has been attached to the window manager and performed a layout in order to determine the dimensions and screen position of the Surface. You will thus usually need to implement Callback.surfaceCreated to find out when the Surface is available for use.

Note that if you directly access the Surface from another thread, it is critical that you correctly implement Callback.surfaceCreated and Callback.surfaceDestroyed to ensure that thread only accesses the Surface while it is valid, and that the Surface does not get destroyed while the thread is using it.

Returns

  • Surface The surface.

public Rect getSurfaceFrame()

Retrieve the current size of the surface. Note: do not modify the returned Rect. This is only safe to call from the thread of SurfaceView's window, or while inside of lockCanvas().

Returns

  • Rect The surface's dimensions. The left and top are always 0.

public Canvas lockCanvas(Rect dirty)

Just like lockCanvas() but allows to specify a dirty rectangle. Every pixel within that rectangle must be written; however pixels outside the dirty rectangle will be preserved by the next call to lockCanvas().

Parameters

dirty Area of the Surface that will be modified.

Returns

  • Canvas Use to draw into the surface.

See Also

public Canvas lockCanvas()

Start editing the pixels in the surface. The returned Canvas can be used to draw into the surface's bitmap. A null is returned if the surface has not been created or otherwise can not be edited. You will usually need to implement Callback.surfaceCreated to find out when the Surface is available for use.

The content of the Surface is never preserved between unlockCanvas() and lockCanvas(), for this reason, every pixel within the Surface area must be written. The only exception to this rule is when a dirty rectangle is specified, in which case, non dirty pixels will be preserved.

If you call this repeatedly when the Surface is not ready (before Callback.surfaceCreated or after Callback.surfaceDestroyed), your calls will be throttled to a slow rate in order to avoid consuming CPU.

If null is not returned, this function internally holds a lock until the corresponding unlockCanvasAndPost(Canvas) call, preventing SurfaceView from creating, destroying, or modifying the surface while it is being drawn. This can be more convenience than accessing the Surface directly, as you do not need to do special synchronization with a drawing thread in Callback.surfaceDestroyed.

Returns

  • Canvas Use to draw into the surface.

public void removeCallback(Callback callback)

Removes a previously added Callback interface from this holder.

Parameters

callback The Callback interface to remove.

public void setFixedSize(int width, int height)

Make the surface a fixed size. It will never change from this size. When working with a {link SurfaceView}, this must be called from the same thread running the SurfaceView's window.

Parameters

width The surface's width.
height The surface's height.

public void setFormat(int format)

Set the desired PixelFormat of the surface. The default is OPAQUE. When working with a {link SurfaceView}, this must be called from the same thread running the SurfaceView's window.

Parameters

format A constant from PixelFormat.

See Also

public void setSizeFromLayout()

Allow the surface to resized based on layout of its container (this is the default). When this is enabled, you should monitor surfaceChanged(SurfaceHolder, int, int, int) for changes to the size of the surface. When working with a {link SurfaceView}, this must be called from the same thread running the SurfaceView's window.

public void unlockCanvasAndPost(Canvas canvas)

Finish editing pixels in the surface. After this call, the surface's current pixels will be shown on the screen, but its content is lost, in particular there is no guarantee that the content of the Surface will remain unchanged when lockCanvas() is called again.

Parameters

canvas The Canvas previously returned by lockCanvas().
Build m5-rc15g - 14 May 2008 12:50