Android — 制作悬浮窗口
16lz
2021-01-23
获取 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); }}
更多相关文章
- android设置保持窗口常亮
- Android 之 Window、WindowManager 与窗口管理
- [zz] Android 初始化语言(Android init Language翻译)
- android的窗口机制分析------事件处理
- Android之SurfaceView窗口/全屏播放
- Android - Android Studio 的 Preview窗口