ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

直播带货app源码如何制作全局悬浮窗?

2021-06-04 18:55:31  阅读:231  来源: 互联网

标签:layoutParams WindowManager app windowManager 悬浮 源码 context 带货


有些直播带货app中经常会用到一个全局可悬浮的按钮、或者窗口,今天我们就来介绍一下直播带货app源码中如何制作“可跟随手指拖动的全局悬浮窗”。
一、自定义一个跟随手指滑动的View
在直播带货app源码中自定义一个跟随手指滑动的View很简单,本篇的教程中只支持简单的悬浮拖动,后期可在此基础上进行功能扩展,挺简单的,不多说了,直接上代码:

public class FloatWindowImageView extends AppCompatImageView implements View.OnTouchListener {
    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private Boolean isMove = false;
    private float lastX;
//构造方法,初始化
    Public	FloatWindowImageView(WindowManager windowManager,WindowManager.LayoutParams layoutParams,Context context) {
        super(context);
        this.windowManager = windowManager;
        this.layoutParams = layoutParams;
        setOnTouchListener(this);
        setOnClickListener(v	->	Toast.makeText(context,"点击了",Toast.LENGTH_LONG).show());
        requestSettingCanDrawOverlays();
    }
 
    public FloatWindowImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public FloatWindowImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
	//在直播带货app源码中设置拖动事件
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getRawX();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                isMove = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(x - lastX) > ViewConfiguration.get(getContext()).getScaledTouchSlop()){
                    isMove = true;
                    layoutParams.x = (int) (event.getRawX() - getWidth() / 2);
                    layoutParams.y = (int) (event.getRawY() - getHeight() / 2);
//更新窗口位置
                    windowManager.updateViewLayout(this,layoutParams);
                }
                return true;
                case MotionEvent.ACTION_UP:
                    if (isMove){
                        return true;
                    }
        }
        lastX = x;
        return super.onTouchEvent(event);
    }
 
// 申请悬浮权限
    private void requestSettingCanDrawOverlays() {
        try {
            //判断当前系统版本
            if (Build.VERSION.SDK_INT >= 23) {
                //判断权限是否已经申请过了(加上这个判断,则使用的悬浮窗的时候;如果权限已经申请则不再跳转到权限开启界面)
                if (!Settings.canDrawOverlays(getContext())) {
                    //申请权限
                    Intent intent2 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                    ((Activity)getContext()).startActivityForResult(intent2, 1001);
                } else {
                    //创建悬浮窗
                    windowManager.addView(this, layoutParams);
                }
            } else {
                windowManager.addView(this, layoutParams);
            }
            System.out.println("Build.VERSION.SDK_INT::::" + Build.VERSION.SDK_INT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
// 申请完权限后显示出来
    public void showFloatWindow(){
        windowManager.addView(this, layoutParams);
    }
}

按上面的直播带货app源码,就基本实现了全局的悬浮效果,接下来就是在Activity中使用了:

WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
//根据Android版本的不同,需要设置不同的type,不设置的话,有些手机系统会报错
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0
                    layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                } else {
                    layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
                }
                layoutParams.format = PixelFormat.TRANSLUCENT;
                layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//设置窗口的起始位置
                layoutParams.x = 0;
                layoutParams.y = 0;
                layoutParams.gravity = Gravity.START | Gravity.TOP;
//设置悬浮窗口的大小
                layoutParams.width = 200;
                layoutParams.height = 200;
                imageView = new FloatWindowImageView(windowManager,layoutParams,this);
//设置图片
                imageView.setImageResource(R.drawable.weixin);

以上就是在直播带货app源码中实现一个简单的悬浮窗口,如果想实现更多的功能窗口,可以在此基础上进行改造。

声明:以上内容为csdn作者:云豹直播官方 原创,未经公司同意,禁止转载,否则将追究相关法律责任

标签:layoutParams,WindowManager,app,windowManager,悬浮,源码,context,带货
来源: https://blog.51cto.com/yunbaokj/2863972

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有