Reproduced: the end of 9102, Android view my understanding of Article 13;
( Pulled left GitHub link, you need access to relevant content such as interviews can find their own )
https://github.com/xiangjiana/Android-MS
Dian View of a drawing process?
- Answer
main View workflow refers measure, layout, draw these three processes, i.e. the measurement, and the layout drawing, wherein determining the measure of View measured width / / high , the layout determining View final width / / high and four vertices location , and draw View will draw on the screen
View of the drawing process follow the following steps :
- Draw background background.draw (canvas)
- Draw your own (onDraw)
- Drawing children (dispatchDraw)
-
Drawing decorative (onDrawScollBars)
Two Dian View event distribution system
Click event occurs, the first delivery to the Activity dispatchTouchEvent
method, by PhoneWindow
passing to DecorView
and then passed root ViewGroup
, enter ViewGroup
the dispatchTouchEvent
method, the implementation of onInterceptTouchEvent
the method to determine whether the interception, interception of the case do not, at this time will traverse ViewGroup
child elements into a sub-View dispatchToucnEvent
method, if the child view is set up onTouchListener
on the implementation of onTouch
the method, and according to onTouch
the return value of true or false to decide whether to perform the onTouchEvent
method, if it is false to continue onTouchEvent
. In onTouchEvent
the Action Up event in judgment, if set up onClickListener
, on the implementation of onClick
the method.
View event delivery distribution mechanism?
Reference answer :
- View event distribution is essentially on the
MotionEvent
course of events distributed. That is, when a MotionEvent occurs, the system will be delivered to the click event on a specific View - Click on the event delivery order: Activity (Window) → ViewGroup → View
- Event distribution process jointly carried out by three methods
-
dispatchTouchEvent
: Used to distribute event. If the event can be transferred to the current View, then this method will be called, returns results were affected by the current ViewonTouchEvent
and View of lowerdispatchTouchEvent
impact methods of indicating whether the consumption of current events -
onInterceptTouchEvent
: Inside the method call, the incident interception. This method onlyViewGroup
has, View (not includedViewGroup
) is not. Once intercepted, the implementationViewGroup
ofonTouchEvent
, inViewGroup
the deal with the event, and not then distributed to View. And only called once, returning a result indicating whether the interception Current events -
onTouchEvent
: In thedispatchTouchEvent
method call to handle click events, returns a result indicating whether the consumption of current events
-
Wed and View the loading process
With the creation of the Activity View and loaded when starting a startActivity Activity, in
ActivityThread
the handleLaunchActivity
method performs Activity of onCreate
the method, this time will be called setContentView
to load the layout to create DecorView
and loaded into our layout DecorView
, when the execution handleResumeActivity
time, the Activity onResume
method is called, and then WindowManager
will be DecorView
set to ViewRootImpl
, so, DecorView
it is loaded into the Window, and this time the interface has not been shown, also you need to measure, layout and draw method through the View, the View to complete the workflow. We need to know View drawing is ViewRoot
to be responsible, each DecorView
has an associated ViewRoot
, this relationship is WindowManager
maintained, will DecorView
and ViewRoot
after association, ViewRootImpl
the requestLayout
will be called to complete the initial layout, the scheduleTraversals
main thread method sending a request message to traverse the final call ViewRootImpl
of the performTraversals
method, which performs the measure layout View and draw process
Four Dian custom view points to note
1. Let view support wrap_content property, in onMeasure
doing special processing method for AT_MOST mode, otherwise it wrap_content
will be, and match_parent
the same effect (inherited ViewGroup
also want to onMeasure
make this determination process)
if (widthMeasureSpec == MeasureSpec.AT_MOST && heightMeasureSpec == MeasureSpec.AT_MOST) { setMeasuredDimension( 200 , 200 ); // wrap_content
To set a default value, the 200 is just an example, the final value to be calculated to obtain the contents just wrapped width and height values
} else if (widthMeasureSpec == MeasureSpec.AT_MOST) {
setMeasuredDimension( 200 ,heightMeasureSpec );
} else if (heightMeasureSpec == MeasureSpec.AT_MOST) {
setMeasuredDimension(heightMeasureSpec , 200 );
}
2. Let view support padding (onDraw when width and height values minus the padding, margin control by the parent layout, no view to consider), custom ViewGroup
need to consider the impact of their padding and sub-view of the margin caused
3. Try in view of the Do not use the handler, using their own view of post method
4. onDetachedFromWindow in time to stop a thread or animation
when 5.view nested case with a sliding, sliding handle conflict
Five Dian View, measure layout and draw
In the top of the analysis, we know that the entrance in the process of drawing View ViewRootImpl
of the performTraversals
method, the method calls the first performMeasure
method, a passing childWidthMeasureSpec
and childHeightMeasureSpec
parameters, these two parameters represent DecorView
the MeasureSpec
values, the MeasureSpec
values and the size of the window DecorView
of LayoutParams
the decision, View the final call to measure flow into the measuring method
measure:
View of the process by the measure ViewGroup
from passing, before calling View.measure method according View itself will first LayoutParams
parent layout MeasureSpec
determined in the sub-view MeasureSpec
, then the view width and height corresponding to the measureSpec
transmission method to measure, then the sub-view the MeasureSpec
access rules are what? Several sub-described case where
1. The parent layout is EXACTLY mode:
a. child view width or height is to determine the value, then the child view of the size is the determining value, mode is EXACTLY (is not to say that the child view width and height can be more than parent view? see below a)
b. child view width or height is set to match_parent, then the size of the sub-view is the parent container fills the remaining space model is EXACTLY the
C. sub-view is set to the wrap_content wide or high, then the sub-view size of the remaining space is filled parent container, the container can not exceed the size of the parent mode is AT_MOST
2. The parent layout is AT_MOST mode:
a. Sub view width or height is determined value, then the sub-view of the size is the determination value, mode is EXACTLY the
B. sub-view width or height to match_parent, the sub view of the size that fills the parent container remaining space, no more than parent container size, model is AT_MOST
C. sub-view is set to the wrap_content wide or high, then the sub-view size of the remaining space is filled parent container, the container can not exceed the size of the parent mode is AT_MOST
3. Father layout is UNSPECIFIED mode:
a. Sub view width or height is determined value, then the sub-view of the size is the determination value, mode is EXACTLY the
B. sub-view width or height to match_parent, the sub view of the size is 0, model is UNSPECIFIED
C. sub-view width or height to the wrap_content, then the size of the sub-view is 0, model is UNSPECIFIED
Get to the width and height of the MeasureSpec
post, measure passed in view of the method to determine the view width and height, but also this time Points
1. When
MeasureSpec
the mode is UNSPECIFIED, view of the height or width at this time depends on the background view there is provided, if the background is not set, return the setminWidth
orminHeight
, if the two values are not set a default is 0, if the background view set , it is takenminWidth
, orminHeight
the background of thisdrawable
maximum width inherent high school or return
2. whenMeasureSpec
the mode is AT_MOST and EXACTLY the, case width and height are returned from the viewMeasureSpec
acquired in size to a value, the value determined in the Analytical upper side. So if you want to implement a custom view, be sure to override the view through inheritanceonMeasure
methodwrap_conten
attribute to do, otherwise, hematch_parent
andwrap_content
properties of the effect is the same
layout:
the role of layout method is used to determine the position of the view itself, onLayout
a method for determining the position of all child elements, when ViewGroup
after the position determination, it is onLayout
through all child elements will call its layout and method, the sub-element layout method, onLayout
the method will be called. Process layout method is to first by setFrame
determining the position of the view four vertices method, and then view the location in the parent container also determined, then calls the onLayout
method to determine the position of the child element, onLayout
an empty method, you need to realize successor .
getMeasuredHeight
And getHeight
methods What is the difference?
getMeasuredHeight
(Measurement height) formed on the measure of the process view, getHeight
(final height) is formed in the layout process, in some cases, multiple view to determining the measurement needs to measure the width and height, measured in the previous process several times, the resulting measured width high and possible final inconsistent width and height, but eventually, it will still the same, there is a situation can lead to both the values are different, the following, this code causes the final aspect ratio of the width measurement view tall 100PX
public void layout(int l,int t,int r, int b) { super.layout(l,t,r+100,b+100); { }
View of the drawing process follow the following steps:
a. Draw BACKGROUND background.draw (Canvas)
B. draw itself (the onDraw)
C. plotted Children (dispatchDraw)
D. decorative drawing (onDrawScrollBars)
View passed through the drawing process is dispatchDraw
implemented, it will traverse all draw method of sub-elements, so the event will draw down layer by layer transfer
PS: view there is a special way setWillNotDraw
, if a view not necessarily drawn content, i.e., no need to override onDraw
methods drawing, can open this tag will accordingly optimized. By default, View this flag is not turned on by default think need to implement onDraw
methods to draw, when we inherit ViewGroup
implementing custom controls, and do not need to know exactly have drawn feature, you can turn this mark, if we rewrite onDraw
, so to be displayed Close this tag
Child view width and height can be more than parent view? can
1. Android: clipChildren = "to false" to set this property on the parent view. On behalf of one of the sub-View can go beyond the screen.
2. The sub-view to have a specific size, than some big talent beyond the parent view. For example, a parent view height 100px sub-view sets the height of 150px. Child view is larger than the parent view, such excess property makes sense. (Height dynamically assigned in the code, but can not wrap_content / match_partent).
3. The parent layout as well as demands to use linearLayout (I use anyway RelativeLayout is not). View above if you have to use other layouts may need to go beyond the set of a linearLayout then sets out other layouts.
4. padding outermost If the layout does not exceed
What six Dian MotionEvent that? It contains several events? Under what conditions will produce?
Reference answer:
MotionEvent is a series of events after the finger touches the screen produced. Typical event types are as follows:
ACTION_DOWN: new to finger screen
ACTION_MOVE: move your finger on the screen
ACTION_UP: Release your finger from the screen the moment
ACTION_CANCELL: finger held down operations and move from the current control to the outer layer of control fires
Under normal circumstances, one finger touches the screen's behavior will trigger a series of click
event, consider the following situations:
After clicking the screen release, the sequence of events: DOWN → UP
tap the screen to slide for a while and then release, the sequence of events was DOWN → MOVE → ..... → MOVE → UP
Seven Dian how to resolve the conflict View events
Reference answer:
there is a common development in the event of conflict ScrollView
with the RecyclerView
slide conflict, RecyclerView
embedded while sliding in the same direction
Slide the conflict processing rules:
- Conflict due to external slide and slide inside the sliding direction caused by the inconsistency, who may be determined according to the direction of sliding intercept events.
- For sliding conflict due to external and internal sliding direction of the sliding direction of the same cause, according to business needs dictate when to let external events View interception, when intercepted by an internal event View.
- For nested above two cases, relatively complex, can also be found breakthrough in business on demand.
Slide implementation of conflict:
- External intercept method: finger tap events are first processed through the interception of the parent container, if the parent container need this event to intercept, or not intercepted.
Specific methods: parent container needs to be rewrittenonInterceptTouchEvent
method, accordingly to intercept inside. - Internal intercept method: refers to the parent container does not intercept any event, and all events are passed to the child container, the container needs child if this event directly consumed, otherwise the process will be handed over to the parent container.
Specific methods: need to meetrequestDisallowInterceptTouchEvent
Methods
Eight Dian Scroller is how to achieve resilient slide View,
Reference answer:
- In the
MotionEvent.ACTION_UP
call when the event triggerstartScroll()
method, this method does not perform the actual operation of the slide, but the records related to the amount of sliding (sliding distance, sliding time) - Next call
invalidate/postInvalidate()
method View redraw request, the method is executed cause View.draw - It will be called when the View redraw the draw method
computeScroll
method, andcomputeScroll
will whereaboutsScroller
to get the currentscrollX
andscrollY
; thenscrollTo
implement a sliding method; then they call thepostInvalidate
approach to the second redraw, and processes as before, and so forth continue to lead View slide a small margin, but many times a slight slip on the formation of elastic slide, slide through to the end until the entire
Dian difference nine invalidate () and postInvalidate () is?
invalidate()
And postInvalidate()
are used to refresh the View, the main difference is invalidate()
called in the main thread, if used in the child thread needs to cooperate handler; and postInvalidate()
can be called directly in the child thread.
The difference between ten and SurfaceView View Dian's?
- View picture needs to be refreshed in the UI thread, and
SurfaceView
can refresh the page in the child thread - View update applies to the case of the initiative, and
SurfaceView
is suitable for passive updates, such as frequent refresh, because if you use View frequently refresh will block the main thread, leading to interface Caton -
SurfaceView
At the bottom has been achieved double buffering mechanism, while View is not, and thereforeSurfaceView
more suitable for frequent refresh, refresh the data when processing a large amount of pages (such as video player interface)
The difference between eleven Dian scrollTo () and scollBy () is?
-
scollBy
内部调用了scrollTo
,它是基于当前位置的相对滑动;而scrollTo
是绝对滑动,因此如果使用相同输入参数多次调用scrollTo
方法由于 View 的初始位置是不变的,所以只会出现一次 View 滚动的效果 - 两者都只能对 View 内容的滑动,而非使 View 本身滑动。可以使用
Scroller
有过度滑动的效果
十二丶自定义View 如何考虑机型适配?
- 合理使用 warp_content,match_parent
- 尽可能的是使用 RelativeLayout
- 针对不同的机型,使用不同的布局文件放在对应的目录下,android 会自动匹配。
- 尽量使用点 9 图片。
- 使用与密度无关的像素单位 dp,sp
- 引入 android 的百分比布局。
- 切图的时候切大分辨率的图,应用到布局当中。在小分辨率的手机上也会有很好的显示效果。
十三丶View 的滑动方式
a. layout(left,top,right,bottom)
:通过修改 View 四个方向的属性值来修改 View 的坐标,从而滑动 View
b. offsetLeftAndRight() offsetTopAndBottom()
:指定偏移量滑动 view
c. LayoutParams
,改变布局参数:layoutParams
中保存了 view 的布局参数,可以通过修改布局参数的方式滑动 view
d. 通过动画来移动 view:注意安卓的平移动画不能改变 view 的位置参数,属性动画可以
e. scrollTo/scrollBy
:注意移动的是 view 的内容,scrollBy(50,50)
你会看到屏幕上的内容向屏幕的左上角移动了,这是参考对象不同导致的,你可以看作是它移动的是手机屏幕,手机屏幕向右下角移动,那么屏幕上的内容就像左上角移动了
f. scroller:scroller
需要配置 computeScroll
方法实现 view 的滑动,scroller
本身并不会滑动 view,它的作用可以看作一个插值器,它会计算当前时间点 view 应该滑动到的距离,然后 view 不断的重绘,不断的调用 computeScroll
方法,这个方法是个空方法,所以我们重写这个方法,在这个方法中不断的从 scroller
中获取当前 view 的位置,调用 scrollTo
方法实现滑动的效果