美文网首页
安卓简单创建悬浮窗按钮

安卓简单创建悬浮窗按钮

作者: 510bb14393e1 | 来源:发表于2022-06-24 07:53 被阅读0次

1.添加所需权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

2.编写MainActivity类

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.WindowManager;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.os.Build;
import android.content.Context;
import android.view.View;
import android.view.MotionEvent;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity { 
    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showFloatingWindow();
    }
    //调用该方法,可创建一个悬浮窗显示于屏幕之上
    private void showFloatingWindow() {
        button = new Button(this);
        button.setText("按钮");
        button.setOnTouchListener(new FloatingOnTouchListener());
        button.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View p1) {
                    Toast.makeText(getApplication(), "我被点击了", Toast.LENGTH_SHORT).show();
                }
            });
        layoutParams = new WindowManager
            .LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, 0, 0, PixelFormat.TRANSPARENT);
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
        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.x = 300;
        layoutParams.y = 300;
        windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        windowManager.addView(button, layoutParams);
    }
    private class FloatingOnTouchListener implements View.OnTouchListener {
        private int startX;
        private int startY;

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = (int) event.getRawX();
                    startY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - startX;
                    int movedY = nowY - startY;
                    startX = nowX;
                    startY = nowY;
                    layoutParams.x = layoutParams.x + movedX;
                    layoutParams.y = layoutParams.y + movedY;

                    // 更新悬浮窗控件布局
                    windowManager.updateViewLayout(view, layoutParams);
                    break;
                default:
                    break;
            }
            return false;
        }
    }
    
    }

效果图


相关文章

网友评论

      本文标题:安卓简单创建悬浮窗按钮

      本文链接:https://www.haomeiwen.com/subject/jhdvvrtx.html