Невозможно перетащить изображение за пределы вида карты - PullRequest
0 голосов
/ 09 марта 2020

Под перетаскиванием я подразумеваю изменение rawX и rawY виджета следующим образом:

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

Мой макет содержит вид карты и внутри несколько изображений, которые можно перетаскивать, нажимая и перемещая их. Перетаскивание работает, но изображения исчезают за пределами представления карты:

enter image description here

(изображение посередине было перетащено вниз, и теперь оно только наполовину видимый, потому что он перетаскивается за пределы вида карты.)

Мой макет:

<?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>

Что можно сделать, чтобы изображения перетаскивались за пределы вида карты?

Заранее спасибо

Ответы [ 2 ]

2 голосов
/ 13 марта 2020

вы не можете перетащить его за пределы 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>
1 голос
/ 16 марта 2020

Я не видел ваш java код, но я думаю, что вы должны позволить пользователю перетаскивать CardView, а не ImageView внутри него. Насколько я понимаю, ваш CardView делает именно то, что должен делать. а именно, обеспечивает вид окна или выреза, внутри которого видны содержащиеся изображения (например, я использовал их, чтобы сделать мои изображения круговыми)

Если вы хотите, чтобы изображения выглядели так, как будто они находятся внутри CardView или некоторого другого В контейнере вы можете поместить ImageViews вне CardView, а затем заставить их отображаться поверх него с помощью RelativeLayout. затем вы можете отслеживать их координаты, чтобы увидеть, считаете ли вы, что они выпадают "изнутри" или "наружу"

...