带动画的窗口管理器
首先感谢所有尝试回答这个话题的人。
我有一个活动,我想在屏幕顶部显示一种菜单,我使用窗口管理器来处理这个问题。这是我遇到的UI问题,为什么我选择windowmanager来做这样的菜单。但现在我希望这个菜单有动画效果,但动画似乎没有效果。这是我的代码。
如果有人知道如何为窗口管理器设置动画,我将不胜感激。
Animation animShowTopLine;
animShowTopLine = AnimationUtils.loadAnimation(this, R.anim.translate);
animShowTopLine.reset();
LinearLayout top_line;
WindowManager wm;
WindowManager.LayoutParams wmParams;
LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
top_line = (LinearLayout) inflate.inflate(R.layout.line, null);
wm =(WindowManager) getApplicationContext().getSystemService("window");
wmParams =new WindowManager.LayoutParams();
wmParams.type=2002;
wmParams.format = 1;
wmParams.flags=40;
wmParams.width=WindowManager.LayoutParams.FILL_PARENT;
wmParams.height=WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.gravity = Gravity.TOP;
wm.addView(top_line, wmParams);
top_line.startAnimation(animShowTopLine);
提前致谢。问候。
First of all thanks everyone who tries to reply this topic.
I have an activity and I wanted to show a sort of menu at the top of the screen and I used windowmanager to handle this. it was about UI issues I encountered why I choise windowmanager to do such a menu. But for now I want this menu to animate but it seems animation takes no effect. Here is my code.
If anyone has any idea how to animate windowmanager I ll be appreciate.
Animation animShowTopLine;
animShowTopLine = AnimationUtils.loadAnimation(this, R.anim.translate);
animShowTopLine.reset();
LinearLayout top_line;
WindowManager wm;
WindowManager.LayoutParams wmParams;
LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
top_line = (LinearLayout) inflate.inflate(R.layout.line, null);
wm =(WindowManager) getApplicationContext().getSystemService("window");
wmParams =new WindowManager.LayoutParams();
wmParams.type=2002;
wmParams.format = 1;
wmParams.flags=40;
wmParams.width=WindowManager.LayoutParams.FILL_PARENT;
wmParams.height=WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.gravity = Gravity.TOP;
wm.addView(top_line, wmParams);
top_line.startAnimation(animShowTopLine);
Thanks in advance. Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,为了您自己的理智,请不要硬编码一堆这样的常量。窗口管理器服务的名称是
Context.WINDOW_SERVICE
。窗口类型为WindowManager.LayoutParams.TYPE_PHONE
。您设置的标志是...嗯...WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
(说真的,也是用十进制写的??)。格式为PixelFormat.RGBA_8888
(我强烈建议使用PixelFormat.TRANSLUCENT
)。现在,首先,人们到底在使用
TYPE_PHONE
做什么?你不想那样做。如果您希望窗口位于主窗口之上,则应该使用TYPE_APPLICATION
。事实上,我强烈建议您只使用Dialog
来实现此目的,并将其附加窗口设置为您想要的。它将处理使用窗口管理器的所有细节,并且不会以任何方式限制您如何为其设置动画。我认为你在这里遇到的主要问题是你试图为窗口的根视图设置动画。根视图有些特殊——它定义了窗口的最顶部部分,与窗口完全匹配,并且总是强制为窗口的大小。它是驱动子窗口布局的因素。如果您想做视图动画,您应该单独保留根视图(它是窗口的锚点)并为其内部的元素设置动画。
也就是说,由于您使用的是旧式动画,因此有一种更好的方法来对整个窗口进行动画处理,即系统对对话框和活动进行动画处理等方式:在窗口的布局参数中设置动画。然后窗口管理器将按照您的指定将该动画应用到整个窗口表面。这比在窗口内部执行更有效,因为动画的每一帧只需要使用新的动画转换重新合成屏幕,而不是重新绘制窗口内容然后重新合成。
您可以通过设置 WindowManager.LayoutParams.windowAnimations 来实现此目的。这是一个整数字段,它采用定义与窗口关联的各种动画的 style 资源的资源 ID。例如,用于标准对话框的样式是:
将
windowEnterAnimation
设置为在显示窗口时运行的动画资源,并将windowExitAnimation
设置为在窗口显示时运行的动画资源。它是隐藏的。如果未设置,则不会运行动画。因此,对于此处的代码,您只需创建一个对话框,将其内容设置为自定义内容,将其重力、宽度和高度设置为此处的值,并将其
windowAnimations
字段设置为您的样式定义动画。如果您想要一些与默认对话框不同的行为(不是触摸模式等),您还可以调整标志。Dialog.getWindow()
上的 API 拥有设置布局参数所需的一切。First, please for your own sanity don't hard-code a bunch of constants like that. The name of the window manager service is
Context.WINDOW_SERVICE
. The window type isWindowManager.LayoutParams.TYPE_PHONE
. The flags you have set is... ummm...WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
(seriously, written in decimal as well??). The format isPixelFormat.RGBA_8888
(and I would strongly recommend usingPixelFormat.TRANSLUCENT
instead).Now, first, what in the world are doing using
TYPE_PHONE
? You don't want to do that. If you want a window layered on top of your main window, you should be usingTYPE_APPLICATION
. In fact, I would urge you to just useDialog
for this and set its attached window to be what you want. It will take care of all the details of working with the window manager, and doesn't limit you in any way with how you can animate it.I think the main problem you have here is that you are trying to animate the root view of the window. The root view is somewhat special -- it defines the top-most part of the window, exactly matches the window, and is always forced to be the size of the window. It is what drives the layout of its child windows. If you want to do a view animation, you should leave the root view alone (it is the anchor of the window) and animate the elements inside of it.
That said, since you are using an old-style animation, there is a better way to animate full windows, the way that the system animates dialogs and activities and such: set the animation in the window's layout params. Then the window manager will apply that animation to the entire window surface as you specify. This is more efficient than doing it inside of the window because each frame of the animation only requires re-compositing the screen with the new animation transformation, instead of re-drawing the window contents and then re-compositing it.
You do this by setting
WindowManager.LayoutParams.windowAnimations
. This is an integer field that takes the resource id of a style resource defining the various animations associated with the window. For example, the style used for the standard dialogs is:You set
windowEnterAnimation
to the animation resource to run when the window is being shown, andwindowExitAnimation
to the one to run when it is hidden. If not set, no animation is run.So for your code here, you can just make a Dialog, set its content to your custom content, set its gravity, width and height to the values you have here, and set its
windowAnimations
field to your style defining the animations. You can also tweak the flags if you want some behavior different than a default dialog (not touch modal or such). The API onDialog.getWindow()
has everything you need to set the layout params.mParams.windowAnimations = android.R.style.Animation_Toast;
mParams.windowAnimations = android.R.style.Animation_Toast;
其中 android.R.style.Animation_Toast 是定义用于此窗口的动画的样式资源。这必须是系统资源,它不能是应用程序资源,因为窗口管理器无权访问应用程序。
其他有效的样式有:
Where android.R.style.Animation_Toast is a style resource defining the animations to use for this window. This must be a system resource, it can not be an application resource because the window manager does not have access to applications.
Others valid styles are: