我正在尝试创建一个始终位于所有窗口顶部的始终位于顶部的按钮/可点击图像。
概念证明是
我已经成功并且现在有一个正在运行的服务。该服务一直在屏幕的左上角显示一些文本,而用户可以以正常方式自由地与其余应用程序交互。
我正在做的是子类ViewGroup并将其添加到带有 flag 的根窗口管理器中TYPE_SYSTEM_OVERLAY。现在我想添加一个按钮/可点击图像来代替这个文本,它可以接收自己的触摸事件。我尝试覆盖整个“onTouchEvent”,ViewGroup但它没有收到任何事件。
ViewGroup
TYPE_SYSTEM_OVERLAY
如何仅在始终位于顶部的视图组的某些部分接收事件?请建议。
public class HUD extends Service { HUDView mView; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show(); mView = new HUDView(this); WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, // WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE // | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT); params.gravity = Gravity.RIGHT | Gravity.TOP; params.setTitle("Load Average"); WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.addView(mView, params); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show(); if(mView != null) { ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView); mView = null; } } } class HUDView extends ViewGroup { private Paint mLoadPaint; public HUDView(Context context) { super(context); Toast.makeText(getContext(),"HUDView", Toast.LENGTH_LONG).show(); mLoadPaint = new Paint(); mLoadPaint.setAntiAlias(true); mLoadPaint.setTextSize(10); mLoadPaint.setARGB(255, 255, 0, 0); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawText("Hello World", 5, 15, mLoadPaint); } @Override protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) { } @Override public boolean onTouchEvent(MotionEvent event) { //return super.onTouchEvent(event); Toast.makeText(getContext(),"onTouchEvent", Toast.LENGTH_LONG).show(); return true; } }
这可能是一个愚蠢的解决方案。但它有效。如果你能改进它,请告诉我。
您的服务的 OnCreate:我使用了WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH标志。这是服务的唯一变化。
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
@Override public void onCreate() { super.onCreate(); Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show(); mView = new HUDView(this); WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); params.gravity = Gravity.RIGHT | Gravity.TOP; params.setTitle("Load Average"); WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.addView(mView, params); }
现在,您将开始获取每个点击事件。因此,您需要在事件处理程序中进行纠正。
在您的 ViewGroup 触摸事件中
@Override public boolean onTouchEvent(MotionEvent event) { // ATTENTION: GET THE X,Y OF EVENT FROM THE PARAMETER // THEN CHECK IF THAT IS INSIDE YOUR DESIRED AREA Toast.makeText(getContext(),"onTouchEvent", Toast.LENGTH_LONG).show(); return true; }
此外,您可能需要将此权限添加到清单中:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />