Basic SurfaceView usage and surfaceview usage

Source: Internet
Author: User

Basic SurfaceView usage and surfaceview usage
Core Components of Android game development framework

Core Component introduction SurfaceView introduction sdk Introduction

SurfaceView is an inherited class of View, which is embedded with a Surface dedicated for painting. You can control the format and size of the Surface. Surfaceview controls the position of this Surface.
Surface is ordered in depth (Z-ordered), which indicates that it is always behind its own window. Surfaceview provides a visible area. Only the surface content in this 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 surface
This feature can be used to place overlays (for example, controls such as text and buttons ). Note: If the surface has a transparent control, every change to it will cause the framework to recalculate the transparency between it and the top-level control, which will affect the performance.
You can access this surface through the surfaceHolder interface. The getHolder () method can obtain this interface.
When surfaceview becomes visible, the surface is created. Before surfaceview is hidden, the surface is destroyed. This saves resources. If you want to check the time when the surface is created and destroyed, you can reload surfaceCreated (SurfaceHolder) and surfaceDestroyed (SurfaceHolder) surfaceView. The core of surfaceView is to provide two threads: UI thread and rendering thread.
Note:
1. All SurfaceView and SurfaceHolder. Callback methods are called in the UI thread, which is generally the main thread of the application. Therefore, the variables accessed by the rendering thread should be processed synchronously.
2. Because the surface may be destroyed, it is valid only between SurfaceHolder. Callback. surfaceCreated () and SurfaceHolder. Callback. surfaceDestroyed (). Therefore, the access to the rendering thread must be valid.
Surface.

SurfaceHolder Introduction

SurfaceHolder is the packaging of SurfaceView Surface, not only in SurfaceHolder. the callback interface is responsible for the callback of Surface creation and destruction. It also packages the key Surface Methods LockCanvas () and unLockCanvasAndPost () in thread security, surfaceHolder is the owner of the Surface object and is responsible for calling the Surface operation method in the life cycle of the Surface.
Dirty Rect dirty indicates that the data marked in this rectangle area is voided, that is, the area of the rectangle to be rewritten, LockCanvas (Rect dirty), you can specify a rectangle area, redraw some data on the Canvas on the Surface.

SurfaceView, SurfaceHolder, and Surface relationships

SurfaceView steps SurfaceView demo
Public class GameUI extends SurfaceView implements SurfaceHolder. callback {private SurfaceHolder holder; private RenderThread renderThread; private boolean isDraw = false; // controls the public GameUI (Context context) {super (context); holder = this. getHolder (); holder. addCallback (this); renderThread = new RenderThread () ;}@ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}@ Override public void surfaceCreated (SurfaceHolder holder) {isDraw = true; renderThread. start () ;}@ Override public void surfaceDestroyed (SurfaceHolder holder) {isDraw = false ;} /*** draw interface Thread ** @ author Administrator **/private class RenderThread extends Thread {@ Override public void run () {// keep drawing interface while (isDraw) {drawUI ();} super. run () ;}}/*** interface drawing */public void drawUI () {Canvas canvas = holder. lockCanvas (); try {drawCanvas (canvas);} catch (Exception e) {e. printStackTrace ();} finally {holder. unlockCanvasAndPost (canvas);} private void drawCanvas (Canvas canvas) {// draw the required image on the canvas }}

Luo shengyang's blog SurfaceView source code analysis

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.