如何在Android中将弹出视图拖动到屏幕之外?

问题描述 投票:0回答:3

我得到了一个在WindowManager的帮助下以及这些参数(如您在其他stackoverflow问题中所发现的那样)生成的弹出窗口:

new WindowManager.LayoutParams(
  ViewGroup.LayoutParams.WRAP_CONTENT,
  ViewGroup.LayoutParams.WRAP_CONTENT,
  WindowManager.LayoutParams.TYPE_PHONE,
  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
  PixelFormat.TRANSLUCENT
)

我的问题类似于这个问题:Android: Drag View outside screen,也类似于这个问题:Dragging a view outside of RelativeLayout

不同之处在于WindowManager.LayoutParams不包含任何类型的边距,因此无法在相对布局等中使用边距技巧。

关于如何将视图拖动到android屏幕边界之外的任何想法?

谢谢

编辑:正如您在这里问的那样,暂时进行拖动的相关代码是

view.setOnTouchListener(new OnTouchListener {
  def onTouch(view: View, motionEvent: MotionEvent): Boolean = {
    val x = motionEvent.getRawX.toInt
    val y = motionEvent.getRawY.toInt

    motionEvent.getAction & MotionEvent.ACTION_MASK match {
      case MotionEvent.ACTION_DOWN =>
        val params = view.getLayoutParams.asInstanceOf[WindowManager.LayoutParams]

        xDelta = x - params.x

        yDelta = y - params.y

      case MotionEvent.ACTION_UP =>
        draggingOn = false

      case MotionEvent.ACTION_MOVE =>
        if (draggingOn) {
          val params = view.getLayoutParams.asInstanceOf[WindowManager.LayoutParams]

          params.x = x - xDelta

          params.y = y - yDelta

          //view.setLayoutParams(params)
          windowManager.updateViewLayout(view, params)

          //log log "current x is: " + x

          //log log "current y is: " + y
        } else {
          //nop
        }

      case _ =>
      //case MotionEvent.ACTION_POINTER_DOWN =>
      //case MotionEvent.ACTION_POINTER_UP =>
    }

    false
  }
})

上面的代码在scala中,但我想您不会很难阅读它

android drag android-windowmanager
3个回答
0
投票

让您的拖动视图为20X20,整个屏幕为100X100。可以通过以下几行来实现您希望的这种效果:

  1. 检测拖动的视图不能包含在屏幕边界中时的状态假设当前的拖动位置为(50,90),这意味着视图的10个单位(垂直)不可见。在检测到这一点时,应将marginY设置为+10个单位。

  2. 同样,如果“拖动”位置为(-10,-15),则这意味着视图的10个单位(水平)不可见(垂直)和15个单位(垂直)。在检测到这一点时,应设置marginY = -15个单位,marginX = -10个单位。

要记住的是,尺寸必须使用相同的单位。希望这会有所帮助。


0
投票

编辑:我找到了一个更简单的解决方案:只需致电:

setClippingEnabled(false);

并且下面的代码没有边际技巧:

window.update(mOffsetX, mOffsetY, -1, -1, true);

旧回复:

您不能在屏幕外部设置X位置,也不能在PopupWindow布局参数上设置边距。

作为一种解决方法,我使用:

case MotionEvent.ACTION_DOWN:
    mOrgX = (int) event.getRawX();
    break;

case MotionEvent.ACTION_MOVE:
    mOffsetX = (int) event.getRawX() - mOrgX;
    window.update(mOffsetX, mOffsetY, -1, -1, true);

正如我之前说过的,您不能更改PopupWindow的页边距,但是可以更改内容视图的页边距。

当offsetX大于弹出窗口的起始X位置时,我开始更改视图边距

if (mStartingWindowPosition + mOffsetX <= 0 
         || mWindowWith + mOffsetX > mScreenWidth) {

    int delta = 0;
    if (mOffsetX > 0) {
        delta = window.getContentView().getWidth() + mOffsetX - mScreenWidth;
    }
    else {
        delta = mStartingWindowPosition + mOffsetX;
    }
    params.rightMargin = -delta;
    params.leftMargin = delta;
}

with:

if (mWindowWith == 0) {
    mWindowWith = window.getContentView().getWidth();
    mStartingWindowPosition = mScreenWidth / 2 - mWindowWith / 2;  // my window is X centered
}
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)    window.getContentView().getLayoutParams();

对我来说,它完成了工作

在4.X和5.0设备上测试过

注意:此代码目前尚无生产,所以我不知道是否有副作用


0
投票

只需在LayoutParams中使用标志FLAG_LAYOUT_NO_LIMITS

new WindowManager.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.TYPE_PHONE,
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
    PixelFormat.TRANSLUCENT
)
© www.soinside.com 2019 - 2024. All rights reserved.