Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How android customizes controls

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to customize the control of android. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

In the custom control, in addition to the api for measuring layout and drawing involved in the drawing process, some api are also used frequently. Here, Uncle makes a summary and asks students to supplement or correct them, so that Uncle also rises his posture.

Inflate

The inflate method is often used to parse an xml layout file, and is often used in custom composite controls, with poses such as:

View.inflate (context, resource, root) LayoutInflater.from (getContext ()) .propagate (resource, root)

In fact, View.inflate still calls LayoutInflater to parse a xml:

Public static View inflate (Context context, int resource, ViewGroup root) {LayoutInflater factory = LayoutInflater.from (context); return factory.inflate (resource, root);}

So there is no difference between the two poses. Let's discuss the return value of inflate (resouce, root). The parameter resource is the layout resource and root is a root node passed in. If a null,inflate is passed into the root, the xml corresponding to the resource will be parsed and the root node in the xml will be returned. If the root is not null,inflate, the xml layout will be parsed and added to the root node root, and then the root node root will be returned.

There is also an inflate method with three parameters:

Inflate (int resource, ViewGroup root, boolean attachToRoot)

There is an extra parameter attachToRoot. If root is null, the root node in the parsed xml layout is returned. If the root is not null,attachToRoot, the xml layout is parsed and added to the root node root, and then the root node root is returned. If root is not null,attachToRoot, false,inflate will parse the xml layout but will not add it to the root node root, and then return the root node in the parsed xml layout. In this case, root only provides the attributes of layout parameters for the root node in xml, because the root node in xml does not know who its parent container is, so if no one provides it, its layout parameters will be invalid.

OnFinishInflate

OnFinishInflate is a call when all the children have finished parsing. For example, if we customize a ViewGroup and want to find the child to make some settings, if we go to findViewById in the constructor of the custom ViewGroup, a null will be returned, because the child has not yet been parsed, that is, it has not been born yet. At this time, we can overwrite onFinishInflate, and then go to find after the child has parsed it.

RequestLayout

There is a lot about requestLayout, and the requestLayout () method triggers the measure procedure and the layout procedure, does not call the draw procedure, and does not redraw any View, including the caller itself.

OnSizeChange (int w, int h, int oldw, int oldh)

OnSizeChange is called when the size of the control changes, and its call trajectory is layout- > setFrame- > sizeChange- > onSizeChange. It is sure to be called when the control is laid out, and we can override this method to get the size of the control. So this method is usually used to initialize member variables related to the size of the control.

Invalidate

Invalidate is used so frequently that it triggers the redrawing of the View, that is, the draw process of the drawing process, but does not call the measurement and layout process.

PostInvalidate

We all know that Android's UI is a single-threaded model, and UI can only be updated in the main thread, so we can only call invalidate in the main thread. If you want to update the ui in the child thread, you can use handler to send a msg to the main thread, and then call invalidate when processing the msg. In addition, we can directly call postInvalidate to update the UI,postInvalidate internal implementation in the child thread and also use handler to send msg to the main thread and then call invalidate.

SetWillNotDraw

Custom ViewGroup usually does not draw itself. If you rewrite the draw method or onDraw method in ViewGroup, you will find that they will not be called at all. But if you set a background for your ViewGroup, you will find that both the draw method and the onDraw method are gone again.

We know that ViewGroup itself is a View, and its drawing is initiated by its parent container, and the specific location is the drawChild method in ViewGroup:

Protected boolean drawChild (Canvas canvas, View child, long drawingTime) {return child.draw (canvas, this, drawingTime);}

Note that the draw method here takes three parameters, which is different from the draw method we usually talk about with one parameter. Find the draw method with three parameters in the View class and find a piece of code in it:

If (! hasDisplayList) {/ / Fast path for layouts with no backgrounds if ((mPrivateFlags & PFLAG_SKIP_DRAW) = = PFLAG_SKIP_DRAW) {mPrivateFlags & = ~ PFLAG_DIRTY_MASK; dispatchDraw (canvas);} else {draw (canvas);}}

From this we can see a clue, usually a ViewGroup will skip drawing by default, that is, (mPrivateFlags & PFLAG_SKIP_DRAW) = = PFLAG_SKIP_DRAW will return a true, then it will directly take the dispatchDraw method to draw its own child, and will not call draw (canvas) with a parameter, but when the ViewGroup has a background or setWillNotDraw (false), it will take the draw (canvas) method. So if we customize a ViewGroup and want to implement its own drawing, we can set a background for it or call setWillNotDraw (false).

OnAttachedToWindow

OnAttachedToWindow is called when a View is bound to window, and according to the comments on this method in the View class, onAttachedToWindow is definitely called before the onDraw method.

In the custom control, we can register some broadcast receivers in onAttachedToWindow, observer or open some tasks, you can refer to the implementation of TextClock.

OnDetachedFromWindow

OnDetachedFromWindow corresponds to onAttachedToWindow, which is a call when a View is removed from the window. If you register some listeners in onAttachedWindow, you usually have to unregister them in onDetachedFromWindow.

ViewTreeObserver

ViewTreeObserver is the observer of the view tree, listening for some global changes of the view tree, including the layout of the whole view tree, the beginning of drawing, the change of touch mode, and so on. We can't initialize the object of ViewTreeObserver directly, we need to get it through getViewTreeObserver ().

ViewTreeObserver.OnGlobalLayoutListener

When the global layout changes in a view tree or the visual state of a view in the view tree changes, the general posture is:

GetViewTreeObserver () .addOnGlobalLayoutListener (new OnGlobalLayoutListener () {@ Override public void onGlobalLayout () {getViewTreeObserver () .removeGlobalOnLayoutListener (this); / / do something you like / / for example, get view width or height height}})

ViewTreeObserver.OnPreDrawListener

When a view tree is about to be drawn, the general posture is:

GetViewTreeObserver () .addOnPreDrawListener (new OnPreDrawListener () {@ Override public boolean onPreDraw () {/ / do something before draw / / for example, request a new layout return true;}}) This is the end of this article on "how to customize controls for android". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report