程序小白—-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;   //创建一个矩形 用以方置LOGOpublic 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();                                                                //初始化surfaceHoldersurfaceHolder.addCallback(this);//添加回调函数paint = new Paint();   //初始化画笔paint.setAntiAlias(true); //取消锯齿  }@Overridepublic 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);    }}

更多相关文章

  1. 一起学android之底部菜单TabHost的实现(9)
  2. 2011.09.23——— android sample之Notepad(context menu)
  3. Android(安卓)Window系列(一)- window与decorview
  4. Android(安卓)菜单(OptionMenu)大全 建立你自己的菜单
  5. Android底部菜单栏 仿微博效果
  6. Android,百度,云知声tts总结
  7. linux内核启动android文件系统过程分析
  8. Android实现导航菜单左右滑动效果
  9. 热修复AndFix详解

随机推荐

  1. android 遇到TextView 没到末尾 自动换行
  2. 阿里架构师最新整理 Android(安卓)面试点
  3. android 在APP内打开在线的office文件
  4. Android——View绘制流程
  5. Android(安卓)PopWindow与GridView练习
  6. Android——布局中bottom不起作用/left、
  7. Android之Android(安卓)studio设置背景图
  8. android gradle导入依赖的另一个方法
  9. NoClassDefFoundError: android/os/Persi
  10. Android(安卓)音视频开发(二) -- Camera1