View
View类代表用户组件的基本构建块,一个View在屏幕中占据一个矩形区域,用来处理drawing和Event handling。
View类是widgets的基类,widgets可以用来创建UI组件(Button,text fields )。View类的直接子类ViewGroup类
是layouts的基类,layouts是不可见的容器用户包含其他Views或者其他ViewGroups和定义它们的布局属性。
View在窗口中被组织成一个单一的树,可以通过编码和XML布局文件来增加View。创建View时可以设置properties,
focus,listener,visibility。
View 结构体系
在Android平台,你要用到View或ViewGroup的层、节点的方式来定义一个Android用户界面你可以用android
系统已经定义好的小控件或者布局,或者自定义的View。
在activity中调用setContentView()方法并且传递一个根结点对象的引用,来让view显示。Android系统接受
到这个引用并使用它来验证,测量,绘制树。相应的View节点要求它的子视图节点绘制自己。
自定义视图可以覆盖的方法
Category | Methods | Description |
Creation | Constructors | There is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file. |
onFinishInflate() | Called after a view and all of its children has been inflated from XML. | |
Layout | 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. | |
Drawing | onDraw(android.graphics.Canvas) | Called when the view should render its content. |
Event processing | onKeyDown(int, KeyEvent) | Called when a new hardware key event occurs. |
onKeyUp(int, KeyEvent) | Called when a hardware key up event occurs. | |
onTrackballEvent(MotionEvent) | Called when a trackball motion event occurs. | |
onTouchEvent(MotionEvent) | Called when a touch screen motion event occurs. | |
Focus | onFocusChanged(boolean, int, android.graphics.Rect) | Called when the view gains or loses focus. |
onWindowFocusChanged(boolean) | Called when the window containing the view gains or loses focus. | |
Attaching | onAttachedToWindow() | Called when the view is attached to a window. |
onDetachedFromWindow() | Called when the view is detached from its window. | |
onWindowVisibilityChanged(int) | Called when the visibility of the window containing the view has changed. |
1.当一个事件别派到指定的View,View将处理这个事件并通知所有的监听器,
2.View的边界发生变化,调用requestLayout(),
3.View的外表发生变化,调用invalidate(),
4.requestLayout()或invalidate()调用时,框架都会测量,布局,绘制。
View在单一的UI线程中处理,当在其他线程中改变View需要使用Handler类。
Animation
调用setCurrentAnimation(Animation)或startAnimation(Animation)把一个动画对象加到View中。
动画可以改变View的比例,旋转,平移和Alpha。动画开始,框架重绘View直到动画结束。
SurfaceView
在视图层内嵌入了专门用于绘图的Surface,SurfaceView负责将surface放在屏幕中合理的位置。surface是
深排序的,表明它总是排在自己所在窗口后。surfaceview提供了一个可见区域,只有在这个可见区域内的
surface部分内容才可见,可见区域外的部分不可见。
可以通过SurfaceHolder接口访问下层的surface,SurfaceHolder可以用getHolder()获取。Surface
直到窗口可视Surface被创建。1线程
SurfaceView提供了UI线程和渲染线程,所有SurfaceView和SurfaceHolder.Callback中的方法都在UI线程中
调用。渲染线程访问的各种变量应做同步处理。Surface可能被销毁,Surface只在Callback.SurfaceCreated()
和Callback.SurfaceDestroyed()之间有效,要确保渲染线程访问的Surface是有效的。
Surface是受屏幕排序管理的一块原始缓冲区,一个windows对应一个Surface,任何view都画在Surface上。
SurfaceView继承View但有自己的Surface
if (mWindow == null) {
mWindow = new MyWindow(this);
mLayout.type = mWindowType;
mLayout.gravity = Gravity.LEFT|Gravity.TOP;
mSession.addWithoutInputChannel(mWindow, mWindow.mSeq, mLayout,
mVisible ? VISIBLE : GONE, mContentInsets);
}
从源码中看到,Surface创建时会创建一个MyW indows,window和SurfaceView绑定在一起,也就内嵌了一个Surface。