zoukankan      html  css  js  c++  java
  • Android游戏开发教程之六:自定义View详解

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

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

    Java代码
    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.   
    8.   //dosomething   
    9.   
    10. }  
    11. 本文转自:http://www.jizhuomi.com/android/game/68.html
  • 相关阅读:
    kettle在linux下执行任务
    activemq spring 集成与测试
    mysql创建存储过程,定时任务,定时删除log
    dubbo-admin 无法支持JDK1.8
    自定义事件驱动加异步执行
    AOP 实现自定义注解
    mybatis-plus的学习
    阿里巴巴架构师的成长之路
    Redis错误:jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
    freemark基础知识
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959944.html
Copyright © 2011-2022 走看看