Android游戏开发之旅View类详解自定义View的常用方法

113 篇文章 0 订阅
Android游戏开发之旅 View类详解

  自定义 View的常用方法:

  onFinishInflate() 当View中所有的子控件 均被映射成xml后触发

  onMeasure(int, int) 确定所有子元素的大小

  onLayout(boolean, int, int, int, int) 当View分配所有的子元素的大小和位置时触发

  onSizeChanged(int, int, int, int) 当view的大小发生变化时触发

  onDraw(Canvas) view渲染内容的细节

  onKeyDown(int, KeyEvent) 有按键按下后触发

  onKeyUp(int, KeyEvent) 有按键按下后弹起时触发

  onTrackballEvent(MotionEvent) 轨迹球事件

  onTouchEvent(MotionEvent) 触屏事件

  onFocusChanged(boolean, int, Rect) 当View获取 或失去焦点时触发

  onWindowFocusChanged(boolean) 当窗口包含的view获取或失去焦点时触发

  onAttachedToWindow() 当view被附着到一个窗口时触发

  onDetachedFromWindow() 当view离开附着的窗口时触发,Android123提示该方法和 onAttachedToWindow() 是相反的。

  onWindowVisibilityChanged(int) 当窗口中包含的可见的view发生变化时触发

  以上是View实现的一些基本接口的回调方法,一般我们需要处理画布的显示时,重写onDraw(Canvas)用的的是最多的:

  view plaincopy to clipboardprint
@Override 
protected void onDraw(Canvas canvas) { 
  //这里我们直接使用canvas对象处理当前的画布,比如说使用Paint来选择要填充的颜色 
Paint paintBackground = new Paint(); 
paintBackground.setColor(getResources().getColor(R.color.xxx)); //从Res中找到名为xxx的color颜色定义 
canvas.drawRect(0, 0, getWidth(), getHeight(), paintBackground); //设置当前画布的背景颜色为paintBackground中定义的颜色,以0,0作为为起点,以当前画布的宽度和高度为重点即整块画布来填充,具体的请查看Android123未来讲到的Canvas和Paint,在Canvas中我们可以实现画路径,图形,区域,线。而Paint作为绘画方式的对象可以设置颜色,大小,甚至字体的类型等等。 
} 
当然还有就是处理窗口还原状态问题(一般用于横竖屏切换),除了在Activity中可以调用外,开发游戏时我们尽量在View中使用类似

  view plaincopy to clipboardprint
@Override 
protected Parcelable onSaveInstanceState() { 
   Parcelable p = super.onSaveInstanceState(); 
   Bundle bundle = new Bundle(); 
   bundle.putInt("x", pX); 
   bundle.putInt("y", pY); 
   bundle.putParcelable("android123_state", p); 
   return bundle; 
} 
@Override 
protected void onRestoreInstanceState(Parcelable state) { 
   Bundle bundle = (Bundle) state; 
   dosomething(bundle.getInt("x"), bundle.getInt("y")); //获取刚才存储的x和y信息 
   super.onRestoreInstanceState(bundle.getParcelable("android123_state")); 
   return; 
}

  出自: doc-view-5324.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeArray 是 Android 中的一个特殊的资源型,用于在 XML 中声明自定义 View 的属性。使用 TypeArray 可以方便地在 XML 布局中指定 View 的属性,而不需要在 Java 代码中进行硬编码。 使用 TypeArray 的步骤如下: 1. 在 res/values/attrs.xml 文件中定义自定义 View 的属性。 ```xml <resources> <declare-styleable name="MyCustomView"> <attr name="customAttr1" format="integer" /> <attr name="customAttr2" format="string" /> <attr name="customAttr3" format="boolean" /> </declare-styleable> </resources> ``` 2. 在自定义 View 的构造函数中获取 TypedArray 对象,并从中获取属性值。 ```java public class MyCustomView extends View { private int customAttr1; private String customAttr2; private boolean customAttr3; public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); customAttr1 = a.getInt(R.styleable.MyCustomView_customAttr1, 0); customAttr2 = a.getString(R.styleable.MyCustomView_customAttr2); customAttr3 = a.getBoolean(R.styleable.MyCustomView_customAttr3, false); a.recycle(); } } ``` 在上面的代码中,`context.obtainStyledAttributes(attrs, R.styleable.MyCustomView)` 用于获取 TypedArray 对象,`R.styleable.MyCustomView` 是在 attrs.xml 文件中定义的自定义属性集合,`a.getInt()`、`a.getString()`、`a.getBoolean()` 用于从 TypedArray 对象中获取属性值,最后需要调用 `a.recycle()` 来回收 TypedArray 对象。 3. 在 XML 布局中使用自定义 View,并设置属性值。 ```xml <com.example.MyCustomView android:layout_width="match_parent" android:layout_height="wrap_content" app:customAttr1="123" app:customAttr2="hello" app:customAttr3="true" /> ``` 在上面的代码中,`app:customAttr1`、`app:customAttr2`、`app:customAttr3` 是在 attrs.xml 文件中定义的自定义属性名,可以在 XML 布局中使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值