资源描述:
《android雷电开发文档》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、Android雷电开发一游戏背景搭建实现一张图片的循环滚动。GameView.Javapackagecom.xzh.fireplane;importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importa
2、ndroid.graphics.Rect;importandroid.view.KeyEvent;importandroid.view.SurfaceHolder;importandroid.view.SurfaceView;publicclassGameViewextendsSurfaceViewimplementsSurfaceHolder.Callback,Runnable{privateSurfaceHoldersh;privatePaintpaint;privateBitmapbitmap;private
3、RectsrcRect;privateRectdesRect;privateRectdesRect2;privateintdy,dy2=-480;privatebooleanisRunning=true;privateCanvascanvas;publicGameView(Contextcontext){super(context);this.setFocusable(true);sh=this.getHolder();sh.addCallback(this);paint=newPaint();//将原始图片改成3
4、60*480的图片srcRect=newRect(0,0,300,501);desRect=newRect(0,dy,320,480);desRect2=newRect(0,dy2,320,480);bitmap=BitmapFactory.decodeResource(this.getResources(),R.drawable.background);}publicvoidrun(){while(isRunning){drawView();try{Thread.sleep(100);}catch(Interru
5、ptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}publicvoiddrawView(){try{if(sh!=null){canvas=sh.lockCanvas();canvas.drawColor(Color.BLACK);paint.setColor(Color.RED);dy+=3;dy2+=3;desRect.set(0,dy,320,480+dy);desRect2.set(0,dy2,320,480+dy2);
6、canvas.drawBitmap(bitmap,srcRect,desRect,paint);canvas.drawBitmap(bitmap,srcRect,desRect2,paint);if(dy>=480){dy=-480;}if(dy2>=480){dy2=-480;}}}catch(Exceptione){//TODO:handleexception}finally{if(canvas!=null){sh.unlockCanvasAndPost(canvas);}}}publicvoidsurface
7、Changed(SurfaceHolderholder,intformat,intwidth,intheight){}publicvoidsurfaceCreated(SurfaceHolderholder){newThread(this).start();isRunning=true;}publicvoidsurfaceDestroyed(SurfaceHolderholder){isRunning=false;}@OverridepublicbooleanonKeyDown(intkeyCode,KeyEven
8、tevent){if(keyCode==KeyEvent.KEYCODE_BACK){isRunning=false;}returnsuper.onKeyDown(keyCode,event);}}然后,我们在mainactivty.java类中调用一下本文件即可。packagecom.xzh.fireplane;importandroid.app.Acti