gpt4 book ai didi

android - WindowManager 不允许通过触摸

转载 作者:行者123 更新时间:2023-12-03 17:26:20 32 4
gpt4 key购买 nike

I have drawn an overlay using WindowManager. it's width and height are WindowManager.LayoutParams.MATCH_PARENT with transparent background.



我的 View 必须匹配父级并处理圆形触摸监听器并将其余触摸传递到下面的屏幕

我有两个小 circle in left and right corner .我让它们在工作正常的屏幕上可拖动。但是当我点击可见的主屏幕按钮时。 WindowManager 不要让可见项目可点击。
        int LAYOUT_FLAG = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.
LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PHONE;

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,//changed it to full
WindowManager.LayoutParams.MATCH_PARENT,
LAYOUT_FLAG,

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
///| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, this flag can make it in touchable.
///WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
PixelFormat.TRANSLUCENT);

mFloatingView = LayoutInflater.from(this).inflate(R.layout.item_circle_dragging, null);

mFloatingView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
//Add the view to the window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView,params);

item_circle_dragging.xm l
<?xml version="1.0" encoding="utf-8"?>
<customviewpracticing.CircleDraggingView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

</customviewpracticing.CircleDraggingView>

CircleDragginView onTouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(isPointInside(event.getRawX(),event.getRawY()))
isAllowedToDrag = true;
Log.d(TAG, "ACTION_DOWN:getRawX= " + event.getRawX() + " getRawY= " + event.getRawY() + " getX= "
+ event.getX() + " getY= " + event.getY());
break;
///return true;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "ACTION_MOVE:getRawX= " + event.getRawX() + " getRawY= " + event.getRawY() + " getX= "
+ event.getX() + " getY= " + event.getY());
if(isAllowedToDrag){
center_circle_X = event.getRawX() ;
center_circle_Y = event.getRawY();
}/*this.animate().x(event.getRawX()).y(event.getRawY())
.setDuration(50).start();*/
break;
case MotionEvent.ACTION_UP:
if(isAllowedToDrag)
isAllowedToDrag = false;
break;
default:
return true;//I changed it
}
// Force a view to draw again
///postInvalidate();
invalidate();
return true;
}

也试过 返回错误 onTouchEventCircleDraggingView还有主要的 mFloatingView ( Root View )。

最佳答案

恐怕你不能在同一个 View 上这样做,因为触摸事件要么进入你的 View ,要么不进入。是否使用拦截、返回 false 等...它们都取决于事件是否真正 达到您的观点,即使那样,返回 false 也不会将事件传递到下面的屏幕,因为它们是不同的过程。在您的情况下,该事件不会到达您的 窗口 ,这意味着您根本无法处理该事件。
我不完全确定 parent 做了什么,我相信你在触摸它后试图将圆形 View 移动到这里,通过拖动操作或某种方式。因此,我将提供一个示例方法来说明如何做到这一点。
可以在您正在创建的布局参数中指定在左右角添加 View 。例如,左上角看起来像这样:

params.x = 0;
paramx.y = 0;
右上角看起来像这样:
WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

Point size = new Point();

// Get the screen dimensions here
manager.getDefaultDisplay().getSize(point);
int screenWidth = point.x;

// Assuming your view has a 40 dp width and height,
// calculate its pixel variant
int circleWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DP, 40, getResources().getDisplayMetrics());

// Target the top-right corner of the screen.
params.x = screenWidth - circleWidth;

// Y coordinate would be 0.
params.y = 0;
View 参数如下所示:
// Assuming your view has a 40 dp width and height,
// calculate its pixel variant
int circleSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DP, 40, getResources().getDisplayMetrics());

WindowManager.LayoutParams params = new WindowManager.LayoutParams(circleSize /* width */,
circleSize /* height */,
0 /* x */,
0 /* y */,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY /* windowType */,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH /* outsideTouch */,
PixelFormat.TRANSLUCENT /* format */);
之后,移动 View 实际上需要更改布局参数并更新 View 布局。在您的 onTouchEvent你可以这样做:
// Assuming you know the new x and y values of the view, you can
// update the layout params of the view like this:
((WindowManager.LayoutParams) getLayoutParams()).x = targetX;
((WindowManager.LayoutParams) getLayoutParams()).y = targetY;

// To update the view, you need to either call requestLayout() or
// do it from window manager directly.

// If the first way doesn't work, 2nd will work.

// First way:
requestLayout();

// Second way:

// This will get the same window manager the service has.
WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);

// Update the view layout afterwards.
manager.updateViewLayout(this, getLayoutParams());
这样,您可以使用触摸事件移动 View 。
需要注意的几点:
  • 如果能抓到onTouchEvent你知道你已经触摸了这个圆圈,所以你不需要使用原始值来执行坐标检查(我指的是isPointInside()函数)。从所有这些中返回 true 将帮助您接收所有事件,但这不是必需的。无论如何,您可能知道生命周期是如何工作的。
  • 每个圆圈都必须以 WindowManager.addView() 的 root 身份添加到窗口管理器中。这样触摸就不会相互干扰,并且触摸不在圆圈中的点会将事件传递到下面的屏幕。

  • 这将是您要求的开始,我认为您可以根据自己的需要进行调整。

    关于android - WindowManager 不允许通过触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51924911/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com