网站大量收购独家精品文档,联系QQ:2885784924

SurfaceView与游戏.doc

  1. 1、本文档共18页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
SurfaceView与游戏SurfaceView与游戏.doc

SurfaceView与游戏 在android中开发游戏,一般来说,或想写一个复杂一点的游戏,是必须用到SurfaceView来开发的。 经过这一阵子对android的学习,我找到了自已在android中游戏开发的误区,不要老想着用Layout和view去实现,不要将某个游戏 中的对象做成一个组件来处理。应该尽量想着在Canvas(画布)中画出游戏戏中的背景、人物、动画等... SurfaceView提供直接访问一个可画图的界面,可以控制在界面顶部的子视图层。SurfaceView是提供给需要直接画像素而不是使用 窗体部件的应用使用的。Android图形系统中一个重要的概念和线索是surface。View及其子类(如TextView, Button) 要画在surface上。每个surface创建一个Canvas对象(但属性时常改变),用来管理view在surface上的绘图操作,如画点画线。 还要注意的是,使用它的时候,一般都是出现在最顶层的:The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. 使用的SurfaceView的时候,一般情况下还要对其进行创建,销毁,改变时的情况进行监视,这就要用到SurfaceHolder.Callback. class BBatt extends SurfaceView implements SurfaceHolder.Callback { public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){} //看其名知其义,在surface的大小发生改变时激发 public void surfaceCreated(SurfaceHolder holder){} //同上,在创建时激发,一般在这里调用画图的线程。 public void surfaceDestroyed(SurfaceHolder holder) {} //同上,销毁时激发,一般在这里将画图的线程停止、释放。 } 例子: public class BBatt extends SurfaceView implements SurfaceHolder.Callback, OnKeyListener { private BFairy bFairy; private DrawThread drawThread; public BBatt(Context context) { super(context); this.setLayoutParams( new ViewGroup.LayoutParams( Global.battlefieldWidth, Global.battlefieldHeight)); this.getHolder().addCallback( this ); this.setFocusable( true ); this.setOnKeyListener( this ); bFairy = new BFairy(this.getContext()); } public void surfaceChanged(SurfaceHolder holder, int format,int width,int height) { drawThread = new DrawThread(holder); drawThread.start(); } public void surfaceDestroyed(SurfaceHolder holder) { if( drawThread != null ) { drawThread.doStop(); while (true) try { drawThread.join(); break ; } catch(Exception ex) {} } } public boolean onKey(View view, int keyCode, KeyEvent event) {} } 实例2:用线程画一个蓝色的长方形。 package com.g3.test; /* * SurfaceView的示例程序 * 演示其流程 */ import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import androi

文档评论(0)

ganqludp + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档