当前位置:网站首页>Android interview knowledge points

Android interview knowledge points

2022-07-07 11:17:00 InfoQ

Requestlayout,OnLayout,OnDraw,DrawChild Differences and connections

  • requestLayout() Method  : Will result in a call to measure() The process   and  layout() The process  . explain : Just for View Rearrange the tree layout The process includes measure() and layout() The process , Not invoke draw() The process , But no view will be redrawn, including the caller itself .
  • onLayout() Method ( If it's time to View yes ViewGroup object , This method needs to be implemented , Layout each subview )
  • onDraw() Method to draw the view itself  ( Every View You need to overload the method ,ViewGroup There is no need to implement this method )
  • drawChild() Go back to the draw() Method .

Android How much memory is allocated for each application ?

android Program memory is generally limited to 16M, There are also 24M.

Android View Refresh mechanism

from ViewRoot Object's performTraversals() Method call draw() Method initiates the drawing of the View Trees , It is worth noting that every time a drawing is initiated , Does not redraw every View View of the tree , And will only redraw those “ Need to redraw ” The view of ,View Class internal variable contains a flag bit DRAWN, When the view needs to be redrawn , It's for the sake of View Add this flag bit . Calling process  :
  • mView.draw() Began to draw ,draw() The functions of the method are as follows : Draw the View Do some preparatory operations for displaying the gradient box .
  • call onDraw() Method to draw the view itself  ( Every View You need to overload the method ,ViewGroup There is no need to implement this method )
  • call dispatchDraw () Method to draw a subview ( If it's time to View Type is not ViewGroup, That is, it does not contain sub views , There is no need to overload the method ) It's worth noting that ,ViewGroup Class has been overridden for us dispatchDraw () Function realization of , Applications generally do not need to override this method , But you can overload the parent function to achieve specific functions .

LinearLayout contrast RelativeLayout

  • RelativeLayout Will let son View call 2 Time onMeasure,LinearLayout  There is weight when , Will also call the sub View2 Time onMeasure
  • RelativeLayout The son of View If the height and RelativeLayout Different , It will lead to efficiency problems , space in between View When it's complicated , This problem will be more serious . If possible , Use as much as possible padding Instead of margin. Without affecting the depth of the hierarchy , Use LinearLayout and FrameLayout instead of RelativeLayout.
Why? Google A new one is created for developers by default RelativeLayout, And I'm DecorView I used a LinearLayout. because DecorView The depth of the hierarchy is known and fixed , A title bar above , The next content bar . use RelativeLayout It does not reduce the level depth , So at this time, use... On the root node LinearLayout It's the most efficient . The reason is that a new one is created by default for developers RelativeLayout I hope developers can adopt as few as possible View Hierarchical representation of the layout for optimal performance , Because it's complicated View Nesting has a greater impact on performance .
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070909199085.html