安卓学习58

今天学习了安卓的一些知识其中主要的是:

View 类详解

在Android 游戏开发之旅二中我们讲到了View 和SurfaceView 的区别,

今天Android123 从View 类开始着重的介绍Android 图形显示基类的相关方法和注意点。

自定义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)用的的是最多的:

@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 中使用类似@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;

}

在View 中如果需要强制调用绘制方法onDraw,可以使用invalidate()方法,它有很多重载版本,同时在线程中的

postInvailidate()方法将在Android 游戏开发之旅六中的自定义View 完整篇讲到。

原文地址:https://www.cnblogs.com/092e/p/14916713.html