Android -- & gt; game framework

Source: Internet
Author: User

 

Note: we have been developing games on the j2's platform for a long time. I want to switch to android this time. I think it's okay to think about it. So I wrote this article.

 

Let's talk about the source code.

 

 

 

Package Sur. Face. Pro;

 

Import android. content. Context;

Import android. graphics. Canvas;

Import android. graphics. Color;

Import android. graphics. Paint;

Import android. util. AttributeSet;

Import android. view. SurfaceHolder;

Import android. view. SurfaceView;

Import android. view. SurfaceHolder. Callback;

 

Public class MySurfaceView extends SurfaceView implements Callback, Runnable {

Public SurfaceHolder sfh;

Public Canvas canvas;

Public Paint paint;

Public static int SC _W, SC _H;

Public MySurfaceView (Context context, AttributeSet attrs ){

Super (context, attrs );

Sfh = getHolder ();

Sfh. addCallback (this); // binding surface to surface view

Paint = new Paint ();

Paint. setAntiAlias (true );

Paint. setColor (Color. RED );

This. setKeepScreenOn (true); // keep screen light

}

 

Public void run (){

Int count = 0;

While (count ++ <20 ){

Logic ();

Paint ();

Sleep ();

}

}

 

Private void logic (){

// Do your logic

}

 

Public void sleep (){

Try {

Thread. sleep (100 );

} Catch (InterruptedException e ){

E. printStackTrace ();

}

}

 

Public void paint (){

Canvas = sfh. lockCanvas (); // obtain the class canvas. Other operations are the same as those of j2s. // do your paint

 

Sfh. unlockCanvasAndPost (canvas );

 

}

 

Public void surfaceCreated (SurfaceHolder holder ){

SC _W = getWidth ();

SC _H = getHeight ();

New Thread (this). start ();

 

}

 

Public void surfaceChanged (SurfaceHolder holder, int format, int width,

Int height ){

 

}

 

Public void surfaceDestroyed (SurfaceHolder holder ){

 

}

 

}

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.