Android 视图内部MotionLayout不保留高程更改

Android 视图内部MotionLayout不保留高程更改,android,android-motionlayout,Android,Android Motionlayout,我试图实现我们在协调器布局中所做的动画,比如使用运动布局折叠工具栏。我有以下带有工具栏(framelayout)、imageview和scrollview的运动布局。滑动scrollview会增加工具栏的高度。但当scrollview在工具栏下移动后,其高程(阴影)将不再可见。我不知道为什么阴影不可见 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.mo

我试图实现我们在协调器布局中所做的动画,比如使用运动布局折叠工具栏。我有以下带有工具栏(framelayout)、imageview和scrollview的运动布局。滑动scrollview会增加工具栏的高度。但当scrollview在工具栏下移动后,其高程(阴影)将不再可见。我不知道为什么阴影不可见

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene"
    app:showPaths="true"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv_top"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/top"
        android:scaleType="centerCrop"
        android:src="@drawable/test_vector"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar" />

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/iv_top">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="@string/stub"
            android:textSize="24sp" />

    </androidx.core.widget.NestedScrollView>

    <FrameLayout
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="@color/colorPrimary"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.motion.widget.MotionLayout>


您将阴影放在工具栏上,但我认为您真正想要的是将其放在上下移动的ImageView上

问题是ImageView没有背景,只有一个src。因此,您需要通过添加
android:outlineProvider=“bounds”
来定义阴影的放置位置。现在图像上方也可以看到一点阴影,因此您必须将工具栏提升得更高,并使用android:outlineProvider=“none”
删除它自己的阴影。所有这些更改都在
activty_main.xml

现在,
activity\u main\u scene.xml
变得更小了。只需确保android:layout_height至少保持1dp,否则阴影会随视图消失

这是我使用的代码:

活动\u main.xml:

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

<androidx.constraintlayout.motion.widget.MotionLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene"
    app:showPaths="true"
    tools:context=".MainActivity">
    
    <FrameLayout
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="@color/red4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:elevation="21dp"
        android:outlineProvider="none"/>

    <ImageView
        android:id="@+id/iv_top"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="string/top"
        android:scaleType="centerCrop"
        android:src="@drawable/sof"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        android:elevation="20dp"
        android:outlineProvider="bounds"/>
    
    <androidx.core.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/iv_top">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="string/stub"
            android:textSize="24sp" />

    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.motion.widget.MotionLayout>
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ConstraintSet android:id="@+id/start">
    </ConstraintSet>
    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/iv_top"
            android:layout_height="1dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </ConstraintSet>
    <Transition
        app:constraintSetEnd="@id/end"
        app:constraintSetStart="@+id/start"
        app:duration="1000">
        <OnSwipe
            app:touchAnchorId="@+id/scroll_view"
            app:touchAnchorSide="top" />
    </Transition>
</MotionScene>

activity\u main\u scene.xml:

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

<androidx.constraintlayout.motion.widget.MotionLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene"
    app:showPaths="true"
    tools:context=".MainActivity">
    
    <FrameLayout
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="@color/red4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:elevation="21dp"
        android:outlineProvider="none"/>

    <ImageView
        android:id="@+id/iv_top"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="string/top"
        android:scaleType="centerCrop"
        android:src="@drawable/sof"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        android:elevation="20dp"
        android:outlineProvider="bounds"/>
    
    <androidx.core.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/iv_top">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="string/stub"
            android:textSize="24sp" />

    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.motion.widget.MotionLayout>
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ConstraintSet android:id="@+id/start">
    </ConstraintSet>
    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/iv_top"
            android:layout_height="1dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </ConstraintSet>
    <Transition
        app:constraintSetEnd="@id/end"
        app:constraintSetStart="@+id/start"
        app:duration="1000">
        <OnSwipe
            app:touchAnchorId="@+id/scroll_view"
            app:touchAnchorSide="top" />
    </Transition>
</MotionScene>