Android — 制作悬浮窗口

423 查看

获取 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);
    }
}