首先关键一个class文件叫GameSurfaceView
1、定义
可以直接从内存或者DMA等硬件接口取得图像数据,是个非常重要的绘图容器。
它的特性是:可以在主线程之外的线程中向屏幕绘图上。这样可以避免画图任务繁重的时候造成主线程阻塞,从而提高了程序的反应速度。在游戏开发中多用到SurfaceView,游戏中的背景、人物、动画等等尽量在画布canvas中画出。
2、实现
首先继承SurfaceView并实现SurfaceHolder.Callback接口 使用接口的原因:因为使用SurfaceView 有一个原则,所有的绘图工作必须得在Surface 被创建之后才能开始(Surface—表面,这个概念在 图形编程中常常被提到。基本上我们可以把它当作显存的一个映射,写入到Surface 的内容 可以被直接复制到显存从而显示出来,这使得显示速度会非常快),而在Surface 被销毁之前必须结束。所以Callback 中的surfaceCreated 和surfaceDestroyed 就成了绘图处理代码的边界。
package com.example.dfcn.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; import android.widget.Button; /** * Created by 11918 on 2017/5/24. */ public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback { private Paint paint;//画笔 private Canvas canvas;//画布 private SurfaceHolder surfaceHolder; private Button btn1 = null; private GameMenu menu; private Bitmap bmpMenuBG; private Bitmap bmpLogo; private Bitmap bmpButton; private Bitmap bmpText; public static int screenWidth; public static int screenHight; public static int i=5; public GameSurfaceView(Context context) { super(context); //初始化surfaceHolder surfaceHolder = this.getHolder(); //添加回调函数 surfaceHolder.addCallback(this); paint = new Paint();//创建画笔 paint.setAntiAlias(true);//取消锯齿 } @Override public void surfaceCreated(SurfaceHolder holder) { screenWidth = this.getWidth(); screenHight = this.getHeight(); initBitmap(); new Thread(new Runnable() { @Override public void run() { while (true) { myDraw(); } } }).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } private void myDraw() { //canvas进行锁定 canvas = surfaceHolder.lockCanvas(); menu.drawMenu(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); menu = new GameMenu(bmpMenuBG,bmpLogo,bmpButton,bmpText); } }
然后在创建一个Menu文件来调试背景字体的格局具体代码如下:
package com.example.dfcn.myapplication; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; /** * Created by 11918 on 2017/5/24. */ public class GameMenu { private Bitmap bmpMenuBG; private Bitmap bmpLogo; private Bitmap bmpButton; private Bitmap bmpText; private Rect rect1,rect2; public GameMenu(Bitmap bmpMenuBG,Bitmap bmpLogo,Bitmap bmpButton,Bitmap bmpText){ this.bmpMenuBG = bmpMenuBG; this.bmpLogo = bmpLogo; this.bmpButton = bmpButton; this.bmpText = bmpText; rect1 = new Rect(0,GameSurfaceView.screenHight/3,GameSurfaceView.screenWidth,GameSurfaceView.screenHight/3+GameSurfaceView.screenHight/5); rect2 = new Rect(0,0,GameSurfaceView.screenWidth,GameSurfaceView.screenHight); } public void drawMenu(Canvas canvas, Paint paint){ canvas.drawBitmap(bmpMenuBG,null,rect2,paint); canvas.drawBitmap(bmpLogo,null,rect1,paint); int x1 = GameSurfaceView.screenWidth/2-bmpButton.getWidth()/2; int y1 = GameSurfaceView.screenHight/3*2; canvas.drawBitmap(bmpButton,x1,y1,paint); int x2 = GameSurfaceView.screenWidth/2-bmpText.getWidth()/2; int y2 = GameSurfaceView.screenHight/3*2; canvas.drawBitmap(bmpText,x2,y2+10,paint); } }