資源描述:
《android雷電開(kāi)發(fā)文檔》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在學(xué)術(shù)論文-天天文庫(kù)。
1、Android雷電開(kāi)發(fā)一游戲背景搭建實(shí)現(xiàn)一張圖片的循環(huán)滾動(dòng)。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);}}然后,我們?cè)趍ainactivty.java類中調(diào)用一下本文件即可。packagecom.xzh.fireplane;importandroid.app.Acti