In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to modify the default sequence drawing sub-View of ViewGroup". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to modify the default sequence drawing sub-View of ViewGroup".
Item processing of TV App
Modify the drawing order of View, which is basically not needed in daily development. Most of the UI designs of App on mobile phones adopt the idea of flattening, except for some very special custom View, in most cases, we do not need to consider the default drawing order of View.
It is also easy to understand that normally, the View added after the ViewGroup should visually overwrite the previous View.
But there is one scene design, which is very special, and that is Android TV App.
In the design of TV, because of the need for key control of the remote control, in order to enrich the visual experience, it is necessary to deal with the change of focus state of View.
For example: the focus of the ItemView whole highlight, zoom in and add a shadow, are very common designs.
Then this brings a problem, normally we use RecyclerView to achieve the list effect, when the spacing between Item is too small, a single Item will be masked when it is magnified.
For example, as shown in the image above, a common focus is on a highlighted design, but it is obscured by the View behind it.
How can such a situation be solved?
I clapped my head and thought, since the spacing is too small, let's just widen the spacing. Modify an attribute to solve a requirement, and the designer cries and faints on the station.
However, there are some design effects, and the spacing is enough, so there is no covering phenomenon.
But we can't just change the spacing to solve the problem. In most cases, the designer doesn't leave us much spacing.
Since there is no escape, let's study how to solve it.
Modify the principle of drawing order
Changing the drawing order is actually very simple. Android has set aside an extension point for us.
We know that ViewGroup stores the child View through its member mChildren array. In the dispatchDraw () method loop of the ViewGroup drawing child View, the index is not directly used to fetch values from the mChildren array.
Override protected void dispatchDraw (Canvas canvas) {/ /. Final ArrayList preorderedList = usingRenderNodeProperties? Null: buildOrderedChildList (); final boolean customOrder = preorderedList = = null & & isChildrenDrawingOrderEnabled (); for (int I = 0; I
< childrenCount; i++) { // ... final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); // 并非直接从 mChildren 中获取 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { more |= drawChild(canvas, child, drawingTime); } } // ... } 可以看到,child 并非是从 mChildren 中直取,而是通过 getAndVerifyPreorderedView() 获得,它的参数除了 children 外,还有一个 preorderedList 的 ArrayList,及子 View 的索引。 private static View getAndVerifyPreorderedView(ArrayList preorderedList, View[] children, int childIndex) { final View child; if (preorderedList != null) { child = preorderedList.get(childIndex); if (child == null) { throw new RuntimeException("Invalid preorderedList contained null child at index " + childIndex); } } else { child = children[childIndex]; } return child; } 在其中,若 preorderedList 不为空,则从其中获取子 View,反之则还是从 children 中获取。 回到前面 dispatchDraw() 中,这里使用的 preorderedList 关键列表,来自 buildOrderedChildList(),在方法中通过 getAndVerifyPreorderedIndex() 获取对应子 View 的索引,此方法需要一个 Boolean 类型的 customOrder,即表示是否需要自定义顺序。 ArrayList buildOrderedChildList() { // ... final boolean customOrder = isChildrenDrawingOrderEnabled(); for (int i = 0; i < childrenCount; i++) { // add next child (in child order) to end of list final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); final View nextChild = mChildren[childIndex]; final float currentZ = nextChild.getZ(); // insert ahead of any Views with greater Z int insertIndex = i; while (insertIndex >0 & & mPreSortedChildren.get (insertIndex- 1). GetZ () > currentZ) {insertIndex--;} mPreSortedChildren.add (insertIndex, nextChild);} return mPreSortedChildren;}
The logic of buildOrderedChildList () is to adjust the children order according to the Z axis. If the Z axis value is the same, refer to the configuration of customOrder.
The sub-View,Z values in ViewGroup are usually the same, so the key parameter is the customOrder switch.
You know from the code that customOrder is obtained through the isChildrenDrawingOrderEnabled () method, which corresponds to the fact that setChildrenDrawingOrderEnabled () can set the value of customOrder.
In other words, if we want to adjust the order, we only need to adjust it in two steps:
Call setChildrenDrawingOrderEnable (true) to turn on the custom drawing order
Override getChildDrawingOrder () to modify the value index of View
Example
Finally, we write a Demo and rewrite the getChildDrawingOrder () method of RecycleView to achieve the View final drawing that gets the focus.
@ Override protected int getChildDrawingOrder (int childCount, int I) {View view = getLayoutManager (). GetFocusedChild (); if (null = = view) {return super.getChildDrawingOrder (childCount, I);} int position = indexOfChild (view); if (position < 0) {return super.getChildDrawingOrder (childCount, I);} if (I = = childCount-1) {return position;} if (I = position) {return childCount-1;} return super.getChildDrawingOrder (childCount, I) }
Don't forget to call setChildrenDrawingOrderEnabled (true) to turn on the custom drawing order.
At this point, when the focus is enlarged, it will not be obscured by other View.
Thank you for your reading, the above is the content of "how to modify the default sequence drawing sub-View of ViewGroup". After the study of this article, I believe you have a deeper understanding of how to modify the default sequence drawing sub-View of ViewGroup, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.