通过拖动,我的意思是像这样更改小部件的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>
我该怎么做才能将图像拖到卡片视图之外?
提前感谢
它将允许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确实在执行应有的操作。即提供某种窗口或切口,在其中可见包含的图像视图(例如,我已使用它们使我的图像视图变为圆形)
由于您似乎对cardview并没有做任何事情,所以您最好也不要使用它,或使用其他容器来查看imageview。
如果您出于某些尚不清楚的其他原因而确实需要CardView,则只需将CardView的高度设置为与父级相匹配,如下所示:
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="8dp"
app:cardBackgroundColor="#f2f2f2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:padding="8dp">