Android inherits the View class to refresh the page [Android evolution 11]

Source: Internet
Author: User

Recently, in my project, I found that the subclass of the View class can be refreshed using the invalidate method, or processed using the hanlder message mechanism and thread, or directly using postinvalidate in the thread, however, if the child class inherits the surface class, you cannot use the invalidate method to refresh the interface. I wrote a demo for loading images on the simple refresh interface. Let's take a look at it first:

Program diagram: No Click Event click the effect of the circle in front of the boys, click disappear again

Click the rectangular area, and the Arrow below shows the number change.

Click the rectangular area: the upper arrow changes the number and adds:

 

I. mainactivity code:

Package com.cn. android; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. window; import android. view. windowmanager; public class mainactivity extends activity {public static final int refresh = 0; public static final int return = 1; public static final int regain = 2; gameview mgameview = NULL; // = new gameview (this, this); @ o Verride public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); this. initgameview (); New thread (New gamethread ()). start ();} handler mhandler = new handler () {public void handlemessage (Message MSG) {Switch (MSG. wha T) {Case mainactivity. refresh: mgameview. postinvalidate (); // refresh the break interface; Case mainactivity. regain: initgameview (); break; Case mainactivity. return: initreturn (); break; default: Break ;}}; public void initgameview () {mgameview = new gameview (this, this); setcontentview (mgameview );} public void initreturn () {// end program system. exit (0);} class gamethread implements runnable {public void run () {whil E (! Thread. currentthread (). isinterrupted () {message = new message (); message. what = mainactivity. refresh; // send the message mainactivity. this. mhandler. sendmessage (Message); try {thread. sleep (100);} catch (interruptedexception e) {thread. currentthread (). interrupt ();}}}}}

Ii. Code of the gameview class:

Package com.cn. android; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. paint; import android. graphics. rect; import android. view. motionevent; import android. view. view; public class gameview extends view {private mainactivity activity; private bitmap bggame_image; private bitmap game_spoint; private Bitmap game_dpoint; private Boolean showspoint = false; private Boolean showdpoint = false; private Boolean shownose = false; Private Static int scount = 1; Private Static int dcount = 1; private Static int ncount = 1; paint; private string redtext = "6"; Private Static int rcount = 6; private string bluetext = "1"; Private Static int bcount = 1; public gameview (context, mainactivity Activity) {super (context); this. activity = activity; initbitmap () ;}@ override public void ondraw (canvas) {paint = new paint (); paint. settextsize (35366f); canvas. drawbitmap (bggame_image, 0, 0, null); If (showspoint) {canvas. drawbitmap (game_spoint, 32,124, null);} If (showdpoint) {canvas. fig (game_dpoint, 32,207, null);} If (shownose) {canvas. drawbitmap (game_dpoint, 88,329, null);} canvas. dra Wtext (redtext, 148,152, paint); canvas. drawtext (bluetext, 148,230, paint);} // load the image private void initbitmap () {bggame_image = bitmapfactory. decoderesource (this. getresources (), R. drawable. ernie); game_spoint = bitmapfactory. decoderesource (this. getresources (), R. drawable. game_point); game_dpoint = bitmapfactory. decoderesource (this. getresources (), R. drawable. game_point) ;}@ override public Boolean Ontouchevent (motionevent event) {int x = (INT) event. getx (); int y = (INT) event. Gety (); If (event. getaction ()! = Motionevent. action_down) return Super. ontouchevent (event); // set the click area. You can use the refresh interface to display the rect rcsimplechoice, response, rcregaingame, rcreturneuron, rcredbig, rcredsmall, rcbluebig, callback, and rcnose; rcsimplechoice = new rect (21,112, 63,157); rcdoulbechoice = new rect (21,196, 63,235); rcregaingame = new rect (148,424,189,445); rcreturnn = new rect (216,424,258,445 ); rcredbig = new rect (192,114,240,137); rcredsmall = new rect (189,142,236,168); rcbluebig = new rect (196,188,244,215); rcbluesmall = new rect (193,219,240,245); rcnose = new rect (82,326,111,343 ); // click the boys' circle if (rcsimplechoice. contains (x, y) {If (scount % 2 = 1) {showspoint = true;} else if (scount % 2 = 0) {showspoint = false ;} scount ++; // refresh the Interface Activity by sending messages. mhandler. sendemptymessage (mainactivity. refresh); // invalidate ();} // click the girl's circle if (rcdoulbechoice. contains (x, y) {If (dcount % 2 = 1) {showdpoint = true;} else if (dcount % 2 = 0) {showdpoint = false ;} dcount ++; // activity. mhandler. sendemptymessage (mainactivity. refresh); // or directly send the message invalidate ();} If (rcregaingame. contains (x, y) {// click the return key activity. mhandler. sendemptymessage (mainactivity. return);} If (rcreturneuron. contains (x, y) {// click to replay rcount = 6; bcount = 1; activity. mhandler. sendemptymessage (mainactivity. regain);} // click the arrow on the red ball if (rcredbig. contains (x, y) {If (rcount <20) {rcount ++; redtext = string. valueof (rcount); invalidate () ;}// click the arrow next to the red ball if (rcredsmall. contains (x, y) {If (rcount> 6) {rcount --; redtext = string. valueof (rcount); invalidate () ;}// click the arrow on the blue ball if (rcbluebig. contains (x, y) {If (bcount <16) {bcount ++; bluetext = string. valueof (bcount); invalidate () ;}// click the blue-ball down arrow button if (rcbluesmall. contains (x, y) {If (bcount> 1) {bcount --; bluetext = string. valueof (bcount); invalidate () ;}// click if (rcnose. contains (x, y) {If (ncount % 2 = 1) {shownose = true;} else if (ncount % 2 = 0) {shownose = false ;} ncount ++; // refresh the Interface Activity by sending messages. mhandler. sendemptymessage (mainactivity. refresh); // invalidate ();} return Super. ontouchevent (event );}}

3. Note: If you want source code, you can leave a message. If you have any questions, you can leave a message. If you have good opinions, you can also leave a message to discuss and make progress together;

Reprinted please indicate the source: http://blog.csdn.net/wdaming1986/article/details/6677729

Thank you.

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.