Android view learning example

Source: Internet
Author: User

View is a superclass of Android, which contains almost all screen types. Each view has a canvas for drawing, which can be expanded at will.

You can customize the view in game development. The canvas function can meet our needs in game development. In Android, any view class only needs to override the ondraw method to display the interface. The custom view can be a complex 3D implementation or a very simple text form.

The core of the game is to constantly draw and refresh the interface. The invalidate method is provided in Android to refresh the interface. Note: invalidate cannot be called directly in the thread, that is, it cannot be called in the Child thread. Therefore, it violates the single-thread model of Android: Android UI operations are not thread-safe, these operations must be executed in the UI thread. Therefore, the most common method in Android is to use handler to update the UI thread. You can also use asynctask.

Example:

Activity:

Public class activity01 extends activity {</P> <p> Private Static final string tag = "mthread"; <br/> Private Static final int refresh = 0x000001; <br/> private gameview mgameview = NULL; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> This. mgameview = new gameview (this); <br/> mgameview. X = 3200000f; <br/> mgameview. y = 1200000f; <br/> set Contentview (mgameview); </P> <p> New thread (New gamethread ()). start (); <br/>}</P> <p> handler myhandler = new handler () {</P> <p> @ override <br/> Public void handlemessage (Message MSG) {</P> <p> switch (MSG. what) {<br/> case activity01.refresh: <br/> mgameview. invalidate (); // repaint view <br/> break; <br/>}< br/> super. handlemessage (MSG); <br/>}< br/>}; </P> <p> class gamethread implements runnable {</P> <p> @ over Ride <br/> Public void run () {<br/> while (! Thread. currentthread (). isinterrupted () {<br/> message = new message (); <br/> message. what = activity01.refresh; </P> <p> activity01.this. myhandler. sendmessage (Message); <br/> try {<br/> thread. sleep (1000); <br/>} catch (interruptedexception e) {<br/> thread. currentthread (). interrupt (); <br/>}</P> <p> @ override <br/> Public Boolean ontouchevent (motionevent event) {<br/> If (event. getaction () = motionevent. action_down) {<br/> mgameview. X = event. getx (); <br/> mgameview. y = event. gety (); <br/>}</P> <p> return true; <br/>}</P> <p> @ override <br/> Public Boolean onkeydown (INT keycode, keyevent event) {<br/> If (keycode = keyevent. keycode_back) {<br/> This. finish (); <br/>}</P> <p> return true; <br/>}< br/>}
Gameview

<Textarea readonly name = "code" class = "Java"> public class gameview extends view {<br/> int COUNT = 0; <br/> float x = 0, y = 0; </P> <p> Public gameview (context) {<br/> super (context ); <br/>}</P> <p> Public void ondraw (canvas) {<br/> If (count <100) {<br/> count ++; <br/>}else {<br/> COUNT = 0; <br/>}</P> <p> paint mpaint = new paint (); <br/> switch (count % 4) {<br/> case 0: <br/> mpaint. setcolor (color. blue); <br/> break; <br/> case 1: <br/> mpaint. setcolor (color. green); <br/> break; <br/> case 2: <br/> mpaint. setcolor (color. red); <br/> break; <br/> case 3: <br/> mpaint. setcolor (color. yellow); <br/> break; <br/> default: <br/> mpaint. setcolor (color. white); <br/> break; <br/>}</P> <p> canvas. drawrect (X-40, Y-20, x + 40, Y + 20, mpaint); <br/>}< br/> </textarea>
Running effect:

Source code download

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.