In many cases, if you want to design a beautiful Android UI, you must consider the self-painted controls to display class views on the android interface, we can use the inheritance extension to override related methods to draw our graphics. First, we need to understand the underlying implementation of the View class. In the SDK, we can see that the view directly inherits from the Java base class object and implements drawing and button event drawable. callback keyevent. callback-related methods: We mainly implement the ondraw method in self-painting. For interface computing, we can rewrite the onmeasure method. For related buttons, We can reload onkeydown, onkeyup, and ontouchevent, the following Android development network is represented by an instance. Public class cwjview extendsview { Public cwjview (context) { This (context, null ); } Public cwjview (context, attributeset attrs) { This (context, attrs, 0 ); } Public cwjview (context, attributeset attrs, int defstyle) { Super (context, attrs, defstyle ); // Here is the construction of this class, and related initialization can be added here Code } @ Override Protectd void ondraw (canvas) { Super (canvas ); // The Key to drawing. You can see that a canvas handle is included. You can perform related operations directly through the canvas class we mentioned earlier. Complete example, you can refer to the example in the android SDK to implement the Snake snake game. } } For update of the View class, call invalidate (int l, int R, int R, int B) to update a rect rectangular area or update all, at the same time, we need to call postinvalidate in the thread to update the interface. |
This article is transferred fromWww.35java.com