ViewOverlay: When, How and for What Purpose?
来源:互联网 发布:ibatis源码下载 编辑:程序博客网 时间:2024/06/05 00:45
我的总结
In Android 4.3, a new feature for developer has been added: Overlays. When announcing this feature, the first thing I did was looking at the source code to understand how it works. Right after, I tried to find good use cases to fully exploit the potential of this feature. So in this post, I’m going to tell you more about this useful feature.
Obviously, you must install the following apk on Android 4.3
Overlays, how it works ?
In a few words
The official documentation about overlays is quite clear and you should read it. For more details, you can read this Chet Haase’s post.
Important points to remember about overlays:
- two types of overlays : ViewOverlay (for Views) and ViewGroupOverlay (for ViewGroups)
- two methods to retrieve a view overlay :
View.getViewOverlay
/ViewGroup.getViewGroupOverlay
- you can add/remove a drawable in ViewOverlay and ViewGroupOverlay
- you can add/remove a view in ViewGroupOverlay
- the documentation says
An overlay [...] on top of a View [...] drawn after all other content in that view [...]
- Chet Haase’s said
[...] you are responsible for positioning/sizing the views/drawables where you want them in the overlay [...]
Reading the source code
Let’s start by looking at the View.getViewOverlay
/ View.getViewGroupOverlay
methods.
A ViewOverlay/ViewGroupOverlay is created when the get method is called.
How ViewOverlay/ViewGroupOverlay works? Note that a ViewGroupOverlay extends ViewOverlay, so we can focus on the ViewOverlay implementation
ViewOverlay.java
ViewOverlay implementation is based on a OverlayViewGroup (static inner class in ViewOverlay). The OverlayViewGroup extends ViewGroup and contains a list of drawables. As you can see, there isn’t any method to layout drawables or child views that’s why you are responsible for positioning/sizing them.
Two more interesting things :
- the documentation says
view will be repositioned such that it is in the same relative location inside the activity
.
- all invalidation calls on a OverlayViewGroup are forwarded to its host view
because the overlay is not a child of the host view and invalidation cannot therefore follow the normal path up through the parent hierarchy
Two real use cases
I underlined the word real because I didn’t want to explain you how to use Overlay with irrelevant examples. That’s why, I’m going to present you two problems solved thanks to Overlays : a button with a banner and how to animate a view deletion.
A button with a banner (ViewOverlay)
Here we want to add a banner with some text on top-right corner of a button as you can see below.
The main difficulty is that we don’t want the button’s behaviour to be changed by the banner (i.e. click, touch etc.).
A basic xml layout
Here we have a basic layout with one button.
code snippetA custom banner drawable
For more flexibility (change the banner color, text etc), you have to implement a custom drawable in order to draw a banner.
I’m not gonna explain how to draw a banner because this is not the main purpose of this post. If you have questions, don’t hesitate to ask me. So, here is the custom drawable.
code snippetFinally, use a ViewOverlay
This last step is quite simple. It consists in sizing and positioning a BannerDrawable and to add it to the button’s ViewOverlay when the button has been fully laid out.
Note that the drawable bounds is the top right square of the button.
code snippetThis is piece of cake, isn’t it ?
Animating a view deletion (ViewGroupOverlay)
This second use case is a bit more complicated. We’re gonna animate the deletion of a view (i.e. Button) in a parent (i.e. LinearLayout) with a eyecatching approach.
If you still have not understand the use case I want to explain to you, you can take a look on the animated gif below.
Step1: fill the parent
Assume that we have an xml layout with an empty LinearLayout (id=buttons
) inside a ScrollView. We create 10 buttons inside the LinearLayout:
Each button show a toast onClick and is deleted onLongClick.
Step2: animate the deletion 难道ViewGroupOverlay只是为了在移除view的时候 还是删除这个view以后 播放动画??
On delete, we scale up the button and decrease the alpha property. To achieve that, we use multiple ObjectAnimators played together thanks to an AnimatorSet.
Before starting the animation, don’t forget to add the button to the ViewGroupOverlay of the top parent (i.e. the activity main view) in order not to clip the button in the ViewGroupOverlay by its direct parent (i.e. LinearLayout).
Finally, when the animation ends or is cancelled, remove the button from the ViewGroupOverlay.
It gives us the following method
animatedDelete(Button)And this is it!
Conclusion
I’ve shown you two real examples where using Overlay is clearly an appropriated method. The only serious disadvantage is that Overlays are only available since API level 18.
ViewGroupOverlay (view source)
extends ViewOverlayClass Overview
A group overlay is an extra layer that sits on top of a ViewGroup (the "host view") which is drawn after all other content in that view (including the view group's children). Interaction with the overlay layer is done by adding and removing views and drawables.
ViewGroupOverlay is a subclass of ViewOverlay
, adding the ability to manage views for overlays on ViewGroups, in addition to the drawable support in ViewOverlay.
See Also
getOverlay()
Summary
Public Methods
public void add (View view)
Adds a View to the overlay. The bounds of the added view should be relative to the host view. Any view added to the overlay should be removed when it is no longer needed or no longer visible.
Views in the overlay are visual-only; they do not receive input events and do not participate in focus traversal. Overlay views are intended to be transient, such as might be needed by a temporary animation effect.
If the view has a parent, the view will be removed from that parent before being added to the overlay. Also, if that parent is attached in the current view hierarchy, the view will be repositioned such that it is in the same relative location inside the activity. For example, if the view's current parent lies 100 pixels to the right and 200 pixels down from the origin of the overlay's host view, then the view will be offset by (100, 200).
Parameters
See Also
remove(View)
add(Drawable)
public void remove (View view)
Removes the specified View from the overlay.
Parameters
See Also
add(View)
remove(Drawable)
- ViewOverlay: When, How and for What Purpose?
- what does router do and its purpose?
- What is Called and When
- When and How to Micromanage
- The what, when and why of wireframes
- General-purpose and introductory examples for the scikit.
- C#设计模式(What,How,When,Why)
- Java Native Interface: Who When Where What Why How
- What is Observer and Observable and when we used these?
- What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations in Android?
- When and how to use a ThreadLocal
- Developers should know How, What and Why
- What and How to use JSF
- camera flicker--- what why and how
- SCN – What, why, and how?
- only for test purpose
- purpose for urjnasw xkfjjkn
- what’s a virtual destructor and when is it needed?
- OpenStack 2014 用户调查解析——中国部署规模仅次于美国
- LaTeX制作动图效果
- 如何学好网络博客推广技术
- 程矢Axure夜话:中继器系列视频教程之中继器表格删除数据2
- 在android C/C++ native编程(ndk)中使用logcat
- ViewOverlay: When, How and for What Purpose?
- 程矢Axure夜话:中继器系列视频教程之中继器表格删除数据
- 大苏打撒旦撒旦撒旦啊撒旦
- const A& fun(const A& a) const {} 的理解
- 此主题相关图片如下
- WebService大数据量时异常
- session与cookie的区别
- 程矢Axure夜话:中继器系列视频教程之中继器表格奇偶行换色2
- 025 替换字符串中的空格(keep it up)