Android draw、onDraw、dispatchDraw、invalidate、computeScroll 一些简要说明
2015-02-28 16:48
387 查看
View中:
public void draw(Canvas canvas) {
/*
1. Draw the background 绘制背景
2. If necessary, save the canvas' layers to prepare for fading 如有必要,颜色渐变淡之前保存画布层(即锁定原有的画布内容)
3. Draw view's content 绘制view的内容
4. Draw children 绘制子view
5. If necessary, draw the fading edges and restore layers 如有必要,绘制颜色渐变淡的边框,并恢复画布(即画布改变的内容附加到原有内容上)
6. Draw decorations (scrollbars for instance) 绘制装饰,比如滚动条
*/
...
if (!dirtyOpaque) {
drawBackground(canvas); //背景绘制
}
// skip step 2 & 5 if possible (common case) 通常情况跳过第2和第5步
...
if (!dirtyOpaque)
onDraw(canvas); //调用onDraw
dispatchDraw(canvas);
//绘制子view
onDrawScrollBars(canvas); //绘制滚动条
...
}
protected void dispatchDraw(Canvas canvas) { //空实现 }
protected void onDraw(Canvas canvas) { //空实现 }
ViewGroup中:
protected void dispatchDraw(Canvas canvas) {
...
drawChild(...); //绘制子view
...
}
protected boolean drawChild(Canvas canvas, View child,
long drawingTime) {
return child.draw(canvas,
this, drawingTime);
}
说明:
1. 自定义一个view时,重写onDraw。
调用view.invalidate(),会触发onDraw和computeScroll()。前提是该view被附加在当前窗口上
view.postInvalidate(); //是在非UI线程上调用的
2. 自定义一个ViewGroup,重写onDraw。
onDraw可能不会被调用,原因是需要先设置一个背景(颜色或图)。
表示这个group有东西需要绘制了,才会触发draw,之后是onDraw。
因此,一般直接重写dispatchDraw来绘制viewGroup
3. 自定义一个ViewGroup
dispatchDraw会调用drawChild。
public void draw(Canvas canvas) {
/*
1. Draw the background 绘制背景
2. If necessary, save the canvas' layers to prepare for fading 如有必要,颜色渐变淡之前保存画布层(即锁定原有的画布内容)
3. Draw view's content 绘制view的内容
4. Draw children 绘制子view
5. If necessary, draw the fading edges and restore layers 如有必要,绘制颜色渐变淡的边框,并恢复画布(即画布改变的内容附加到原有内容上)
6. Draw decorations (scrollbars for instance) 绘制装饰,比如滚动条
*/
...
if (!dirtyOpaque) {
drawBackground(canvas); //背景绘制
}
// skip step 2 & 5 if possible (common case) 通常情况跳过第2和第5步
...
if (!dirtyOpaque)
onDraw(canvas); //调用onDraw
dispatchDraw(canvas);
//绘制子view
onDrawScrollBars(canvas); //绘制滚动条
...
}
protected void dispatchDraw(Canvas canvas) { //空实现 }
protected void onDraw(Canvas canvas) { //空实现 }
ViewGroup中:
protected void dispatchDraw(Canvas canvas) {
...
drawChild(...); //绘制子view
...
}
protected boolean drawChild(Canvas canvas, View child,
long drawingTime) {
return child.draw(canvas,
this, drawingTime);
}
说明:
1. 自定义一个view时,重写onDraw。
调用view.invalidate(),会触发onDraw和computeScroll()。前提是该view被附加在当前窗口上
view.postInvalidate(); //是在非UI线程上调用的
2. 自定义一个ViewGroup,重写onDraw。
onDraw可能不会被调用,原因是需要先设置一个背景(颜色或图)。
表示这个group有东西需要绘制了,才会触发draw,之后是onDraw。
因此,一般直接重写dispatchDraw来绘制viewGroup
3. 自定义一个ViewGroup
dispatchDraw会调用drawChild。
相关文章推荐
- Android draw、onDraw、dispatchDraw、invalidate、computeScroll 一些简要说明(转)
- Android draw、onDraw、dispatchDraw、invalidate、computeScroll 一些简要说明
- Android layout、onLayout 一些简要说明
- Android layout、onLayout 一些简要说明(转)
- Android onMeasure、Measure、measureChild、measureChildren 一些简要说明
- Android layout、onLayout 一些简要说明
- Android onMeasure、Measure、measureChild、measureChildren 一些简要说明
- Android 视图更新的一些说明/Invaliadte()与postInvalidate()的区别
- Android layout、onLayout 一些简要说明
- android view组件draw onDraw,dispatchDraw
- Android 利用invalidate 函数与onDraw 事件来实现绘图动画
- directshow一些过滤器的简要说明
- android系统开发 AP 和 BP 简要说明
- onDraw调用和android中Invalidate和postInvalidate的区别
- android系统开发 AP 和 BP 简要说明
- android 关闭app简要说明
- 浅谈AnDroidDraw+DroidDraw实现Android程序UI设计的分析说明
- 关于Android使用新浪API的一些说明
- Android 利用invalidate 函数与onDraw 事件来实现绘图动画
- Android中JNI 的一些常用说明 JNI_OnLoad registerNatives registerNativeMethods