标签:
iPhone有个很好用的白色圆点,今天就来研究下Android中的悬浮框,这里主要是实现一个快捷键的功能,当然也可以在悬浮框中做想做的事!
悬浮窗的实现主要是通过WindowManager实现,当然WindowManager只是一个接口,想了解源码的同志们可以去看WindowManagerImpl,悬浮框主要是通过WindowManager中的addView,updateView,removeView实现
WindowManager.LayoutParams这个类用于提供悬浮窗所需的参数
WindowManager.LayoutParams参数说明:
了解这些参数基本就能创建一个悬浮框了
private void createFloatView() {
mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
wmParams = new WindowManager.LayoutParams();
wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
wmParams.x = 100;
wmParams.y = 100;
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
LayoutInflater inflater = LayoutInflater.from(getApplication());
mFloatLayout = (LinearLayout) inflater.inflate(R.layout.layout_float_window, null);
mWindowManager.addView(mFloatLayout, wmParams);
mFloatView = (ImageView) mFloatLayout.findViewById(R.id.img_float_window);
mFloatView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth() / 2;
wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 25;
mWindowManager.updateViewLayout(mFloatLayout, wmParams);
return false;
}
});
mFloatView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FloatWindowService.this, SearchActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);
}
});
}
if (mFloatLayout != null) {
mWindowManager.removeView(mFloatLayout);
}
以上就是悬浮框的过程,当然使用系统window必须要申请权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
通过上面这个过程知道了如何通过WindowManager创建悬浮框
下面给你一个Demo,其中有两个悬浮窗,第一个悬浮窗就是普通的悬浮窗,第二个悬浮窗加了一些判断,可以直接在app中使用,这个悬浮窗在桌面和你自己的app中显示
标签:
原文地址:http://blog.csdn.net/elinavampire/article/details/50360018