Android Game Development Tour view class detailed
Common ways to customize view:
Onfinishinflate () triggers when all child controls in view are mapped to XML
onmeasure (int, int) determines the size of all child elements
OnLayout (boolean, int, int, int, int) triggered when view allocates the size and position of all child elements
onsizechanged (int, int, int, int) triggered when the size of the view changes
OnDraw (Canvas) View details of rendered content
OnKeyDown (int, keyevent) has a button pressed and then triggered
onKeyUp (int, keyevent) triggers when the button is pressed and bounced
Ontrackballevent (motionevent) trackball Event
Ontouchevent (motionevent) Touch screen Event
Onfocuschanged (boolean, int, Rect) triggers when view Gets or loses focus
Onwindowfocuschanged (Boolean) Triggers when a window contains a view that gets or loses focus
Onattachedtowindow () Triggers when view is attached to a window
Ondetachedfromwindow () Triggers when view leaves an attached window, Android123 hints that the method and Onattachedtowindow () are reversed.
onwindowvisibilitychanged (int) triggers when the visible view that is contained in the window has changed
These are the callback methods for some of the basic interfaces of the view implementation, and generally we need to handle the display of the canvas, overriding the OnDraw (canvas) with the most:
View Plaincopy to Clipboardprint
@Override
protected void OnDraw (canvas canvas) {
Here we use the Canvas object to work with the current canvas, for example, using paint to select the color to fill
Paint Paintbackground = new paint ();
Paintbackground.setcolor (Getresources (). GetColor (r.color.xxx)); A color color definition named XXX was found from res
Canvas.drawrect (0, 0, getwidth (), GetHeight (), paintbackground); Set the background color of the current canvas to the color defined in Paintbackground, with 0,0 as the starting point, with the width and height of the current canvas focusing on the entire canvas, and see the canvas and paint that Android123 will cover in the future, In the canvas we can implement picture paths, graphs, areas, lines. Paint as a way of painting objects can be set color, size, and even the type of font and so on.
}
Of course, there is the processing window restore state problem (generally used for screen switching), in addition to the activity can be called outside, in the development of the game we try to use similar in view
View Plaincopy to Clipboardprint
@Override
Protected Parcelable onsaveinstancestate () {
Parcelable p = super.onsaveinstancestate ();
Bundle 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 = (bundle) state;
DoSomething (Bundle.getint ("X"), Bundle.getint ("Y")); Get the X and y information you just stored
Super.onrestoreinstancestate (bundle.getparcelable ("android123_state"));
Return
}
Originating From: doc-view-5324.html
Android Game Development Tour view class detailed