package com.example.fly.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
/**
* Created by fly on 2017/5/24.
*/
public class GameMenu {
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmpMenuLogo;//菜单页面Logo
private Bitmap bmpMenuButton;//菜单页面按钮
private Bitmap bmpbtBG;//菜单页面按钮背景
private Rect rect;
public GameMenu(Bitmap bmpMenuBG, Bitmap bmpMenuLogo, Bitmap bmpMenuButton, Bitmap bmpbtBG) {
this.bmpMenuBG = bmpMenuBG;
this.bmpMenuLogo = bmpMenuLogo;
this.bmpMenuButton = bmpMenuButton;
this.bmpbtBG = bmpbtBG;
rect=new Rect
(0,GameSurfaceview.screenHeight/3,GameSurfaceview.screenWidth,GameSurfaceview.screenHeight/3+GameSurfaceview.screenHeight/5);
}//GameSurfaceview.screenHeight是调用这个高度;GameSurfaceview.screenWidth是调用这个宽度,四个变量,分别是左上角x,y坐标和右下角x,y坐标
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bmpMenuBG, 0, 0, paint);
canvas.drawBitmap(bmpMenuLogo, null, rect, paint);
int x=GameSurfaceview.screenWidth/2-bmpMenuButton.getWidth()/2;
int y=GameSurfaceview.screenHeight/3*2;
canvas.drawBitmap(bmpMenuButton, x, y, paint);
int m=GameSurfaceview.screenWidth/2-bmpbtBG.getWidth()/2;
int n=GameSurfaceview.screenHeight/3*2;
canvas.drawBitmap(bmpbtBG, m, n, paint);
}
}
package com.example.fly.myapplication;
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 fly on 2017/5/24.
*/
public class GameSurfaceview extends SurfaceView implements SurfaceHolder.Callback {
private Canvas canvas;
private Paint paint;
private SurfaceHolder surfaceHolder;
public static int screenWidth;//手机屏幕的宽
public static int screenHeight;//手机屏幕的高
//Menu相关
private GameMenu gameMenu;
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmpMenuLogo;//菜单页面Logo
private Bitmap bmpMenuButton;//菜单页面按钮
private Bitmap bmpbtBG;//菜单页面按钮背景
public GameSurfaceview(Context context) {
super(context);
surfaceHolder = this.getHolder();//初始化holder对象
surfaceHolder.addCallback(this);
paint=new Paint();
paint.setAntiAlias(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenWidth=this.getWidth();
screenHeight=this.getHeight();
initBitmap();
new Thread(new Runnable() {
@Override
public void run() {
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对象画游戏菜单页面
gameMenu.draw(canvas,paint);
if(canvas!=null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
/**
* 初始化图片方法
*/
private void initBitmap() {
//把图片转化成Bitmap
bmpMenuBG = BitmapFactory.decodeResource(this.getResources(), R.drawable.mainmenu);
bmpMenuLogo = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);
bmpMenuButton = BitmapFactory.decodeResource(this.getResources(), R.drawable.starttext);
bmpbtBG = BitmapFactory.decodeResource(this.getResources(), R.drawable.menustart);
//初始化对象
gameMenu = new GameMenu(bmpMenuBG, bmpMenuLogo, bmpbtBG, bmpMenuButton);
}
}
22:58:32
package com.example.fly.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
/**
* Created by fly on 2017/5/24.
*/
public class GameMenu {
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmpMenuLogo;//菜单页面Logo
private Bitmap bmpMenuButton;//菜单页面按钮
private Bitmap bmpbtBG;//菜单页面按钮背景
private Rect rect;
public GameMenu(Bitmap bmpMenuBG, Bitmap bmpMenuLogo, Bitmap bmpMenuButton, Bitmap bmpbtBG) {
this.bmpMenuBG = bmpMenuBG;
this.bmpMenuLogo = bmpMenuLogo;
this.bmpMenuButton = bmpMenuButton;
this.bmpbtBG = bmpbtBG;
rect=new Rect
(0,GameSurfaceview.screenHeight/3,GameSurfaceview.screenWidth,GameSurfaceview.screenHeight/3+GameSurfaceview.screenHeight/5);
}//GameSurfaceview.screenHeight是调用这个高度;GameSurfaceview.screenWidth是调用这个宽度,四个变量,分别是左上角x,y坐标和右下角x,y坐标
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bmpMenuBG, 0, 0, paint);
canvas.drawBitmap(bmpMenuLogo, null, rect, paint);
int x=GameSurfaceview.screenWidth/2-bmpMenuButton.getWidth()/2;
int y=GameSurfaceview.screenHeight/3*2;
canvas.drawBitmap(bmpMenuButton, x, y, paint);
int m=GameSurfaceview.screenWidth/2-bmpbtBG.getWidth()/2;
int n=GameSurfaceview.screenHeight/3*2;
canvas.drawBitmap(bmpbtBG, m, n, paint);
}
}
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
/**
* Created by fly on 2017/5/24.
*/
public class GameMenu {
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmpMenuLogo;//菜单页面Logo
private Bitmap bmpMenuButton;//菜单页面按钮
private Bitmap bmpbtBG;//菜单页面按钮背景
private Rect rect;
public GameMenu(Bitmap bmpMenuBG, Bitmap bmpMenuLogo, Bitmap bmpMenuButton, Bitmap bmpbtBG) {
this.bmpMenuBG = bmpMenuBG;
this.bmpMenuLogo = bmpMenuLogo;
this.bmpMenuButton = bmpMenuButton;
this.bmpbtBG = bmpbtBG;
rect=new Rect
(0,GameSurfaceview.screenHeight/3,GameSurfaceview.screenWidth,GameSurfaceview.screenHeight/3+GameSurfaceview.screenHeight/5);
}//GameSurfaceview.screenHeight是调用这个高度;GameSurfaceview.screenWidth是调用这个宽度,四个变量,分别是左上角x,y坐标和右下角x,y坐标
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bmpMenuBG, 0, 0, paint);
canvas.drawBitmap(bmpMenuLogo, null, rect, paint);
int x=GameSurfaceview.screenWidth/2-bmpMenuButton.getWidth()/2;
int y=GameSurfaceview.screenHeight/3*2;
canvas.drawBitmap(bmpMenuButton, x, y, paint);
int m=GameSurfaceview.screenWidth/2-bmpbtBG.getWidth()/2;
int n=GameSurfaceview.screenHeight/3*2;
canvas.drawBitmap(bmpbtBG, m, n, paint);
}
}
package com.example.fly.myapplication;
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 fly on 2017/5/24.
*/
public class GameSurfaceview extends SurfaceView implements SurfaceHolder.Callback {
private Canvas canvas;
private Paint paint;
private SurfaceHolder surfaceHolder;
public static int screenWidth;//手机屏幕的宽
public static int screenHeight;//手机屏幕的高
//Menu相关
private GameMenu gameMenu;
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmpMenuLogo;//菜单页面Logo
private Bitmap bmpMenuButton;//菜单页面按钮
private Bitmap bmpbtBG;//菜单页面按钮背景
public GameSurfaceview(Context context) {
super(context);
surfaceHolder = this.getHolder();//初始化holder对象
surfaceHolder.addCallback(this);
paint=new Paint();
paint.setAntiAlias(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenWidth=this.getWidth();
screenHeight=this.getHeight();
initBitmap();
new Thread(new Runnable() {
@Override
public void run() {
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对象画游戏菜单页面
gameMenu.draw(canvas,paint);
if(canvas!=null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
/**
* 初始化图片方法
*/
private void initBitmap() {
//把图片转化成Bitmap
bmpMenuBG = BitmapFactory.decodeResource(this.getResources(), R.drawable.mainmenu);
bmpMenuLogo = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);
bmpMenuButton = BitmapFactory.decodeResource(this.getResources(), R.drawable.starttext);
bmpbtBG = BitmapFactory.decodeResource(this.getResources(), R.drawable.menustart);
//初始化对象
gameMenu = new GameMenu(bmpMenuBG, bmpMenuLogo, bmpbtBG, bmpMenuButton);
}
}
22:58:32
package com.example.fly.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
/**
* Created by fly on 2017/5/24.
*/
public class GameMenu {
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmpMenuLogo;//菜单页面Logo
private Bitmap bmpMenuButton;//菜单页面按钮
private Bitmap bmpbtBG;//菜单页面按钮背景
private Rect rect;
public GameMenu(Bitmap bmpMenuBG, Bitmap bmpMenuLogo, Bitmap bmpMenuButton, Bitmap bmpbtBG) {
this.bmpMenuBG = bmpMenuBG;
this.bmpMenuLogo = bmpMenuLogo;
this.bmpMenuButton = bmpMenuButton;
this.bmpbtBG = bmpbtBG;
rect=new Rect
(0,GameSurfaceview.screenHeight/3,GameSurfaceview.screenWidth,GameSurfaceview.screenHeight/3+GameSurfaceview.screenHeight/5);
}//GameSurfaceview.screenHeight是调用这个高度;GameSurfaceview.screenWidth是调用这个宽度,四个变量,分别是左上角x,y坐标和右下角x,y坐标
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bmpMenuBG, 0, 0, paint);
canvas.drawBitmap(bmpMenuLogo, null, rect, paint);
int x=GameSurfaceview.screenWidth/2-bmpMenuButton.getWidth()/2;
int y=GameSurfaceview.screenHeight/3*2;
canvas.drawBitmap(bmpMenuButton, x, y, paint);
int m=GameSurfaceview.screenWidth/2-bmpbtBG.getWidth()/2;
int n=GameSurfaceview.screenHeight/3*2;
canvas.drawBitmap(bmpbtBG, m, n, paint);
}
}