我在窗口管理器中为某些操作创建菜单,但是当我为Holder Button菜单和某些itemView添加主机布局并为show cricle菜单设置ontouch浮动按钮时,出现错误无法单击到后面。我尝试仅添加主机布局(例如framelayout),不添加任何子视图,也不设置任何侦听器和触摸,但是它不起作用。 enter image description here
private void init() {
int statusBarHeight = 0;
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
statusBarHeight = getResources().getDimensionPixelSize(
Integer.parseInt(field.get(obj).toString()));
} catch (Exception e) {
e.printStackTrace();
}
mScreenWidth = getResources().getDisplayMetrics().widthPixels;
// mScreenHeight = getResources().getDisplayMetrics().heightPixels;
mScreenHeight = getResources().getDisplayMetrics().heightPixels - statusBarHeight;
try {
mDegree = 180 / (mNumber - 1);
float minScreenWH = Math.min(mScreenWidth, mScreenHeight);
mMarginY = ChatHeadUtils.dpToPx(getContext(), ConfigFloatFab.MARGINY);
if (mMarginY > (minScreenWH / 2)) mMarginY = minScreenWH / 2;
mDistance = ChatHeadUtils.dpToPx(getContext(), ConfigFloatFab.DISTANCE);
if (mDistance > mMarginY) mDistance = mMarginY;
mImageSize = ChatHeadUtils.dpToPx(getContext(), ConfigFloatFab.IMAGESIZE);
if (mImageSize > mDistance) mImageSize = mDistance;
mResImage[0] = R.drawable.ic_home_record;
mResImage[1] = R.drawable.ic_home_capture;
mResImage[2] = R.drawable.ic_home_menu;
mResImage[3] = R.drawable.ic_home_ads;
addHostLayout();
WindowManager.LayoutParams overlayParam = getParam();
overlayView = new ChatHeadOverlayView(getContext());
overlayView.setBackgroundResource(R.drawable.overlay_transition);
overlayView.setOnClickListener(v -> closeSuspendButton());
addView(overlayView, overlayParam);
hideOverlayView(false);
WindowManager.LayoutParams subparam = getParam();
subparam.width = (int) mImageSize;
subparam.height = (int) mImageSize;
for (int i = 0; i < mNumber; i++) {
ImageView iv = new ImageView(getContext());
iv.setPadding(8, 8, 8, 8);
iv.setImageResource(mResImage[i]);
imageViewList.add(i, iv);
final int index = i;
imageViewList.get(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onChildButtonClick(index);
}
}
});
addView(imageViewList.get(i), subparam);
}
imageMain = new ImageView(getContext());
imageMain.setPadding(0, 0, 0, 0);
imageMain.setImageResource(R.drawable.ic_home_logo);
addView(imageMain, subparam);
imageMain.setOnTouchListener(this);
ViewTreeObserver vto = imageMain.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
moveAnimSingle(imageMain, 0f, 0f, 0f, mMarginY, 0, false);
moveAnimInitAll(imageViewList, 0f, 0f, 0f, mMarginY, 0);
//noinspection deprecation
imageMain.getViewTreeObserver().removeGlobalOnLayoutListener(this);
suspendedStatus = SUSPEND_BUTTON_CLOSED;
for (int i = 0; i < imageViewList.size(); i++) {
imageViewList.get(i).setVisibility(View.GONE);
}
isInit = true;
if (initPos) {
movePosition(!suspendedInLeft, mStayPosY);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void addHostLayout() {
hostLayour = new FrameLayout(context);
windowManager.addView(hostLayour, getParam());
}
private void addView(View view, WindowManager.LayoutParams overlayParam) {
hostLayour.addView(view, overlayParam);
}
private WindowManager.LayoutParams getParam() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
layoutParams.x = 0;
layoutParams.y = 0;
layoutParams.gravity = Gravity.TOP | Gravity.START;
return layoutParams;
} else {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
layoutParams.x = 0;
layoutParams.y = 0;
layoutParams.gravity = Gravity.TOP | Gravity.START;
return layoutParams;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
float viewX = v.getX();
float viewY = v.getY();
float eventX = event.getRawX();
float eventY = event.getRawY();
float startX, startY, endX, endY;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = eventX;
lastY = eventY;
downX = eventX;
downY = eventY;
break;
case MotionEvent.ACTION_MOVE:
startX = viewX;
startY = viewY;
endX = viewX + (eventX - lastX);
endY = viewY + (eventY - lastY);
if (suspendedStatus == SUSPEND_BUTTON_CLOSED) {
moveAnimSingle(imageMain, startX, startY, endX, endY, 0, false);
moveAnimAll(imageViewList, eventX - lastX, eventY - lastY, 0);
if (Math.abs(lastX - downX) > 1 && Math.abs(lastY - downY) > 1) {
suspendedStatus = SUSPEND_BUTTON_MOVING;
if (listener != null) {
listener.onButtonStatusChanged(suspendedStatus);
}
suspendedStatus = SUSPEND_BUTTON_CLOSED;
}
}
lastX = eventX;
lastY = eventY;
break;
case MotionEvent.ACTION_UP:
startX = viewX;
startY = viewY;
if ((viewX + (v.getWidth() / 2)) < (mScreenWidth / 2)) {
suspendedInLeft = true;
endX = 0;
} else {
suspendedInLeft = false;
endX = mScreenWidth - v.getWidth();
}
if (viewY < mMarginY) {
endY = mMarginY;
} else if ((viewY + v.getHeight()) > (mScreenHeight - mMarginY)) {
endY = mScreenHeight - mMarginY - v.getHeight();
} else {
endY = viewY;
}
if (suspendedStatus == SUSPEND_BUTTON_CLOSED) {
moveAnimSingle(imageMain, startX, startY, endX, endY, 0, false);
moveAnimAll(imageViewList, endX - startX, endY - startY, 0);
if (Math.abs(lastX - downX) > 1 && Math.abs(lastY - downY) > 1) {
suspendedStatus = SUSPEND_BUTTON_MOVED;
if (listener != null) {
listener.onButtonStatusChanged(suspendedStatus);
}
suspendedStatus = SUSPEND_BUTTON_CLOSED;
}
}
if (Math.abs(lastX - downX) < 1 && Math.abs(lastY - downY) < 1) {
openSuspendButton();
closeSuspendButton();
}
break;
default:
break;
}
return true;
}