1.悬浮窗权限申请:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(getApplicationContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivityForResult(intent, 100);
} else {
/*需要启动的service*/
mIntent = new Intent(MainActivity.this, FloatWindowService.class);
bindService(mIntent, serviceConnection, Context.BIND_AUTO_CREATE);//直接启动服务方式启动
}
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
在Activity回调申请权限的结果:
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(this)) {
Intent mIntent = new Intent(MainActivity.this,FloatWindowService.class);
startService(mIntent);//直接启动服务方式启动
}
}
}
2.创建悬浮窗:
WindowManager windowManager = (WindowManager) mContext.getSystemService(WINDOW_SERVICE);
// 设置LayoutParam
WindowManager.LayoutParams 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.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//宽高自适应
layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR; //竖屏
layoutParams.gravity = Gravity.START | Gravity.TOP;
//显示的位置
layoutParams.x = ScreenManager.dipToPx(mContext, 14);
layoutParams.y = ScreenManager.dipToPx(mContext, 97);
View floatView = LayoutInflater.from(mContext).inflate(R.layout.window_float_view, null);
xml内部布局
<LinearLayout
android:id="@+id/layout_voice_broadcast"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@color/color_FF198CFF"
android:orientation="horizontal"
tools:ignore="UselessLeaf,UselessParent">
</LinearLayout>
xml颜色
<color name="color_FF198CFF">#FF198CFF</color>
/**
* 根据手机的分辨率从 dip 的单位 转成为 px(像素)
*/
public static int dipToPx(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
3.设置滑动监听:
floatView.setOnTouchListener(new FloatingOnTouchListener());
private class FloatingOnTouchListener implements View.OnTouchListener {
private int
mTouchStartX,
mTouchStartY,
mTouchCurrentX,
mTouchCurrentY,
mMoveX,
mMoveY;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mTouchStartX = (int) event.getRawX();
mTouchStartY = (int) event.getRawY();
mMoveX = 0;
mMoveY = 0;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
mTouchCurrentX = (int) event.getRawX();
mTouchCurrentY = (int) event.getRawY();
mMoveX = mTouchCurrentX - mTouchStartX;
mMoveY = mTouchCurrentY - mTouchStartY;
mTouchStartX = mTouchCurrentX;
mTouchStartY = mTouchCurrentY;
layoutParams.x += mMoveX;
layoutParams.y += mMoveY;
windowManager.updateViewLayout(floatView, layoutParams);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
windowManager.updateViewLayout(floatView, layoutParams);
}
return false;
}
}
/**
* 根据手机的分辨率从 dip 的单位 转成为 px(像素)
*/
public static int dipTopx(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
如对此有疑问,请联系qq1164688204。
推荐Android开源项目
项目功能介绍:RxJava2和Retrofit2项目,添加自动管理token功能,添加RxJava2生命周期管理,使用App架构设计是MVP模式和MVVM模式,同时使用组件化,部分代码使用Kotlin,此项目持续维护中。
项目地址:https://gitee.com/urasaki/RxJava2AndRetrofit2