我可以将视图附加到动画视图吗?

Can I attach a view to an animated view?

我有一个视图 (ImageView),我想制作动画,我正在为这张图片附加一个按钮。该按钮受限于图像(我使用的是受限布局)但是在为图像设置动画时,该按钮是分离的并且不参与动画。

有没有一种简单的方法可以将按钮限制在图像上,以便它们在为图像设置动画时同时移动?

我试着让按钮成为图像的子元素:

    <ImageView
        android:id="@+id/profilePic2"
        android:layout_width="@dimen/profile_pic_reorder_size"
        android:layout_height="@dimen/profile_pic_reorder_size"
        android:layout_margin="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/profile_pic2">
        <Button
            android:id="@+id/deletePic2"
            android:layout_width="@dimen/delete_picture_button_size"
            android:layout_height="@dimen/delete_picture_button_size"
            android:background="@drawable/delete_button"
            app:layout_constraintBottom_toBottomOf="@+id/profilePic2"
            app:layout_constraintEnd_toEndOf="@+id/profilePic2" />
    </ImageView>

但随后按钮消失了,显然是在图像下方。除了为按钮创建单独的动画之外,是否有简单的解决方案?

您可以在 ConstraintLayout 中使用(透明)CardView,将图像和按钮放入 CardView 中并为 CardView 设置动画。