2017-5-24实训的第十天!

来源:互联网 发布:十本法术工厂升级数据 编辑:程序博客网 时间:2024/06/05 09:40

飞机大战(菜单页面)


ヾ(Ő∀Ő๑)ノ太好惹 今天终于开始Android Studio的学习了
(눈‸눈) 虽然我还是学的脑里一团浆糊

GameSurface

 public class GameSurface extends SurfaceView implements SurfaceHolder.Callback{private SurfaceHolder surfaceHolder;private Canvas canvas;private Paint paint;public static int screenWidth;public static int screenHeight; // Menu相关//菜单类所需的图片private GameMenu gameMenu;private Bitmap bmpMenuBG;//菜单页面背景图片private Bitmap bmpLogo;//菜单页面logoprivate Bitmap bmpButton;//菜单页面Buttonprivate Bitmap bmpText;//菜单页面文本public GameSurface(Context context){    super(context);    surfaceHolder=this.getHolder();    surfaceHolder.addCallback(this);    paint=new Paint();    paint.setAntiAlias(true);}@Overridepublic void surfaceCreated(SurfaceHolder holder) {    screenWidth=this.getWidth();    screenHeight=this.getHeight();    initBitmap();    new Thread(new Runnable() {        @Override        public void run() {            while (true) {                myDraw();            }        }    }).start();}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {}private void myDraw() {    canvas =surfaceHolder.lockCanvas();    // 调用GameMenu对象画游戏菜单页面    gameMenu.draw(canvas,paint);    if (canvas!=null){        surfaceHolder.unlockCanvasAndPost(canvas);    } }  //  初始化图片方法 private void initBitmap() {     //把图片转换成Bitmap格式     bmpMenuBG=BitmapFactory.decodeResource(             this.getResources(),R.drawable.mainmenu);     bmpLogo=BitmapFactory.decodeResource(             this.getResources(),R.drawable.logo);     bmpButton=BitmapFactory.decodeResource(             this.getResources(),R.drawable.menustart);     bmpText=BitmapFactory.decodeResource(             this.getResources(),R.drawable.starttext);     //初始化对象     gameMenu = new GameMenu(bmpMenuBG, bmpLogo, bmpButton, bmpText); } }

GameMenu

   public class GameMenu {   private Bitmap bmpMenuBG;   private Bitmap bmpLogo;   private Bitmap bmpButton;   private Bitmap bmpText;   private Rect rect;   public GameMenu(Bitmap bmpMenuBG,Bitmap bmpLogo,Bitmap bmpButton,Bitmap bmpText){    this.bmpMenuBG=bmpMenuBG;    this.bmpLogo=bmpLogo;    this.bmpButton=bmpButton;    this.bmpText=bmpText;    rect = new Rect(0,            GameSurface.screenHeight / 3,            GameSurface.screenWidth,            GameSurface.screenHeight / 3 + GameSurface.screenHeight / 5);}/** * 画菜单页面 * @param canvas * @param paint */public void draw(Canvas canvas,Paint paint){    // 画背景图    canvas.drawBitmap(bmpMenuBG,0,0,paint);    canvas.drawBitmap(bmpLogo,null,rect,paint);    int x=GameSurface.screenWidth/2-bmpButton.getWidth()/2;    int y=GameSurface.screenHeight/3*2;    canvas.drawBitmap(bmpButton,x,y,paint);    int z=GameSurface.screenWidth/2-bmpButton.getWidth()/3;    int s=GameSurface.screenHeight/3*2+20;    canvas.drawBitmap(bmpText,z,s,paint);       }   }

MainActivity

  public class MainActivity extends AppCompatActivity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(new GameSurface(this));     }  }

铛铛铛٩(๑^o^๑)۶完成是这样哒!

这里写图片描述

原创粉丝点击