introduction of android view

20
View 的的的的 Charlie Tsai

Upload: charile-tsai

Post on 16-Apr-2017

834 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Introduction of Android View

View的大小事Charlie Tsai

Page 2: Introduction of Android View

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

Page 3: Introduction of Android View

Agenda

• View & ViewGroup• Mechanism of View• Live debugging• Free topics :D

Page 4: Introduction of Android View

View• Attached to window / Detached from

window• Measure• Layout• Draw

Page 5: Introduction of Android View

ViewGroup• ViewGroup extend View (composition

pattern)• The draw functions are different:

• View.onDraw(Canvas);• ViewGroup.dispatchDraw(Canvas);

Page 6: Introduction of Android View

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

Page 7: Introduction of Android View

Mechanism of View

• Draw from parent, then to children• Canvas is given by Android framework• Cost of overlay?

Page 8: Introduction of Android View

Attach / Detach• onAttachedToWindow() / onDetachedFromWindow()• What is Window?

• WindowManager• ViewRootImpl (composition pattern)

• interactive with WindowManager• performTraversals() & scheduleTraversals()• own the Surface and send Canvas to View

Page 9: Introduction of Android View

Measure

• How big this View should be?• void onMeasure(int, int)• setMeasuredDimension(int, int)

Page 10: Introduction of Android View

Ex: Square View

Page 11: Introduction of Android View

Layout

• Assign the size and position to self and all descendants

• onLayout(boolean changed, l, t, r b)• addOnLayoutChangeListener(…)

Page 12: Introduction of Android View

Draw

• Should I use super.onDraw(Canvas)?• View.onDraw(): hook for customized

drawing• Why ViewGroup.dispatchDraw()?

Page 13: Introduction of Android View

Live Debugging

Page 14: Introduction of Android View

Let’s Go Deeper…

Page 15: Introduction of Android View

Hardware Acceleration

• View.setLayerType(…)• LAYER_TYPE_NONE• LAYER_TYPE_HARDWARE• LAYER_TYPE_SOFTWARE

Page 16: Introduction of Android View

Hardware Acceleration

• save() -> setXXX -> draw -> restore()• setXXX - a function which doesn’t effect

the content• e.g. setX(), setY(), setRotation()…

Page 17: Introduction of Android View

SurfaceView• SurfaceView doesn’t render from parent

• System directly maintain its Canvas• Transparent SurfaceView?

• SurfaceView.setZOrderOnTop()• SurfaceView.getHolder().setFormat(PixelF

ormat.TRANSPARENT);

Page 18: Introduction of Android View

固定聚會:每週三@果子咖啡 (近捷運南京復興站 )技術分享:每月底週六 14:30 - 17:30以及眾多不定期活動 (ex: Workshop)

Page 19: Introduction of Android View

Questions?

Page 20: Introduction of Android View

Thanks!