Android仿360桌面悬浮窗代码Demo下载
程序员文章站
2022-03-01 14:35:08
...
本Demo是一个Android应用仿360安全工具的悬浮窗的小例子源码、例子里的悬浮窗可随意移动、并且可以实时显示当前内存数据、上层数据表示可用内存值、下层数据表示总内存值、点击悬浮窗出现关闭小图标可直接关闭
另外要说明的是、在安卓4.4.2以上自动把所有悬浮窗都禁止显示了、如果你的系统是4.4.2以上的需要手动在设置 > 应用 > 点击具体应用 > 把里面的“显示悬浮窗”选项打开才可以正常运行哦
FloatService 代码
private void createView() { SharedPreferences shared = getSharedPreferences("float_flag", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = shared.edit(); editor.putInt("float", 1); editor.commit(); // 获取WindowManager wm = (WindowManager) getApplicationContext() .getSystemService("window"); // 设置LayoutParams(全局变量)相关参数 wmParams = ((MyApplication) getApplication()).getMywmParams(); wmParams.type = 2002; wmParams.flags |= 8; // 调整悬浮窗口至左上角 wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 以屏幕左上角为原点,设置x、y初始值 wmParams.x = 0; wmParams.y = 0; // 设置悬浮窗口长宽数据 wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT; wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT; wmParams.format = 1; wm.addView(view, wmParams); view.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // 获取相对屏幕的坐标,即以屏幕左上角为原点 x = event.getRawX(); y = event.getRawY() - 25; // 25是系统状态栏的高度 // 调试信息 Log.i("currP", "currX" x "====currY" y); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: state = MotionEvent.ACTION_DOWN; StartX = x; StartY = y; // 获取相对View的坐标,即以此View左上角为原点 mTouchStartX = event.getX(); mTouchStartY = event.getY(); Log.i("startP", "startX" mTouchStartX "====startY" mTouchStartY);// 调试信息 break; case MotionEvent.ACTION_MOVE: state = MotionEvent.ACTION_MOVE; updateViewPosition(); break; case MotionEvent.ACTION_UP: state = MotionEvent.ACTION_UP; updateViewPosition(); showImg(); mTouchStartX = mTouchStartY = 0; break; } return true; } }); iv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent serviceStop = new Intent(); serviceStop.setClass(FloatService.this, FloatService.class); stopService(serviceStop); } }); }
MainActivity 调用方法
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnstart = (Button) findViewById(R.id.btnstart); btnstart.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { Intent service = new Intent(); service.setClass(memFloat.this, FloatService.class); startService(service); } }); btnstop = (Button) findViewById(R.id.btnstop); btnstop.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { Intent serviceStop = new Intent(); serviceStop.setClass(memFloat.this, FloatService.class); stopService(serviceStop); } }); }
源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1ntzcxgH 密码: wcn7
上一篇: 12个非常不错的免费HTML后台管理模板
下一篇: 给大家推荐两款Chrome效率插件