在Android游戏开发中,有时Android控件不能满足我们的要求,就有必要使用Android自定义View。自定义View实现起来也不难,就是先继承View类,然后重写构造函数、onDraw、onMeasure等函数。
       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支持多继承可以帮助我们不断的完善复杂的问题。
Java代码
    - public class cwjView extends View {   
-   
-     public cwjView(Context context) {   
-          
-       super(context);    
-         
-       setFocusable(true);   
-       setFocusableInTouchMode(true);   
-          
-    }   
-   
-    @Override  
-    protected Parcelable onSaveInstanceState() {    
-       Parcelable pSaved = super.onSaveInstanceState();   
-         
-       Bundle bundle = new Bundle();   
-     
-        
-       return bundle;   
-    }   
-    @Override  
-    protected void onRestoreInstanceState(Parcelable state) {    
-          
-       Bundle bundle = (Bundle) state;   
-   
-        
-      super.onRestoreInstanceState(bundle.getParcelable("cwj"));   
-       return;   
-    }   
-        @Override  
-    protected void onSizeChanged(int w, int h, int oldw, int oldh)   
-    {   
-       super.onSizeChanged(w, h, oldw, oldh);   
-    }   
-   
-    @Override  
-    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)     
-    {   
-       super.onMeasure(widthMeasureSpec, heightMeasureSpec);   
-    }   
-     
-    @Override  
-    protected void onLayout (boolean changed, int left, int top, int right, int bottom)    
-    {   
-     super.onLayout (changed,left,top, ight,bottom) ;   
-    }   
-   
-    @Override  
-    protected void onDraw(Canvas canvas) {   
-         
-       Paint bg = new Paint();   
-       bg.setColor(Color.Red);   
-       canvas.drawRect(0, 0, getWidth()/2, getHeight()/2, bg);   
-        
-    }   
-   
-    @Override  
-    public boolean onTouchEvent(MotionEvent event) {   
-     
-          return super.onTouchEvent(event);   
-     
-    }   
-   
-    @Override  
-    public boolean onKeyDown(int keyCode, KeyEvent event) {   
-          
-       switch (keyCode) {   
-       case KeyEvent.KEYCODE_DPAD_UP:   
-            
-          break;   
-       case KeyEvent.KEYCODE_DPAD_DOWN:   
-             
-          break;   
-       case KeyEvent.KEYCODE_DPAD_LEFT:   
-             
-          break;   
-       case KeyEvent.KEYCODE_DPAD_RIGHT:   
-           
-          break;   
-          
-       case KeyEvent.KEYCODE_DPAD_CENTER:   
-          
-          break;   
-       default:   
-          return super.onKeyDown(keyCode, event);   
-       }   
-       return true;   
-    }   
-   
-  }  
       以上的代码中,onMeasure其实是直接用的父类的方法。而如果我们要修改自定义View的尺寸大小,可以参考下面的代码。
Java代码
    - @Override  
- protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)     
- {   
-    height = View.MeasureSpec.getSize(heightMeasureSpec);     
-    width = View.MeasureSpec.getSize(widthMeasureSpec);     
-    setMeasuredDimension(width,height);     
-   
-     
-   
- }