本文实例为大家分享了Android实现触摸移动的悬浮窗的具体代码,供大家参考,具体内容如下
1.触摸移动的悬浮窗的快速实现 UI
2. 悬浮窗实现
2.1 悬浮窗权限
为了偷懒,我SDK选择比较低的17,如果不配置权限,则应用会运行报错
正规流程,查询是否有悬浮窗权限,如果没有需要提示,并跳转到悬浮窗设置界面“即是否允许运行在其他应用上面”
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2.2 悬浮窗配置初始化
这里主要关注 mParams.type 和 mParams.flags 这两个参数是核心,我们也可以使用View来加载一个布局,为了侧重悬浮窗界面,我直接使用TextView演示
private void initFloatWindow() { if (mShow == null) { mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); mShow = new TextView(this); mShow.setText("I Love You"); mShow.setTextColor(Color.RED); mShow.setTextSize(50); mShow.setOnTouchListener(new View.OnTouchListener() { int lastX = 0; int lastY = 0; int paramX = 0; int paramY = 0; @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) motionEvent.getRawX(); lastY = (int) motionEvent.getRawY(); paramX = mParams.x; paramY = mParams.y; break; case MotionEvent.ACTION_MOVE: int dx = (int) motionEvent.getRawX() - lastX; int dy = (int) motionEvent.getRawY() - lastY; mParams.x = paramX + dx; mParams.y = paramY + dy; mWm.updateViewLayout(mShow, mParams); break; } return true; } }); mParams = new WindowManager.LayoutParams(); mParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // 实现悬浮窗可以移动的属性 mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; mParams.format = PixelFormat.TRANSPARENT; } }
2.3 悬浮窗显示
mWm.addView(mShow, mParams);
2.4 悬浮窗隐藏
mWm.removeView(mShow);
2.5 完整示例代码
package com.wu.sufadi; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.graphics.PixelFormat; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.TextView; public class MainActivity extends Activity { private TextView mShow; private WindowManager mWm; private WindowManager.LayoutParams mParams; private static final int MSG_SHOW_WINDOW = 1; private static final int MSG_HIDE_WINDOW = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFloatWindow(); } public void click_show(View view) { showFloatWindow(); } public void click_hide(View view) { hideFloatWindow(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MSG_SHOW_WINDOW: mWm.addView(mShow, mParams); break; case MSG_HIDE_WINDOW: mWm.removeView(mShow); break; default: break; } } }; private void showFloatWindow() { handler.sendEmptyMessage(MSG_SHOW_WINDOW); } private void hideFloatWindow() { handler.sendEmptyMessage(MSG_HIDE_WINDOW); } private void initFloatWindow() { if (mShow == null) { mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); mShow = new TextView(this); mShow.setText("I Love You"); mShow.setTextColor(Color.RED); mShow.setTextSize(50); mShow.setOnTouchListener(new View.OnTouchListener() { int lastX = 0; int lastY = 0; int paramX = 0; int paramY = 0; @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) motionEvent.getRawX(); lastY = (int) motionEvent.getRawY(); paramX = mParams.x; paramY = mParams.y; break; case MotionEvent.ACTION_MOVE: int dx = (int) motionEvent.getRawX() - lastX; int dy = (int) motionEvent.getRawY() - lastY; mParams.x = paramX + dx; mParams.y = paramY + dy; mWm.updateViewLayout(mShow, mParams); break; } return true; } }); mParams = new WindowManager.LayoutParams(); mParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // 实现悬浮窗可以移动的属性 mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; mParams.format = PixelFormat.TRANSPARENT; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。