Game Development Section 1-drawing a simple image

Source: Internet
Author: User

Let's first draw a circle to understand the general surfaceview framework.

Circleactivity. JavaCode

Package yy. circle;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. window;
Import Android. View. windowmanager;

Public class circleactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
This. requestwindowfeature (window. feature_no_title );
This. getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen );
Setcontentview (new circle (this ));
}

}

Circle. Java code

Package yy. circle;

Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. Graphics. paintflagsdrawfilter;
Import Android. View. keyevent;
Import Android. View. motionevent;
Import Android. View. surfaceholder;
Import Android. View. surfaceview;
Import Android. View. surfaceholder. Callback;

Public class circle extends surfaceview implements callback, runnable {
Private surfaceholder SFH;
Private paint;
Private thread;
Private Boolean flag;
Private canvas;
Private int screenw, screenh;
Private paintflagsdrawfilter PFD = new paintflagsdrawfilter (0, paint. anti_alias_flag | paint. filter_bitmap_flag );
Public circle (context ){
Super (context );
SFH = This. getholder ();
SFH. addcallback (this );
Paint = new paint ();
Paint. setcolor (color. White );
System. Out. Print ("1 ");
Setfocusable (true );
}
Public void surfacecreated (surfaceholder holder ){
Screenw = This. getwidth ();
Screenh = This. getheight ();
Flag = true;
Thread = new thread (this );
Thread. Start ();
}
Public void mydraw (){
Try {
Canvas = SFH. lockcanvas ();
If (canvas! = NULL ){
Canvas. setdrawfilter (PFD );
Canvas. drawcolor (color. Black );
System. Out. Print ("2 ");
Canvas. drawcircle (50, 50, 50, paint );
System. Out. Print ("3 ");
}
} Catch (exception e ){}
Finally {
If (canvas! = NULL ){
SFH. unlockcanvasandpost (canvas );
}
}
}
Public Boolean ontouchevent (motionevent event ){
Return true;
}
Public Boolean onkeydown (INT keycode, keyevent event ){
Return super. onkeydown (keycode, event );
}
Public void logic (){}
Public void run (){
While (FLAG ){
Long start = system. currenttimemillis ();
Mydraw ();
Logic ();
Long end = system. currenttimemillis ();
Try {
If (end-start <50 ){
Thread. Sleep (50-(end-Start ));
}
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
Public void surfacechanged (surfaceholder holder, int format, int width, int height ){}
Public void surfacedestroyed (surfaceholder holder ){
Flag = false;
}

}

 

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.