【问题标题】:Use AndroidX motion layout to implement a player bottom bar (like Youtube)使用 AndroidX motion layout 实现播放器底部栏(如 Youtube)
【发布时间】: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


    【解决方案1】:

    我怀疑很多事情都是错误的,我注意到一些事情: 表示跟踪@id/header 顶部的向上移动
    但是开始和拥有完全一样 这意味着标题确实移动了 因为app:layout_constraintTop_toTopOf="parent" + height="0dp" 意味着它的顶部被固定。

    一般来说,在你考虑滑动部分之前。 专注于让两个约束集具有您想要的开始和结束状态。 这通常涉及修改 ConstraintSet 中的约束 如果它按照你想要的方式布置

    然后onSwipe anchor需要和移动的物体一起

    有关 onSwipe 的简要概述,请参阅https://youtu.be/XtnAZXM26wQ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 2019-03-05
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多