标签: android-motionlayout

如何在 MotionLayout 中以编程方式设置过渡持续时间?

我在这样的代码中手动设置了一个新的过渡:

intro_motion_layout.setTransition(R.id.middle, R.id.end)
Run Code Online (Sandbox Code Playgroud)

有没有办法我也可以这样设置持续时间?

ConstraintSets在 XML 中定义了多个,当第一次转换完成时,我使用onTransitionCompleted. Transition我的 XML 中有一个,它在那里正确使用了持续时间,但Transitions我在代码中设置的以下内容发生得非常快,我找不到改变它们的持续时间的方法。

android android-motionlayout

5
推荐指数
2
解决办法
4220
查看次数

Android Studio 找不到 MotionLayout 的类

我正在尝试使用 android MotionLayout,但它找不到合适的类并且无法工作。

我尝试实现以下依赖项:

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta1'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha2'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha4'
Run Code Online (Sandbox Code Playgroud)

但它们似乎都不起作用......

这是我的 XML 文件:

<android.support.constraint.motion.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
footstools="http://schemas.android.com/tools"
android:id="@+id/motionLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showPaths="true"
tools:context=".PlayGameActivity">

<View
    android:id="@+id/button"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:background="@color/colorAccent"
    android:text="Button" />

</android.support.constraint.motion.MotionLayout >'
Run Code Online (Sandbox Code Playgroud)

但它总是给我这个错误按摩:

Missing classes
The following classes could not be found:
- android.support.constraint.motion.MotionLayout (Fix Build Path, Edit XML, Create Class)
Tip: Try to build the project.   
Tip: Try to refresh the layout.
Run Code Online (Sandbox Code Playgroud)

在项目的预览窗口中,它以灰色填充整个布局并在中心写入:

android...MotionLayout
Run Code Online (Sandbox Code Playgroud)

android android-layout android-studio android-motionlayout

5
推荐指数
1
解决办法
1248
查看次数

MotionLayout 中的 KeyAttribute 在视图“可见性”更改时被忽略

在滑动动画之后,我正在使用MotionLayout<MotionScene />为bottomSheetView内容设置动画。

开始结束场景之间,我想view_player_status_margin使用从gone到的可见性来显示一个视图visible(不使用,alpha因为另一个视图通过约束链接到我想要显示的视图)。

可以使用标准<Constraint />in <ConstraintSet />,但是当我将可见性链接到 a<KeyAttribute />以使视图仅出现在动画的最后一帧时,它不会遵循特定于帧的规则位置。

我的使用<KeyAttribute />似乎是正确的,因为它适用于其他两个视图的 alpha。

使用KeyAttributewithvisibility属性是否有特定限制?

<?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/player_scene_set_expanded"
        app:constraintSetStart="@+id/player_scene_set_collapsed"
        app:motionInterpolator="easeIn">

        <KeyFrameSet>

            <KeyAttribute
                android:visibility="gone"
                app:framePosition="80"
                app:motionTarget="@id/view_player_status_margin" />

            <KeyAttribute
                android:alpha="0"
                app:framePosition="20"
                app:motionTarget="@id/view_player_collapsed" />

            <KeyAttribute
                android:alpha="1"
                app:framePosition="20"
                app:motionTarget="@id/view_player_expanded" />

        </KeyFrameSet>

    </Transition>

    <ConstraintSet android:id="@+id/player_scene_set_collapsed">

        <Constraint
            android:id="@+id/view_player_collapsed"
            android:layout_width="0dp"
            android:layout_height="64dp"
            android:alpha="1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" /> …
Run Code Online (Sandbox Code Playgroud)

android android-motionlayout

5
推荐指数
1
解决办法
997
查看次数

Android MotionLayout autoTransition 在 beta7 中不起作用

我想在我的片段显示时自动启动动画。我已将该autoTransition属性设置为animateToEnd,但没有任何反应。我目前在constraintLayout beta7。当降级到 beta6 或更低版本时,除了片段进入屏幕时动画完成之外,什么也不会发生。我已经将 TransitionListener 附加到 MotionLayout 并观察到以下内容:

  • beta7 OnTransitionChange 被调用一次,进度值如此之低,我将其称为 0,OnTransitionEnd 从未被调用,并且我的 TextView 保持不可见
  • beta6或更低版本上OnTransitionChange 被调用两次,第一次使用 0 到 0.5 之间相当随机的进度值,第二次使用 1.0。之后,调用 OnTransitionEnd 并且我的 TextView 立即可见,那里没有动画。

这是 MotionScene xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000"
        motion:motionInterpolator="easeInOut"
        motion:autoTransition="animateToEnd">
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint android:id="@id/splash_head_TextView">
            <Layout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                motion:layout_constraintEnd_toEndOf="parent"
                motion:layout_constraintStart_toStartOf="parent"
                motion:layout_constraintTop_toTopOf="@id/topGuideLine"/>
            <CustomAttribute
                motion:attributeName="alpha"
                motion:customFloatValue="0.0" />
        </Constraint>

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@id/splash_head_TextView">
            <Layout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/splash_head"
                motion:layout_constraintEnd_toEndOf="parent"
                motion:layout_constraintStart_toStartOf="parent"
                motion:layout_constraintTop_toTopOf="@id/topGuideLine"/>
            <CustomAttribute
                motion:attributeName="alpha" …
Run Code Online (Sandbox Code Playgroud)

xml android android-animation android-layout android-motionlayout

5
推荐指数
1
解决办法
1207
查看次数

将 RecyclerView 触摸传递给 MotionLayout 不起作用

我在图片上有如下布局。对于动画,我使用 MotionLayout。当用户触摸搜索时,我会向上滚动视图,因此搜索成为最顶部的视图。在搜索下。我在 RecyclerView 中有项目列表。我想通过向下滚动列表项来恢复向上滚动。但我找不到办法做到这一点。我无法从 Recyclerview 获得触摸事件。

在此处输入图片说明

这是我的布局代码:

<?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:id="@+id/manage_wallets"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:showPaths="true"
    app:layoutDescription="@xml/manage_wallets_fragment_scene">

    <com.example.views.ShadowlessToolbarView
        android:id="@+id/customToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.example.views.CoinSearchView
        android:id="@+id/searchView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/customToolbar" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="12dp"
        android:clipToPadding="false"
        android:paddingBottom="32dp"
        android:nestedScrollingEnabled="false"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchView"
        tools:itemCount="4"
        tools:listitem="@layout/view_holder_item" />

</androidx.constraintlayout.motion.widget.MotionLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的场景代码:

<ConstraintSet android:id="@+id/start">
    <Constraint android:id="@+id/customToolbar" />
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
    <Constraint android:id="@+id/customToolbar"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_width="match_parent"
        app:layout_constraintBottom_toTopOf="parent" />
</ConstraintSet>

<Transition
    app:constraintSetEnd="@+id/end"
    app:constraintSetStart="@+id/start" >
    <OnClick
        app:targetId="@+id/searchView" …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview android-motionlayout

5
推荐指数
0
解决办法
615
查看次数

MotionLayout 组可见性问题

我有一个约束布局,其中包含一个 RecyclerView、一个 TextInputEditText、一个约束组(它处理 RecyclerView 和 TextInputEditText、一个 ImageView 和一个 LottieAnimationView 的可见性。

它是这样工作的:

  • 在启动时,只有 LottieAnimation 可见,直到我的 LiveData 从 repo 获取数据并成功更新。

  • 获取数据后,LottieView 被隐藏并显示 Group 可见性,后者依次显示 RecyclerView 和 TextInputEditText。

  • 仅当出现错误时才会显示 ImageView,然后隐藏除 ImageView 之外的所有其他视图。

现在我希望当我滚动 RecyclerView 时,TextInputEditText 也会滚动。(由于滚动时分页滞后,我无法使用 NestedScrollView)。

我使用了 MotionLayout。除了它不能正常工作。我的能见度坏了。我的 Lottie 动画停留在屏幕上。我的布局是这样的:

片段.xml:

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

    <data>

        <variable
            name="ViewModel"
            type="...AllServicesViewModel" />

        <import type="android.view.View" />

    </data>

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/Root_All_Services"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/scene_all_services_header">

        <ImageView
            android:id="@+id/Illus_Error"
            android:layout_width="350dp"
            android:layout_height="350dp"
            android:layout_margin="16dp"
            android:contentDescription="@string/icd_error_services_unavailable"
            android:scaleType="centerCrop"
            android:src="@drawable/il_error"
            android:visibility="@{ViewModel.message != null ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:src="@drawable/il_error" …
Run Code Online (Sandbox Code Playgroud)

android android-xml kotlin android-constraintlayout android-motionlayout

5
推荐指数
1
解决办法
345
查看次数

MotionLayout 和约束组

我将开始使用MotionLayout.
ConstraintLayout具有简单运动场景的 MotionLayout替换现有后,
我注意到它androidx.constraintlayout.widget.Group不再起作用了。
最初,我根据条件显示了两个组中的一个,
但现在它们都可见,即使我将可见性设置为GONE.
我能否以某种方式让 MotionLayout 与 Groups 一起工作,或者我应该使用不同的 MotionLayouts?

android android-motionlayout

4
推荐指数
1
解决办法
792
查看次数

MotionLayout 忽略填充

我正在尝试在滑动时对文本视图的边距、填充和 alpha 进行动画处理。一开始,textview没有padding,alpha为1。最后,我希望alpha为0,padding为100dp,两侧边距为16dp dp,alpha为0.5 一切正常,除了padding是没有改变。MotionLayout 支持填充吗?我正在使用 androidx.constraintlayout:constraintlayout:2.0.0-beta4。

添加更多文本,因为 SO 说我的帖子看起来主要是代码...添加更多文本,因为 SO 说我的帖子看起来主要是代码...添加更多文本,因为 SO 说我的帖子看起来主要是代码.. 。

布局 XML:

    <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:id="@+id/layoutParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/test">

    <TextView
        android:id="@+id/fakenavigationBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="hello world">
    </TextView>
</androidx.constraintlayout.motion.widget.MotionLayout>
Run Code Online (Sandbox Code Playgroud)

运动场景:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@id/fakenavigationBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alpha="1"
            android:padding="100dp"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintStart_toStartOf="parent">
        </Constraint>

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/fakenavigationBar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:alpha="0.5"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent">
        </Constraint>

    </ConstraintSet>

    <Transition
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/end"
        motion:duration="2000"
        motion:motionInterpolator="linear">

        <OnSwipe
            motion:touchAnchorId="@+id/fakenavigationBar"
            motion:touchAnchorSide="top"
            motion:dragDirection="dragDown"/>
    </Transition>
</MotionScene>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述在此输入图像描述

animation android padding android-motionlayout

4
推荐指数
1
解决办法
3885
查看次数

自动转换在 Motion 布局中不起作用

我目前正在学习动作布局。我创建了一个小应用程序,其中中心的图像旋转 5 秒。onClick 和 onSwipe 属性在设备上工作正常,但使用 autotransition = animateToEnd 当我在设备上运行应用程序时没有任何反应。基本上我希望我的转换在活动开始时自动发生。这是我的运动布局文件:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene">

    <ImageView
        android:id="@+id/back"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="TODO"
        android:src="@drawable/background" />

    <ImageView
        android:id="@+id/moon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_moon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.motion.widget.MotionLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的运动场景:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="5000"
        motion:autoTransition="animateToEnd">
       <KeyFrameSet>
           <KeyCycle
               motion:motionTarget="@+id/moon"
               motion:framePosition="0"
               motion:wavePeriod="2"
               motion:waveShape="sawtooth"
               android:rotation="180"
               motion:waveOffset="0" />
       </KeyFrameSet>
    </Transition>

    <ConstraintSet android:id="@+id/start">
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
    </ConstraintSet>
</MotionScene>
Run Code Online (Sandbox Code Playgroud)

如果有任何替代方法可以通过运动布局运行过渡,请告诉我们。**为了运行应用程序,我曾经创建调试 apk,然后在我的设备上运行它。

android android-motionlayout

4
推荐指数
1
解决办法
3820
查看次数

Jetpack Compose 中的 MotionLayout

这可能有点快,但应该有一个解决方法

长话短说

有没有办法在MotionLayout中使用Jetpack Compose 的可组合组件? MotionLayout或Compose的路线图中是否有这样的组合?


Jetpack compose 中是否有替代方案可以执行相同的操作?

由于任何想要在运动场景中修改的视图,合成功能都不能在运动场景中修改。

问题

我想在 a 的中心对图像进行动画处理Surface,并在特定的持续时间内将其放大或缩小。

使用运动,您可以在每个步骤中定义关键帧。

Ui 是使用 Composable 函数制作的如何在 Compose 中使用 MotionLayout 做我能做的事情?


Jetpack 撰写:v1.0.0-alpha02

android android-motionlayout android-jetpack-compose

4
推荐指数
1
解决办法
6764
查看次数