本着针对面试,不负责任的态度,写下《面试总结》系列。本系列记录面试过程中各个知识点,而不是入门系列,如果有不懂的自行学习。
自定义View三种方式,组合现有控件,继承现有控件,继承View
本文只针对继承View的方式,另两种自行学习。
1. 重写方法
onMeasure、 onLayout、onDraw、onTouchEvent
onMeasure
可能多次触发,在measure的过程中注意MeasureSpec,specMode、specSize
讲到LinearLayout、RelativeLayout源码
MeasureSpec
MeasureSpec,specMode、specSize
EXACTLY
表示父布局希望子布局的大小应该是由specSize的值来决定的,系统默认会按照这个规则来设置子布局的大小,开发人员当然也可以按照自己的意愿设置成任意的大小。
AT_MOST
表示子布局最多只能是specSize中指定的大小,开发人员应该尽可能小得去设置这个布局,并且保证不会超过specSize。系统默认会按照这个规则来设置子布局的大小,开发人员当然也可以按照自己的意愿设置成任意的大小。
UNSPECIFIED
表示开发人员可以将布局按照自己的意愿设置成任意的大小,没有任何限制。这种情况比较少见,不太会用到。
childParams/parentMode
EXACTLY
AT_MOST
UNSPECIFIED
dp/px
EXACTLY(childsize)
EXACTLY(childsize)
EXACTLY(childsize)
match_parent
EXACTLY(parentsize)
AT_MOST(parentsize)
UNSPECIFIED(0)
wrap_content
AT_MOST(parentsize)
AT_MOST(parentsize)
UNSPECIFIED(0)
onLayout
在ViewGroup中,只触发一次,决定子View的位置
onDraw
绘制内容,Canvas.drawxxx(),paint
onTouchEvent
处理点击事件
2. 自定义view与viewgroup的区别
onDraw(Canvas canvas)
View类中用于重绘的方法,这个方法是所有View、ViewGroup及其派生类都具有的方法,也是Android UI绘制最重要的方法。开发者可重载该方法,并在重载的方法内部基于参数canvas绘制自己的各种图形、图像效果。
onLayout()
重载该类可以在布局发生改变时作定制处理,这在实现一些特效时非常有用。View中的onLayout不是必须重写的,ViewGroup中的onLayout()是抽象的,自定义ViewGroup必须重写。
dispatchDraw()
ViewGroup类及其派生类具有的方法,控制子View绘制分发,重载该方法可改变子View的绘制,进而实现一些复杂的视效,典型的例子可参见Launcher模块Workspace的dispatchDraw重载。
drawChild()
ViewGroup类及其派生类具有的方法,直接控制绘制某局具体的子view,重载该方法可控制具体某个具体子View。
3. View方法执行过程
Android视图树的根节点是DecorView,而它是FrameLayout的子类,所以就会让其子视图绘制两次,所以onMeasure函数会先被调用两次。
onResume(Activity)
onPostResume(Activity)
onAttachedToWindow(View)
onMeasure(View)
onMeasure(View)
onLayout(View)
onSizeChanged(View)
onMeasure(View)
onLayout(View)
onDraw(View)
dispatchDraw()
4. invalidate()、postInvalidate()、requestLayout()
invalidate()
/**
* Invalidate the whole view. If the view is visible,
* {@link #onDraw(android.graphics.Canvas)} will be called at some point in
* the future.
*
* This must be called from a UI thread. To call from a non-UI thread, call
*/
public void invalidate() {
invalidate(true);
}
invalidate方法会执行draw过程,重绘View树。
当改变view的显隐性、背景、状态(focus/enable)等,这些都属于appearance范畴,都会引起invalidate操作。需要更新界面显示,就可以直接调用invalidate方法。
注意:
View(非容器类)调用invalidate方法只会重绘自身,ViewGroup调用则会重绘整个View树。
postInvalidate()
/**
*
Cause an invalidate to happen on a subsequent cycle through the event loop.
* Use this to invalidate the View from a non-UI thread.
*
*
This method can be invoked from outside of the UI thread
* only when this View is attached to a window.
*/
public void postInvalidate() {
postInvalidateDelayed(0);
}
在子线程中被调用,刷新UI。
requestLayout()
/**
* Call this when something has changed which has invalidated the
* layout of this view. This will schedule a layout pass of the view
* tree. This should not be called while the view hierarchy is currently in a layout
* pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
* end of the current layout pass (and then layout will run again) or after the current
* frame is drawn and the next layout occurs.
*
*
Subclasses which override this method should call the superclass method to
* handle possible request-during-layout errors correctly.
*/
@CallSuper
public void requestLayout() {
}
当View的宽高,发生了变化,不再适合现在的区域,调用requestLayout方法重新对View布局。
当View执行requestLayout方法,会向上递归到顶级父View中,再执行这个顶级父View的requestLayout,所以其他View的onMeasure,onLayout也可能会被调用。
3