【发布时间】:2023-01-24 06:40:00
【问题描述】:
我在弄清楚如何使用 MotionLayout 为类似 Youtube 的底部播放器栏制作动画时遇到了很多麻烦。
在使用the official examples 和大量谷歌搜索尝试了解这种布局几个小时后,我偶然发现了this question,它有一个 GIF chowing确切地我想要达到的目标:
不幸的是,没有完整的代码,所以我无法理解作者是如何做到的。
我现在只有这个:
这里有两个问题:动画立即从 0 跳到 100 并且视图没有崩溃,它一直占据整个屏幕。
我不知道它是否相关,但 UI 的那部分包含在一个片段中。这是活动布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/surface"
app:layout_constraintBottom_toTopOf="@id/appbar_wrapper"
app:layout_constraintTop_toTopOf="parent">
<!--
This is the fragment that contains the view that lies
beneath the player controls. It's not visible on the image above
-->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize"
app:defaultNavHost="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:navGraph="@navigation/main_nav"
tools:layout="@layout/fragment_artists" />
<!--
This is the fragment that contains the fragemtn that contains
the controls that won't collapse
-->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/now_playing"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="audio.funkwhale.ffa.fragments.NowPlayingFragment" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我片段的用户界面:
<?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">
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/now_playing_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/elevatedSurface"
android:orientation="vertical"
app:motionDebug="SHOW_ALL"
app:layoutDescription="@xml/fragment_now_playing_scene">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent" />
<SquareImageView
android:id="@+id/now_playing_details_cover"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
android:padding="8dp"
app:srcCompat="@drawable/cover"
tools:src="@tools:sample/avatars" />
<!-- The rest of the UI is stripped to keep the code short -->
</androidx.constraintlayout.motion.widget.MotionLayout>
</layout>
和我的场景:
<?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">
<Transition
app:constraintSetEnd="@id/end"
app:constraintSetStart="@+id/start"
app:duration="1000"
app:motionInterpolator="linear" >
<OnSwipe
app:dragDirection="dragUp"
app:touchAnchorId="@+id/header"
app:touchAnchorSide="top" />
<KeyFrameSet>
<KeyAttribute
android:alpha="0"
app:framePosition="75"
app:motionTarget="@id/now_playing_details_controls" />
</KeyFrameSet>
<ConstraintSet android:id="@+id/start">
<Constraint android:id="@+id/header" />
<Constraint
android:id="@id/now_playing_details_cover"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"/>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint android:id="@id/header" />
<Constraint
android:id="@id/now_playing_details_cover"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="@id/header"
app:layout_constraintBottom_toBottomOf="@id/header"
app:layout_constraintStart_toStartOf="@id/header" />
</ConstraintSet>
</Transition>
</MotionScene>
【问题讨论】:
标签: android androidx android-motionlayout