[Android] view (required for Game Development)

Source: Internet
Author: User

Display and game logic processing are important and complex in android game development.

Let's talk about it here.Android. View. ViewAndAndroid. View. surfaceview.

Surfaceview is a display class derived from the view base class. Its direct sub-classes include glsurfaceview and videoview. It can be seen that Gl, video playback, and camera cameras generally use surfaceview. What are the advantages?

Surfaceview can control the surface format, such as the size and position of the display on the screen. The most important thing is to provide the surfaceholder class, which is obtained using the getholder method and related to canvas lockcanvas ()

Canvas lockcanvas (rect dirty), void removecallback (surfaceholder. Callback callback), void unlockcanvasandpost (canvas) controls graphics and painting,

In the callback of surfaceholder. Callback interface, you can use the following three abstract classes to define specific implementations, such as the first to change the format and display screen.

abstract void  surfaceChanged(SurfaceHolder holder, int format, int width, int height) abstract void  surfaceCreated(SurfaceHolder holder) abstract void  surfaceDestroyed(SurfaceHolder holder) 

For surface-related applications, the android underlying layer also provides GPU acceleration. Therefore, surfaceview is generally used in highly real-time applications rather than directly built from view, at the same time, the glsurfaceview in OpenGL mentioned later in android123 will also be implemented from this class.

The following describes the View class:

Common Methods for custom view: onfinishinflate () when all the child controls in the view are mapped to XML, onmeasure (INT, INT) is triggered to determine the size of all child elements onlayout (Boolean, int, INT) triggers onsizechanged (INT, Int, Int, INT) when the view allocates the size and position of all child elements) when the View Size changes, the ondraw (canvas) view rendering content details onkeydown (INT, keyevent) are triggered by pressing a key to trigger onkeyup (INT, keyevent) trigger ontrackballevent (motionevent) trackball event ontouchevent (motionevent) touch screen event onfocuschanged (Boolean, Int, rect) when the view gets or loses focus Onwindowfocuschanged (Boolean) trigger onattachedtowindow () when the view contained in the window gets or loses focus. ondetachedtowindow () is triggered when the view is attached to a window. ondetachedfromwindow () is triggered when the view leaves the attached window, android123 indicates that this method is the opposite of onattachedtowindow. Onwindowvisibilitychanged (INT) is triggered when the visible view contained in the window changes

The above are some of the basic interface callback methods implemented by the view. Generally, when we need to process the display of the canvas, rewriteOndraw (canvas)The most frequently used:

@ Override protected void ondraw (canvas) {// here we use the canvas object to process the current canvas directly. For example, we use paint to select the color to be filled: paint paintbackground = new paint (); paintbackground. setcolor (getresources (). getcolor (R. color. XXX); // find the color definition canvas named XXX from res. drawrect (0, 0, getwidth (), getheight (), paintbackground); // set the background color of the current canvas to the color defined in paintbackground, starting from 0 to 0, fill the entire canvas with the focus on the width and height of the current canvas. For details, refer to the canvas and painting mentioned by android123 in the future. In the canvas, we can Draw paths, images, areas, and lines. As a painting object, you can set the color, size, and font type. }

Of course, there is also the problem of restoring the window status (generally usedHorizontal/vertical screen Switching), In addition to being called in the activity, we try to use a similar view when developing a game.

@ Override protected parcelable onsaveinstancestate () {parcelable P = super. onsaveinstancestate (); bundle = new bundle (); bundle. putint ("X", PX); bundle. putint ("Y", Py); bundle. putparcelable ("android123_state", P); Return bundle ;}@ override protected void onrestoreinstancestate (parcelable state) {bundle = (bundle) State; dosomething (bundle. getint ("X"), bundle. getint ("Y"); // obtain the stored X and Y information super. onrestoreinstancestate (bundle. getparcelable ("android123_state"); return ;}

If you need to force call the rendering method ondraw in the view, you can use the invalidate () method, which has many overloaded versions, and postinvailidate () in the thread () the method is described in the complete custom view section of android game development tour 6.

Reprinted from: http://www.android123.com.cn/androidkaifa/541.html

Related Article

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.