安卓开发多个或俩个悬浮球一起悬浮窗

Posted Android记事苟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓开发多个或俩个悬浮球一起悬浮窗相关的知识,希望对你有一定的参考价值。

1. 主页面的设置,开启权限
//启动 计算器悬浮框 服务, 用于快速调出计算器。
public void startFloatingButtonService()
if (FloatingButtonService.isStarted)
return;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (!Settings.canDrawOverlays(this))
// ToastUtils.showShort( “当前无权限,请授权”);
Toast.makeText(this, “当前无权限,请授权”, Toast.LENGTH_SHORT).show();
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse(“package:” + getPackageName())), 0);
else
startService(new Intent(MainActivity.this, FloatingButtonService.class));


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    if (requestCode == 0) 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
            if (!Settings.canDrawOverlays(this)) 
                Toast.makeText(this, "权限获取失败! 计算器悬浮窗口将不可用", Toast.LENGTH_SHORT).show();
            else 
                // Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
                startService(new Intent(MainActivity.this, FloatingButtonService.class));
            
        
    

  1. 在AndroidManifest定义权限
    在 application 里加
    加下面这句话service


    3. 创一个FloatingButtonService extends Service 这里开始设置悬浮窗大小什么的
    public class FloatingButtonService extends Service
    public static boolean isStarted = false;
    int downTime ;//Button被按下时的时间
    int thisTime ;//while每次循环时的时间
    boolean onBtnTouch = false;//Button是否被按下
    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private Context context;
    private ImageView imageView,imageView1;
    LinearLayout mFloatLayout,layout1;
    @Override
    public void onCreate()
    super.onCreate();
    context = this;
    isStarted = true;
    windowManager = (WindowManager) getApplicationContext().getSystemService(getApplicationContext().WINDOW_SERVICE);
    layoutParams = new WindowManager.LayoutParams();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    else
    layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;

    layoutParams.format = PixelFormat.RGBA_8888;
    layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
    layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    layoutParams.width = 80;
    layoutParams.height = 160;
    layoutParams.x = 1200-layoutParams.width;
    layoutParams.y = 300;

    @Nullable
    @Override
    public IBinder onBind(Intent intent)
    return null;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    showFloatingWindow();

    return super.onStartCommand(intent, flags, startId);

    private void showFloatingWindow()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    if (Settings.canDrawOverlays(this))
    layout1=new LinearLayout(getApplicationContext());
    //获取布局界面
    LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
    mFloatLayout=(LinearLayout)inflater.inflate(R.layout.layout,null,false);
    layout1.addView(mFloatLayout);
    imageView = (ImageView) mFloatLayout.findViewById(R.id.close);
    imageView1 = (ImageView) mFloatLayout.findViewById(R.id.back);
    windowManager.addView(layout1,layoutParams);
    layout1.setOnTouchListener(new FloatingOnTouchListener());

            imageView.setOnTouchListener(new View.OnTouchListener() 
                @Override
                public boolean onTouch(View v, MotionEvent event) 
    
                    if (thisTime-downTime<2)
                       Intent intent = new Intent(FloatingButtonService.this, Main2Activity.class);
                       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                       startActivity(intent);
                
                 return false;
               
            );
        
    
    
    


    //设置点击 移动 松开等事件
    private class FloatingOnTouchListener implements View.OnTouchListener
    private int x;
    private int y;

    @Override
    public boolean onTouch(View view, MotionEvent event) 
        switch (event.getAction()) 
            case MotionEvent.ACTION_DOWN:
           // 悬浮球被按下时操作
                x = (int) event.getRawX();
                y = (int) event.getRawY();
                Calendar calendar = Calendar.getInstance();
                downTime = calendar.get(Calendar.SECOND);
                Toast.makeText(getApplicationContext(), downTime + ":" + "按下", Toast.LENGTH_SHORT).show();
                break;
            case MotionEvent.ACTION_MOVE:
            //移动时
                int nowX = (int) event.getRawX();
                int nowY = (int) event.getRawY();
                int movedX = nowX - x;
                int movedY = nowY - y;
                x = nowX;
                y = nowY;
                layoutParams.x = layoutParams.x + movedX;
                layoutParams.y = layoutParams.y + movedY;
                windowManager.updateViewLayout(view, layoutParams);
                break;
            default:
                break;
            case MotionEvent.ACTION_UP:
          //  结束时松开后
                Calendar calendar1 = Calendar.getInstance();
                thisTime = calendar1.get(Calendar.SECOND);
                Toast.makeText(getApplicationContext(), thisTime + ":" + "抬起", Toast.LENGTH_SHORT).show();
                break;
        
        return false;
    
    


4. layout布局

以上是关于安卓开发多个或俩个悬浮球一起悬浮窗的主要内容,如果未能解决你的问题,请参考以下文章

Android 悬浮窗悬浮球开发

uniapp 悬浮窗(悬浮球动态菜单在其他应用上层显示) Ba-FloatBall

自定义水波球清理内存的悬浮窗小工具

安卓编程问题。怎么让悬浮窗中的view不拦截触摸事件,并将触摸事件传递给手机桌面?

Android 悬浮窗如何能让它和他的的下层一起响应触摸事件?

安卓悬浮球源代码(长按判断多次点击判断自动贴边)