悬浮窗 核心代码
int myx=0,myy=0;
LinearLayout mFloatLayout_shou; // 加个手指吧
WindowManager.LayoutParams wmParams_shou; 加个手指吧
//创建浮动窗口设置布局参数的对象
WindowManager mWindowManager_shou;
ImageButton imageView_shou;
/**
* 一直收的 炫富床
*/
private void showFloatingWindow_shou() {
if(wmParams_shou==null){
// wmParams_shou= new WindowManager.LayoutParams();//手
//设置WindowManger布局参数以及相关属性
wmParams_shou = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//设置图片格式,效果为背景透明
wmParams_shou.format = PixelFormat.RGBA_8888;
//设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
wmParams_shou.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//初始化位置
wmParams_shou.gravity = Gravity.TOP | Gravity.LEFT;
wmParams_shou.x = 0;//https://www.jianshu.com/p/579f25ae002b
wmParams_shou.y = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
wmParams_shou.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
wmParams_shou.type = WindowManager.LayoutParams.TYPE_PHONE;
}
// //设置悬浮窗口长宽数据
wmParams_shou.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams_shou.height = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams_shou.width = 22dp;
//
//
// wmParams_shou.height = 25;
mWindowManager_shou= (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
LayoutInflater inflater = LayoutInflater.from(getApplication());
mFloatLayout_shou = (LinearLayout) inflater.inflate(R.layout.item_shou, null);//手
imageView_shou= (ImageButton) mFloatLayout_shou.findViewById(R.id.imageView_shou);
imageView_shou.getBackground().setAlpha(0);//透明
mWindowManager_shou.addView(mFloatLayout_shou, wmParams_shou);
}
// 设置悬浮窗的Touch监听
imageView_shou.setOnTouchListener(new View.OnTouchListener() {//https://blog.csdn.net/shinay/article/details/7783276
//获取X坐标
private int startX;
//获取Y坐标
private int startY;
//初始化X的touch坐标
private float startTouchX;
//初始化Y的touch坐标
private float startTouchY;
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = wmParams_shou.x;
startY = wmParams_shou.y;
startTouchX = event.getRawX();
startTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
wmParams_shou.x = startX + (int) (event.getRawX() - startTouchX);
wmParams_shou.y = startY + (int) (event.getRawY() - startTouchY);
//更新View的位置
mWindowManager_shou.updateViewLayout(mFloatLayout_shou, wmParams_shou);
writeIp("zuobiao", wmParams_shou.x +"#"+ wmParams_shou.y ,editor);
Message message = Message.obtain();
message.what = 2;
message.arg1 = wmParams_shou.x;
message.arg2 = wmParams_shou.y;
// message.obj = "jack";
handler.sendMessage(message);
return true;
}
return false;
}//touch
});
}
//========= 只展示 一只 小手 的悬浮窗====================== 结束
handler对象,用来接收消息
// handler对象,用来接收消息~
private Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) { //这个是发送过来的消息https://www.cnblogs.com/fuck1/p/5513412.html
int what = msg.what;
// 处理从子线程发送过来的消息
if(what==1){
int arg1 = msg.arg1; //获取消息携带的属性值
int arg2 = msg.arg2;
// 这里 让 小手指 移动到底 制动位置
// Object result = msg.obj;
// System.out.println("-arg1--->>" + arg1);
// System.out.println("-arg2--->>" + arg2);
// System.out.println("-what--->>" + what);
wmParams_shou.x = arg1;
wmParams_shou.y = arg2;
// 更新悬浮窗位置
mWindowManager_shou.updateViewLayout(mFloatLayout_shou, wmParams_shou);
/***************************使用*****************************/
zuobiao= sp.getString("zuobiao", "0#0"); // 这个地方会报null 异常
// System.out.println("myx 写入了"+zuobiao);
Toast.makeText(MainActivity.this, "Toast.make"+zuobiao, Toast.LENGTH_SHORT).show();
//传入所在比例
// 广播传值了吧
// Toast.makeText(FindTempActivity.this, "x: "+arg1+" /r y: "+arg2, Toast.LENGTH_SHORT).show();
// System.out.println("-result--->>" + result);
// Bundle bundle = msg.getData(); // 用来获取消息里面的bundle数据
// System.out.println("-getData--->>"
// + bundle.getStringArray("strs").length);
// 查找完毕---坐标是877,239
// 2022-02-15 18:13:52.535 22769-23088/com.example.findtemp I/System.out: 查找完毕---坐标是877,477
// 2022-02-15 18:13:54.541 22769-23088/com.example.findtemp I/System.out: 查找完毕---坐标是877,1192
// 2022-02-15 18:13:56.544 22769-23088/com.example.findtemp I/System.out: 查找完毕---坐标是877,1430
}
if(what==2){
int arg1 = msg.arg1; //获取消息携带的属性值
int arg2 = msg.arg2;
Toast.makeText(MainActivity.this, "x: "+ arg1 +" Y: "+arg2, Toast.LENGTH_SHORT).show();
}
};
};