Android — 制作悬浮窗口

异步冰川
• 阅读 3584

获取 WindowManager

mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

初始化 Params

mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE,
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);

设置 Params 的其他属性

// Set gravity.
mLayoutParams.gravity = Gravity.TOP | Gravity.START;
// Adjust origin position by base point.
mLayoutParams.x = 0;
mLayoutParams.y = 0;
// Adjust origin position by margin.
mLayoutParams.verticalMargin = LAYOUT_PARAMS_VERTICAL_MARGIN;
mLayoutParams.horizontalMargin = LAYOUT_PARAMS_HORIZONTAL_MARGIN;
// Set layout height with constant.
mLayoutParams.height = LAYOUT_PARAMS_HEIGHT;
// The scale is decided by origin icon scale.
mLayoutParams.width = LAYOUT_PARAMS_HEIGHT * LAYOUT_PARAMS_SCALE;

实例化 View

mFloatingView = new ImageView(context);
mFloatingView.setImageResource(iconResId);
mFloatingView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // Your code here.
    }
});

显示悬浮窗口

mWindowManager.addView(mFloatingView, mLayoutParams);

消除悬浮窗口

if (mFloatingView != null && mFloatingView.isShown()) {
    mWindowManager.removeView(mFloatingView);
}

完整代码

/**
 * Created by YuYi App Dev on 2016/8/22.
 * To create a floating button.
 */
public class FloatingBackButton {

    private Context context;
    private int iconResId;
    private Class<?> cls;

    private ImageView mFloatingView;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mLayoutParams;

    /**
     * Constructor function.
     *
     * @param context   the context
     * @param iconResId the pic needed to be displayed
     * @param cls       the class needed to be loaded when button pressed
     */
    public FloatingBackButton(Context context, int iconResId, Class<?> cls) {
        this.context = context;
        this.iconResId = iconResId;
        this.cls = cls;
        initResources();
    }

    private void initResources() {

        // Set as local var to save res.
        final int LAYOUT_PARAMS_HEIGHT = 120;
        final int LAYOUT_PARAMS_SCALE = 2;
        final int LAYOUT_PARAMS_VERTICAL_MARGIN = 0;
        final int LAYOUT_PARAMS_HORIZONTAL_MARGIN = 0;

        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        // Set gravity.
        mLayoutParams.gravity = Gravity.TOP | Gravity.START;
        // Adjust origin position by base point.
        mLayoutParams.x = 0;
        mLayoutParams.y = 0;
        // Adjust origin position by margin.
        mLayoutParams.verticalMargin = LAYOUT_PARAMS_VERTICAL_MARGIN;
        mLayoutParams.horizontalMargin = LAYOUT_PARAMS_HORIZONTAL_MARGIN;
        // Set layout height with constant.
        mLayoutParams.height = LAYOUT_PARAMS_HEIGHT;
        // The scale is decided by origin icon scale.
        mLayoutParams.width = LAYOUT_PARAMS_HEIGHT * LAYOUT_PARAMS_SCALE;
        // Must initiating when attaching, or will cause crash.
        mFloatingView = new ImageView(context);
        mFloatingView.setImageResource(iconResId);
        mFloatingView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, cls);
                context.startActivity(intent);
                if (mFloatingView != null && mFloatingView.isShown()) {
                    mWindowManager.removeView(mFloatingView);
                }
            }
        });
    }

    public void show() {
        mWindowManager.addView(mFloatingView, mLayoutParams);
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
刘望舒 刘望舒
4年前
Android解析WindowManager(三)Window的添加过程
Android框架层Android系统服务WindowManagercategories:Android框架层本文首发于微信公众号「刘望舒」前言在此前的系列文章中我们学习了WindowManager体系和Window的属性,这一篇我们接着来讲Window的添加过程。建议阅读此篇文章前先阅读本系列的前两篇文章。<!more1.概述WindowMana
刘望舒 刘望舒
4年前
Android解析WindowManagerService(一)WMS的诞生
Android框架层Android系统服务WindowManagerServiceAndroid框架层本文首发于微信公众号「后厂技术官」前言此前我用多篇文章介绍了WindowManager,这个系列我们来介绍WindowManager的管理者WMS,首先我们先来学习WMS是如何产生的。本文源码基于Android8.0,与Android7.1.2
刘望舒 刘望舒
4年前
Android解析WindowManager(一)WindowManager体系
Android框架层Android系统服务WindowManagercategories:Android框架层本文首发于微信公众号「刘望舒」前言WindowManagerService(WMS)和AMS一样,都是Android开发需要掌握的知识点,同样的,WMS也很复杂,需要多篇文章来进行讲解,为何更好的理解WMS,首先要了解WindowManage
刘望舒 刘望舒
4年前
Android解析WindowManager(二)Window的属性
Android框架层Android系统服务WindowManagercategories:Android框架层本文首发于微信公众号「刘望舒」前言在上一篇文章我们学习了WindowManager体系,了解了Window和WindowManager之间的关系,这一篇我们接着来学习Window的属性。<!more1.概述上一篇文章中我们讲过了Window
九路 九路
3年前
Android窗口管理框架:Android应用视图的管理者Window
Android窗口管理框架:Android应用视图的管理者Window文章目录一窗口类型二窗口参数三窗口模式四窗口回调五窗口实现从这篇文章开始,我们来分析和Window以及WindowManager相关的内容,Abstractbaseclassforatoplevelwindowlookandbehaviorpolic
Easter79 Easter79
3年前
TextureView onSurfaceTextureAvailable回调不执行
TextureView必须工作在硬件加速条件,否则什么都不执行.因为需要 android:hardwareAccelerated”true”或者 Windowwactivity.getWindow();w.setFlags(WindowManager.LayoutParams.FLAG\_HARDWARE\_ACCELERATED);
Wesley13 Wesley13
3年前
Android动态Java代码调整window大小
Android调整window大小举一个例子,设置当前的APP所需要的屏幕高度为设备高度的一半:WindowwindowgetActivity().getWindow();WindowManager.LayoutParamswindowLayoutParamswindow.ge
Wesley13 Wesley13
3年前
Unity横屏
Android下发现Unity里面的Player设置,并不能完全有效,比如打开了自动旋转,启动的时候还是会横屏,修改XML添加以下代码<applicationandroid:icon"@drawable/ic\_launcher"                    android:label"@string/app\_name"
Stella981 Stella981
3年前
Pre
PAT甲级1119,我先在CSDN上面发布的这篇文章:https://blog.csdn.net/weixin\_44385565/article/details/89737224(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fweixin_443855
美凌格栋栋酱 美凌格栋栋酱
5个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(