Android游戏开发之六:自定义View详解

View需处理的三个问题

对于常规的游戏,我们在View中需要处理以下几种问题: 1.控制事件;2.刷新View;3. 绘制View。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网络空间、营销软件、网站建设、双台子网站维护、网站推广。

1. 对于控制事件今天我们只处理按键事件onKeyDown,以后的文章中将会讲到屏幕触控的具体处理onTouchEvent以及Sensor重力感应等方法。

2. 刷新view的方法这里主要有invalidate(int l, int t, int r, int b) 刷新局部,四个参数分别为左、上、右、下。整个view刷新invalidate(),刷新一个矩形区域invalidate(Rect dirty) ,刷新一个特性Drawable, invalidateDrawable(Drawable drawable) ,执行invalidate类的方法将会设置view为无效,最终导致onDraw方法被重新调用。由于今天的view比较简单,提示大家如果在线程中刷 新,除了使用handler方式外,可以在Thread中直接使用postInvalidate方法来实现。

3. 绘制View主要是onDraw()中通过形参canvas来处理,相关的绘制主要有drawRect、drawLine、drawPath等等。 view方法内部还重写了很多接口,其回调方法可以帮助我们判断出view的位置和大小,比如onMeasure(int, int) Called to determine the size requirements for this view and all of its children.  、onLayout(boolean, int, int, int, int) Called when this view should assign a size and position to all of its children 和onSizeChanged(int, int, int, int) Called when the size of this view has changed. 具体的作用,大家可以用Logcat获取当view变化时每个形参的变动。

自定义View框架

下面cwjView是我们为今后游戏设计的一个简单自定义View框架,我们可以看到在Android平台自定义View还是很简单的,同时Java支持多继承可以帮助我们不断的完善复杂的问题。

 
 
  1. public class cwjView extends View {    
  2.     public cwjView(Context context) {    
  3.       super(context);     
  4.       setFocusable(true); //允许获得焦点    
  5.       setFocusableInTouchMode(true); //获取焦点时允许触控    
  6.    }    
  7.    @Override   
  8.    protected Parcelable onSaveInstanceState() {  //处理窗口保存事件    
  9.       Parcelable pSaved = super.onSaveInstanceState();    
  10.       Bundle bundle = new Bundle();    
  11.      //dosomething    
  12.       return bundle;    
  13.    }    
  14.    @Override   
  15.    protected void onRestoreInstanceState(Parcelable state) {  //处理窗口还原事件    
  16.       Bundle bundle = (Bundle) state;    
  17.      //dosomething   super.onRestoreInstanceState(bundle.getParcelable("cwj"));    
  18.       return;    
  19.    }    
  20.        @Override   
  21.    protected void onSizeChanged(int w, int h, int oldw, int oldh) //处理窗口大小变化事件    
  22.    {    
  23.       super.onSizeChanged(w, h, oldw, oldh);    
  24.    }    
  25.    @Override   
  26.    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)      
  27.    {    
  28.       super.onMeasure(widthMeasureSpec, heightMeasureSpec); //如果不让父类处理记住调用setMeasuredDimension    
  29.    }    
  30.    @Override   
  31.    protected void onLayout (boolean changed, int left, int top, int right, int bottom)     
  32.    {    
  33.     super.onLayout (changed,left,top, ight,bottom) ;    
  34.    }    
  35.    @Override   
  36.    protected void onDraw(Canvas canvas) {    
  37.       Paint bg = new Paint();    
  38.       bg.setColor(Color.Red);    
  39.       canvas.drawRect(0, 0, getWidth()/2, getHeight()/2, bg); //将view的左上角四分之一填充为红色      
  40.    }    
  41.    @Override   
  42.    public boolean onTouchEvent(MotionEvent event) {    
  43.          return super.onTouchEvent(event); //让父类处理屏幕触控事件    
  44.    }    
  45.    @Override   
  46.    public boolean onKeyDown(int keyCode, KeyEvent event) { //处理按键事件,响应的轨迹球事件为 public boolean onTrackballEvent (MotionEvent event)     
  47.       switch (keyCode) {    
  48.       case KeyEvent.KEYCODE_DPAD_UP:    
  49.          break;    
  50.       case KeyEvent.KEYCODE_DPAD_DOWN:    
  51.          break;    
  52.       case KeyEvent.KEYCODE_DPAD_LEFT:    
  53.          break;    
  54.       case KeyEvent.KEYCODE_DPAD_RIGHT:    
  55.          break;    
  56.       case KeyEvent.KEYCODE_DPAD_CENTER: //处理中键按下    
  57.          break;    
  58.       default:    
  59.          return super.onKeyDown(keyCode, event);    
  60.       }    
  61.       return true;    
  62.    }    
  63.  }   

以上的代码中,onMeasure其实是直接用的父类的方法。而如果我们要修改自定义View的尺寸大小,可以参考下面的代码。

 
 
  1. @Override   
  2. protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)      
  3. {    
  4.    height = View.MeasureSpec.getSize(heightMeasureSpec);      
  5.    width = View.MeasureSpec.getSize(widthMeasureSpec);      
  6.    setMeasuredDimension(width,height);   //这里面是原始的大小,需要重新计算可以修改本行    
  7.   //dosomething    
  8. }  

标题名称:Android游戏开发之六:自定义View详解
转载来于:http://www.hantingmc.com/qtweb/news16/292116.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联