Android game Development (ii): Use of Surfaceview

Source: Internet
Author: User

@author: Qingdujun

The use of Surfaceview is very simple, as long as the implementation of Surfaceview and implementation of callback interface can be;

 PackageCom.qdj.surfaceviewone;ImportCom.qdj.ui.SurfaceViewOne;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportAndroid.os.Bundle; Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); /*Set Window*/GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FL        Ag_fullscreen);        Requestwindowfeature (Window.feature_no_title); /*define view by yourself*/Setcontentview (NewSurfaceviewone ( This)); }}

Custom Surfaceview

 PackageCom.qdj.ui;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.view.SurfaceHolder;ImportAndroid.view.SurfaceHolder.Callback;ImportAndroid.view.SurfaceView; Public classSurfaceviewoneextendsSurfaceviewImplementsCallback, Runnable {PrivateSurfaceholder Sfholder; Privatethread thread; PrivateCanvas Canvas; PrivatePaint paint; intScreenw, Screenh; intclock;  PublicSurfaceviewone (Context context) {Super(context); Clock= 1; Thread=NewThread ( This); Sfholder=Getholder (); Sfholder.addcallback ( This); Paint=NewPaint (); Paint.setantialias (true);        Paint.setcolor (Color.Blue); Setkeepscreenon (true); }    Private voidDraw () {Try {            /*get a Canvas object.*/Canvas=Sfholder.lockcanvas (); /*set BG color.*/Canvas.drawcolor (Color.White); Canvas.drawtext ("This is a game." + (clock++), 100, 100, paint); } Catch(Exception e) {}finally {            /*Finish editing pixels in the surface.*/            if(Canvas! =NULL) {sfholder.unlockcanvasandpost (canvas); } }} @Override Public voidrun () { while(true) {draw (); Try {                /*delay 100ms to let us can see.*/Thread.Sleep (100); } Catch(Exception e) {//Todo:handle Exception} }} @Override Public voidsurfacecreated (Surfaceholder holder) {/*get screen size.*/Screenw=getwidth (); Screenh=getheight (); /*start the thread.*/Thread.Start (); } @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) {        //TODO auto-generated Method Stub} @Override Public voidsurfacedestroyed (Surfaceholder holder) {//TODO auto-generated Method Stub    }}

Android game Development (ii): Use of Surfaceview

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.