程序小白----AndroidStudio之飛機大戰

程序小白—-AndroidStudio之飛機大戰

今天我們開始一些Android的學習

一:菜單類

新建一個菜單類用於定義一些變量和方法:

package com.example.xiao.fly;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
//以上爲必要的包
/**
 * Created by Xiao on 2017/5/24.
 */

public class GameMeun {

    private Bitmap bmpMeunBG;  //菜單背景圖片
    private  Bitmap bmpLogo;    //LOGO
    private  Bitmap bmpButton;  //按鈕
    private Bitmap bmpText;     //文本
    private Rect rect;   //創建一個矩形 用以方置LOGO

public GameMeun(Bitmap bmpMeunBG,Bitmap bmpLogo,Bitmap bmpButton,Bitmap bmpText){

        this.bmpMeunBG=bmpMeunBG;
        this.bmpLogo=bmpLogo;
        this.bmpButton=bmpButton;
        this.bmpText=bmpText;
        rect = new Rect(0,GameSurface.hight/3,GameSurface.width,GameSurface.hight/3+GameSurface.hight/5);}
        //這是關於矩形的大小位置設計
/*以下是關於該方法的API文檔解釋
*   * @param left   The X coordinate of the left side of the rectangle
     * @param top    The Y coordinate of the top of the rectangle
     * @param right  The X coordinate of the right side of the rectangle
     * @param bottom The Y coordinate of the bottom of the rectangle
     */
    public Rect(int left, int top, int right, int bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }
*/


    /**
     * 畫背景圖片
     * @param canvas
     * @param paint
     */
    public  void  draw(Canvas canvas , Paint paint){
        //畫背景圖片
        canvas.drawBitmap(bmpMeunBG,0,0,paint);
        //畫logo
        canvas.drawBitmap(bmpLogo,null,rect,paint);
        //畫按鈕
        int x = GameSurface.hight/3*2;
        int y = GameSurface.width/2-bmpButton.getWidth()/2;
        canvas.drawBitmap(bmpButton,y,x,paint);  //前寬厚高
        //畫文本
        int z = GameSurface.width/2-bmpText.getWidth()/2;
        canvas.drawBitmap(bmpText,z,x,paint);
    };
}

二:面板類:

package com.example.xiao.fly;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by Xiao on 2017/5/24.
 */

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback{
//定義一些必要的變量
    private Canvas canvas;//畫布
    private Paint paint;//畫筆
    private SurfaceHolder surfaceHolder;

    public  static  int width;
    public  static  int hight;
    //Meun相關的

   //來自主菜單的一些變量
    private  GameMeun gameMenu;
    private Bitmap bmpMeunBG;
    private  Bitmap bmpLogo;
    private  Bitmap bmpButton;
    private Bitmap bmpText;

    public GameSurface(Context context) {
        super(context);
        surfaceHolder = this.getHolder();                                                                //初始化surfaceHolder
surfaceHolder.addCallback(this);//添加回調函數
paint = new Paint();   //初始化畫筆
paint.setAntiAlias(true); //取消鋸齒

  }
@Override

public void surfaceCreated(SurfaceHolder holder) {
width=this.getWidth();  //獲取面板的寬度
hight=this.getHeight();  //獲取面板的高度

intiBitmap();//初始化的方法

new Thread(new Runnable() {  
         @Override
         public void run() { 
             Mydraw();       ///進入線程通過Mydraw開始寫入到視圖中
         }
     }).start();
    }



    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    /**
     * 繪圖方法
     */
    private void Mydraw() {
      canvas = surfaceHolder.lockCanvas();  //創建視圖到畫板上
        gameMenu.draw(canvas,paint); //把所有的元素都佈局到畫板上
        if (canvas!=null){
    surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }

    /**
     * 初始化圖片
     */

    private void intiBitmap() {
        //把圖片轉化爲Bitmap格式
        bmpMeunBG = 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 GameMeun(bmpMeunBG,bmpLogo,bmpButton,bmpText);

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章