无法将图像拖出卡片视图

Cannot drag an image outside of card view

拖动是指像这样更改小部件的 rawX 和 rawY:

view.y = motionEvent.rawY - ((view.height / 2) + 120)
view.x = motionEvent.rawX - view.width / 2

我的布局包含一个卡片视图和一些图像,可以通过点击和移动来拖动它们。拖动有效,但图像消失在卡片视图的边界之外:

(中间的图片被向下拖动,现在只有一半可见,因为它被拖到了卡片视图的边界之外。)

我的布局:

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.android.navigation.TitleFragment">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="8dp"
            app:cardBackgroundColor="#f2f2f2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:padding="8dp">


                <ImageView
                    android:id="@+id/card1"
                    android:layout_width="@dimen/expense_card_image_size"
                    android:layout_height="@dimen/expense_card_image_size"
                    android:scaleType="centerCrop"
                    android:clickable="true"
                    android:focusable="true"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    android:contentDescription="@string/content_description_groceries"
                    android:src="@drawable/groceries"/>



                <ImageView
                    android:id="@+id/card2"
                    android:layout_width="@dimen/expense_card_image_size"
                    android:layout_height="@dimen/expense_card_image_size"
                    android:scaleType="centerCrop"
                    android:clickable="true"
                    android:focusable="true"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:contentDescription="@string/content_description_rent"
                    android:src="@drawable/rent"/>


        </androidx.cardview.widget.CardView>



    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

如何才能将图像拖到卡片视图之外?

提前致谢

你不能将它拖到 CardView 之外,如果你想在整个屏幕中拖动图像移动 imageView 到顶部父布局

此代码将允许 imageView 拖入整个屏幕

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.android.navigation.TitleFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/card"
            android:layout_width="0dp"
            android:layout_height="@dimen/expense_card_image_size"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:elevation="8dp"
                android:padding="8dp"
                app:cardBackgroundColor="#f2f2f2" />
        </FrameLayout>

        <ImageView
            android:id="@+id/card1"
            android:layout_width="@dimen/expense_card_image_size"
            android:layout_height="@dimen/expense_card_image_size"
            android:clickable="true"
            android:contentDescription="@string/content_description_groceries"
            android:focusable="true"
            android:scaleType="centerCrop"
            android:src="@drawable/groceries"
            app:layout_constraintStart_toStartOf="@id/card"
            app:layout_constraintTop_toTopOf="@id/card" />


        <ImageView
            android:id="@+id/card2"
            android:layout_width="@dimen/expense_card_image_size"
            android:layout_height="@dimen/expense_card_image_size"
            android:clickable="true"
            android:contentDescription="@string/content_description_rent"
            android:focusable="true"
            android:scaleType="centerCrop"
            android:src="@drawable/rent"
            app:layout_constraintEnd_toEndOf="@id/card"
            app:layout_constraintTop_toTopOf="@id/card" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

我还没有看到您的 java 代码,但我认为您应该让用户在其中拖动 CardView 而不是 ImageView。 据我了解,您的 CardView 正在做它应该做的事情。即提供一种 window 或切口,其中包含的图像视图是可见的(例如,我用它们使我的图像视图循环)

如果您希望图像看起来像是在 CardView 或其他容器中,您可以将 ImageView 放置在 CardView 外部,然后使用 RelativeLayout 使它们显示在它上面。然后你可以跟踪他们的坐标,看看你认为他们是掉落 "inside" 还是 "out"