introduction of android view
TRANSCRIPT
View的大小事Charlie Tsai
Charlie Tsai@Android Study [email protected]
Individual App Developer &System Software Engineer@NVIDIA (will quit in
Sep.)
Today’s source codehttps://github.com/chatea/ViewDemo
Agenda
• View & ViewGroup• Mechanism of View• Live debugging• Free topics :D
View• Attached to window / Detached from
window• Measure• Layout• Draw
ViewGroup• ViewGroup extend View (composition
pattern)• The draw functions are different:
• View.onDraw(Canvas);• ViewGroup.dispatchDraw(Canvas);
Mechanism of View• What does View do?• What does View NOT do?• Need to attach to Window• measure: analysis the size of view• layout: determine the position and size of
view• draw: real function to render the View
Mechanism of View
• Draw from parent, then to children• Canvas is given by Android framework• Cost of overlay?
Attach / Detach• onAttachedToWindow() / onDetachedFromWindow()• What is Window?
• WindowManager• ViewRootImpl (composition pattern)
• interactive with WindowManager• performTraversals() & scheduleTraversals()• own the Surface and send Canvas to View
Measure
• How big this View should be?• void onMeasure(int, int)• setMeasuredDimension(int, int)
Ex: Square View
Layout
• Assign the size and position to self and all descendants
• onLayout(boolean changed, l, t, r b)• addOnLayoutChangeListener(…)
Draw
• Should I use super.onDraw(Canvas)?• View.onDraw(): hook for customized
drawing• Why ViewGroup.dispatchDraw()?
Live Debugging
Let’s Go Deeper…
Hardware Acceleration
• View.setLayerType(…)• LAYER_TYPE_NONE• LAYER_TYPE_HARDWARE• LAYER_TYPE_SOFTWARE
Hardware Acceleration
• save() -> setXXX -> draw -> restore()• setXXX - a function which doesn’t effect
the content• e.g. setX(), setY(), setRotation()…
SurfaceView• SurfaceView doesn’t render from parent
• System directly maintain its Canvas• Transparent SurfaceView?
• SurfaceView.setZOrderOnTop()• SurfaceView.getHolder().setFormat(PixelF
ormat.TRANSPARENT);
固定聚會:每週三@果子咖啡 (近捷運南京復興站 )技術分享:每月底週六 14:30 - 17:30以及眾多不定期活動 (ex: Workshop)
Questions?
Thanks!