如何在一切之上绘制视图? [英] How to draw a view on top of everything?

查看:112
本文介绍了如何在一切之上绘制视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以在屏幕上创建当前正在显示的任何内容的通知的应用。类似于Go SMS消息弹出窗口或类似ChatHead的图片如下图所示:

I want to make an app that can create notification on the screen on top of anything that is currently being displayed. Something like the Go SMS message popup or something like the ChatHead in the following picture:

如果可以动态绘制它包括触摸事件会更好。传统或标准的方法是什么?

It would be even better if it is possible to draw it dynamically including touch events.What is the conventional or standard way to do this?

示例:

类似于可以点击或拖动的图标,无论您是在主屏幕还是应用程序抽屉或其他应用程序。注意张贴图片中屏幕边缘附近的圆形图标。您可以在任何应用程序中的任何位置拖动它们。

Like an Icon that can be clicked or dragged no matter whether you are on home screen or app drawer or other apps.Pay attention to the circular icons near the edges of the screen in the picture posted. You can drag them anywhere in any app.

推荐答案

以下是Toast和对话框窗口的工作原理:

Here is how things like Toast and dialog windows work:

如果只是添加或带到前面不起作用,请说当你有服务时将自己的视图添加到另一个客户端活动或应用程序(FaceUnlock这样做),或者你不能依赖于层次结构,您需要使用窗口管理器和窗口令牌来完成这一操作。然后,您可以像以前一样创建布局并利用动画和硬件加速。

In the case where just adding or bringing to front does not work, say when you are having a service add its own view to another client activity or application (FaceUnlock does this), or you cannot depend on hierarchies, you need to use the window manager and a window token to do the trick. You can then create layouts and take advantage of animations and hardware acceleration as before.

    WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);

    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.FIRST_SUB_WINDOW);
    layoutParams.width = 300;
    layoutParams.height = 300;

    layoutParams.format = PixelFormat.RGBA_8888;
    layoutParams.flags =
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
    layoutParams.token = getWindow().getDecorView().getRootView().getWindowToken();

    //Feel free to inflate here
    mTestView = new View(this);
    mTestView.setBackgroundColor(Color.RED);

    //Must wire up back button, otherwise it's not sent to our activity
    mTestView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                onBackPressed();
            }
            return true;
        }
    });
    windowManager.addView(mTestView, layoutParams);

然后一定要删除视图onDestroy(或onPause),否则你将崩溃

Then be sure to remove the view onDestroy (or onPause) or you will crash

   if (mTestView != null) {
       WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
       if (mTestView.isShown()) {
            windowManager.removeViewImmediate(mTestView);
       }
   }

这篇关于如何在一切之上绘制视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆