android - 限制父边界内的 subview

标签 android android-layout

我正在创建一个矩形框并在其中放置一个可以移动的 imageView。在执行 onMove 时,我注意到 imageView 超出了我不想要的父 View 的边界。我想将 imageView 限制在其父 View 的边界内。如何做到这一点。

这是 xml:

<FrameLayout
    android:layout_width="400dp"
    android:layout_height="400dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:id="@+id/objContainer"
    android:background="@android:color/holo_blue_bright" >
 <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/btn_default" />
</FrameLayout>

</RelativeLayout>

处理触摸事件的代码:

@Override
public boolean onTouch(View view, MotionEvent event) {
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            final float x = event.getRawX();
            final float y = event.getRawY();

            // Remember where we started
            mLastTouchX = x;
            mLastTouchY = y;

            break;
        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_POINTER_UP:
            break;
        case MotionEvent.ACTION_MOVE:


            final float x1 = event.getRawX();
            final float y1 = event.getRawY();

            // Calculate the distance moved
            final float dx = x1 - mLastTouchX;
            final float dy = y1 - mLastTouchY;

            // Move the object
            mPosX += dx;
            mPosY += dy;
            view.setX(mPosX);
            view.setY(mPosY);

            // Remember this touch position for the next move event
            mLastTouchX = x1;
            mLastTouchY = y1;

            // Invalidate to request a redraw
            root.invalidate();
            break;
    }
    return true;
}}

最佳答案

在重写的 onTouch 中,您需要检查以下操作是否会将 ImageView 保留在其容器内。

换句话说,您需要检查 ImageView 的矩形在被 dx、dy 移动后是否会留在父级中。

这是一个例子:

case MotionEvent.ACTION_MOVE:
    ...
    // Calculate the distance moved
    final float dx = x1 - mLastTouchX;
    final float dy = y1 - mLastTouchY;

    // Make sure we will still be the in parent's container
    Rect parent = new Rect(0, 0, root.getWidth(), root.getHeight());

    int newLeft = (int) (iv.getX() + dx),
            newTop = (int) (iv.getY() + dy),
            newRight = newLeft + iv.getWidth(),
            newBottom = newTop + iv.getHeight();

    if (!parent.contains(newLeft, newTop, newRight, newBottom)) {
        Log.e(TAG, String.format("OOB @ %d, %d - %d, %d",
                newLeft, newTop, newRight, newBottom));
        break;
    }
    ...

另请注意,您不需要使容器无效,因为 setX/setY 会使 ImageView 本身无效。

关于android - 限制父边界内的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30514519/

相关文章:

android - 在 Android API 22 中打开相机灯

android - 构建后 android phonegap 1.9 中未显示启动画面

java - 从firebase实时数据库中获取指定的子项

android - 获取用户SIM卡列表并选中-flutter

java - 自定义 Android Studio 中的创建方法选项

android - 如何捕获带有背景的SurfaceView的屏幕截图

android - Android中不同的左滑面板

android - Android 中的多列文本显示

android - 如何使 Android GridLayout 与旧版本兼容?

android-layout - Android setTextSize 改变editText高度和形状