【问题标题】:View inside MotionLayout not retaining the elevation change在 MotionLayout 内部查看不保留高程变化
【发布时间】:2020-07-04 22:57:39
【问题描述】:

我试图实现我们在协调器布局中所做的动画,例如使用运动布局折叠工具栏。我有以下带有工具栏(框架布局)、图像视图和滚动视图的运动布局。工具栏的高度随着滚动视图的滑动而增加。但是在滚动视图在工具栏下移动后,它的高度(阴影)不再可见。我不确定为什么阴影不可见。

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

我的动态场景是:

    <?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="0dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <Constraint
            android:id="@id/toolbar"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:elevation="20dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="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>

【问题讨论】:

    标签: android android-motionlayout


    【解决方案1】:

    你把阴影放在工具栏上,但我认为你真正想要的是把它放在上下移动的 ImageView 上。

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

    activity_main_scene.xml 现在变得更小了。只要确保android:layout_height 至少保持 1dp,否则阴影会随着视图消失。

    这是我使用的代码:

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

    activity_main_scene.xml:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 2019-01-04
      相关资源
      最近更新 更多