标签: android-motionlayout

在 MotionLayout 中以编程方式设置边距

我有一些视图需要以编程方式设置一些边距(来自 applyWindowInsets 侦听器),但这些视图似乎忽略了我用代码设置的任何边距,即使我没有为边距设置动画。

我可以很好地设置填充,但是仅使用填充无法完成我需要的操作。

该问题似乎与 MotionLayout 相关,因为如果它是 ConstraintLayout,它就可以正常工作。

我一直在使用这个 util 方法。

public static void addTopMargin(View v, int margin) {
    ((ViewGroup.MarginLayoutParams) v.getLayoutParams()).topMargin += margin;
}
Run Code Online (Sandbox Code Playgroud)

android android-motionlayout

7
推荐指数
1
解决办法
1087
查看次数

android:textSize 在 MotionLayout 中的变化

我将使用 MotionLayout 为文本大小更改设置动画。

我为开始状态执行以下操作

<CustomAttribute
    motion:attributeName="textSize"
    motion:customDimension="16sp" />
Run Code Online (Sandbox Code Playgroud)

以及以下结束状态

<CustomAttribute
    motion:attributeName="textSize"
    motion:customDimension="14sp" />
Run Code Online (Sandbox Code Playgroud)

结果,看起来大小实际上在变化,但它比14sp-16sp大得多

那么,如何正确改变文字大小呢?

android android-layout android-motionlayout

7
推荐指数
3
解决办法
4244
查看次数

带有滑动手势的运动布局 + SwipeRefreshLayout + RecyclerView 错误行为向上滚动

我正在使用 MotionLayout 构建包含 2 个部分的 UI - 顶部有一些视图,底部有 SwipeRefresh 和 RecyclerView 内部。另外,我有一个 MotionLayout 手势 - SwipeRefresh 在向上滑动时向上移动到顶视图上方。问题是当我将 RecyclerView 滚动到底部(顶视图“折叠”)然后到顶部时 - MotionLayout 开始立即反转我的过渡(“展开”) - 当 RecyclerView 没有完全滚动到顶部而不是滚动 RecyclerView第一的。当我的 SwipeRefresh 正在更新或刷新时,它可以正常工作。禁用它会导致刷新布局进度条在没有动画的情况下消失 - 这不是一个好的解决方案。任何解决方法?

布局 xml 要点

布局场景要点

android android-animation android-layout android-recyclerview android-motionlayout

7
推荐指数
1
解决办法
2614
查看次数

Android MotionLayout setGuidelinePercent 不适用于 alpha4 版本

我的应用程序中有一个用于滑动过渡的 MotionLayout。目前我刚刚更新2.0.0-alpha32.0.0-alpha4发布,有些事情不像以前那样工作。有一个“myView”,它是一个应该从屏幕底部扩展到标题视图的布局。它对指南有一个约束顶部,我需要以编程方式控制(基于一些用户操作)。

代码中没有其他更改,因此降级到 alpha3 会使一切按预期工作。

代码非常基本:

<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:interpolator="linear">
        <!-- I used motion:motionInterpolator="linear" for alpha4-->

        <OnSwipe
            motion:dragDirection="dragUp"
            motion:touchAnchorId="@id/myView"
            motion:touchAnchorSide="top" />

    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@id/myView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="@id/myGuideline" />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/myView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/someHeaderView" />
    </ConstraintSet>
</MotionScene>
Run Code Online (Sandbox Code Playgroud)

问题是在 alpha3 版本中,我可以使用这样的东西(并且它有效):

myMotionLayout.getConstraintSet(R.id.start)?.setGuidelinePercent(R.id.myGuideline, guidelinePercentage)
Run Code Online (Sandbox Code Playgroud)

现在我升级到2.0.0-alpha4,这不再有效,我不明白为什么。它始终采用 xml 中的默认值,并且在我尝试动态执行时永远不会更改。

我想要做的是根据一些动态数据(在 Fragment 的开头或用户返回时可用)更改过渡的起点。如果您对我如何使用 MotionLayout 实现这一点有更好的想法,请给我一个提示。

摇篮实现:

implementation "com.android.support.constraint:constraint-layout:2.0.0-alpha4"
Run Code Online (Sandbox Code Playgroud)

android android-layout kotlin android-motionlayout

6
推荐指数
1
解决办法
1172
查看次数

是否可以使用 MotionLayout 为组可见性设置动画?

我正在尝试使用 MotionLayout 制作动画,我需要隐藏一些元素。我在单个元素中测试了可见性属性并且它有效,但是为了使 XML 更短,我希望能够仅指定一个包含所有这些元素的组(来自 ConstraintLayout 助手)

像这样的东西

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        app:constraintSetStart="@id/start"
        app:constraintSetEnd="@id/end"

        app:duration="300">

        <OnSwipe
            app:touchAnchorId="@id/details_group"
            app:touchAnchorSide="bottom"
            app:dragDirection="dragDown"
            />

    </Transition>

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

        <Constraint
            android:id="@+id/details_group"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="gone"
            app:constraint_referenced_ids="detail_value_topl,detail_icon_topl,detail_value_topr" />

    </ConstraintSet>

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

        <Constraint
            android:id="@+id/details_group"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="visible"
            app:constraint_referenced_ids="detail_value_topl,detail_icon_topl,detail_value_topr" />

    </ConstraintSet>

</MotionScene>
Run Code Online (Sandbox Code Playgroud)

但它不起作用,知道如何使它起作用吗?

另外,我不想使用 alpha,因为所有约束都已设置,以便当它们消失时容器会调整大小

android android-motionlayout

6
推荐指数
1
解决办法
2969
查看次数

淡入淡出在 MotionLayout 中不起作用

我正在尝试使用 alpha 和 motionLayout 制作淡入淡出的效果,但似乎不起作用。

这是我想要淡出的 imageView。

    <ImageView
    android:id="@+id/tijeras"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:contentDescription="@string/tijeras"
    android:src="@drawable/ic_tijeras" />
    </android.support.constraint.motion.MotionLayout>
Run Code Online (Sandbox Code Playgroud)

这是运动文件

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Transition
        app:duration="2000"
        app:constraintSetStart="@+id/start"
        app:constraintSetEnd="@+id/end">
        <KeyFrameSet>
            <KeyAttribute
                app:framePosition="0"
                app:motionTarget="@id/tijeras"
                android:alpha="1.0"/>
            <KeyAttribute
                app:framePosition="50"
                app:motionTarget="@id/tijeras"
                android:alpha="0.0"/>
        </KeyFrameSet>
    </Transition>
    <ConstraintSet android:id="@+id/start">
        <Constraint android:id="@+id/tijeras"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="1.0"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/logo"/>
    </ConstraintSet>
    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@+id/tijeras"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="1.0"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/logo"/>
    </ConstraintSet>
</MotionScene>
Run Code Online (Sandbox Code Playgroud)

我可以看到图像,但它没有进行 alpha 输入和输出。任何的想法?我需要触发它才能启动吗?

android android-transitions android-motionlayout

6
推荐指数
1
解决办法
2438
查看次数

MotionLayout:单击和触摸在运动场景中的过渡时不起作用(像 UI 一样的 Youtube 播放器)

我有一个类似视图的 Youtube 视频播放器,我有一个列表,点击后会在下一个屏幕中播放视频。

我已将 Motionscene 添加到视频视图中,因此在向下拖动时,视频视图变小。但是这样做时 onClick 或 onTouch 事件不适用于播放/暂停控件。

在 youtube 上,我们可以看到两者都运行良好。所以我想知道我哪里出错了。

运动场景:

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@id/collapsed"
        motion:constraintSetStart="@id/expanded"
        motion:duration="100">

        <OnSwipe
            motion:dragDirection="dragDown"
            motion:maxAcceleration="200"
            motion:touchAnchorId="@+id/player"
            motion:touchAnchorSide="bottom" />

    </Transition>

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

        <Constraint
            android:id="@id/player"
            android:layout_height="200dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />

        <Constraint
            android:id="@id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="visible"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/player" />

    </ConstraintSet>

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

        <Constraint
            android:id="@id/player"
            android:layout_height="85dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent" />

        <Constraint
            android:id="@id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="gone"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/player" />

    </ConstraintSet>
</MotionScene>
Run Code Online (Sandbox Code Playgroud)

主布局:

<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/youtube_scene"
    tools:context=".ProfileActivity"
    tools:showPaths="true">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/player"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_constraintBottom_toTopOf="@id/scrollView" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-constraintlayout android-motionlayout

6
推荐指数
1
解决办法
1123
查看次数

使用 MotionLayout 并使用数据绑定设置可见性失败

我正在使用implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1',并启用了数据绑定。

当我的看法

<ImageView
            android:id="@+id/im_lightning"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="4dp"
            android:contentDescription="@string/charging"
            android:src="@drawable/ic_lightning"
            android:visibility="@{batteryViewModel.liveData.charging ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="@id/tv_percent"
            app:layout_constraintRight_toLeftOf="@id/tv_percent"
            app:layout_constraintTop_toTopOf="@id/tv_percent"
            app:layout_constraintVertical_bias="1.0" />
Run Code Online (Sandbox Code Playgroud)

围绕协调器布局,其可见性变化按预期发生。一旦我将它包装在 MotionLayout 中,可见性更改就不会像以前一样工作。准确地说,视图在应该可见的时候不可见。它在触发事件后变为可见一秒钟,然后恢复为不可见。这是一个已知的错误?

需要时的代码:

协调器布局:

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBackground">
        <ImageView (same as above) />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

运动布局:

<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"
    tools:context=".ui.MainActivity">

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/motion_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBackground"
        app:layoutDescription="@xml/home_scene_0"
        app:showPaths="true">

        <ImageView (same as above) />
    </androidx.constraintlayout.motion.widget.MotionLayout>
    <data>
        <import type="android.view.View" />
        <variable
            name="batteryViewModel"
            type="rish.crearo.minimalphone.viewmodels.BatteryViewModel" />
    </data>
</layout>
Run Code Online (Sandbox Code Playgroud)

android android-coordinatorlayout android-motionlayout

6
推荐指数
1
解决办法
2797
查看次数

底单+Android MotionLayout 实现

我正在尝试使用 MotionLayout 实现底部工作表。它在一个微不足道的情况下工作 - 当底部工作表应该仅在屏幕的一半中可见时(例如)。但是当底部工作表扩展并填满整个屏幕时,我无法让它工作。

在此处输入图片说明

所以这里可以是 3 个状态:

  1. 底部工作表隐藏
  2. 底部工作表填满了半个屏幕
  3. 底页填满整个屏幕

这是布局:

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

         <FrameLayout
            android:id="@+id/fragmentPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="570dp"
            android:elevation="8dp"> some content here </FrameLayout>


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

这里570dp等于半屏(例如)

以及内容scene_description.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">

    <Transition
        android:id="@+id/transition1"
        app:constraintSetStart="@id/bottomSheetHidden"
        app:constraintSetEnd="@id/bottomSheetHalfOpen"
        app:duration="300">

        <OnSwipe
            app:dragDirection="dragDown"
            app:onTouchUp="autoCompleteToStart"
            app:touchAnchorId="@+id/fragmentPlaceholder"
            app:touchAnchorSide="bottom"/>

    </Transition>


    <Transition
        android:id="@+id/transition2"
        app:constraintSetStart="@id/bottomSheetHalfOpen"
        app:constraintSetEnd="@id/bottomSheetOpenFullScreen"
        app:duration="300">

    </Transition>


    <Transition
        android:id="@+id/transition3"
        app:constraintSetStart="@id/bottomSheetHidden"
        app:constraintSetEnd="@id/bottomSheetOpenFullScreen"
        app:duration="300">

        <OnSwipe
            app:dragDirection="dragDown"
            app:touchAnchorId="@+id/fragmentPlaceholder"
            app:touchAnchorSide="bottom" />

    </Transition>


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

        <Constraint android:id="@id/fragmentPlaceholder">
            <Layout
                android:layout_width="match_parent"
                android:layout_height="570dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="parent"/> <!-- below screen, not …
Run Code Online (Sandbox Code Playgroud)

java android kotlin android-jetpack android-motionlayout

6
推荐指数
1
解决办法
2092
查看次数

Resources$NotFoundException:无法使用 NestedScrollView 和 Motionlayout 找到资源 ID #0xffffffff

NestedScrollView当与 结合使用时Motionlayout,我收到上述写入错误。这里奇怪的是,只有当其中有一些项目(或,等)并且当我尝试向上移动时,才会发生错误。NestedscrollviewConstraintlayouttextviewbuttonedittextNestedScrollView

使用 aScrollView解决了这个问题,但是滚动不起作用......

BaseXML(用于 MotionLayout)

<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="product"
            type="com.example.app.framework.datasource.models.product.Product" />
    </data>

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/shop_item_content_scene"
        app:showPaths="true"
        tools:context=".framework.ui.view.fragments.shop.ShopItemFragment">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/shop_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
            app:menu="@menu/shop_item_toolbar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/product_image"
            android:layout_width="0dp"
            android:layout_height="142dp"
            android:layout_marginStart="72dp"
            android:layout_marginTop="@dimen/standard8dpMargin"
            android:layout_marginEnd="72dp"
            android:adjustViewBounds="true"
            android:background="@color/color_white"
            android:contentDescription="@null"
            android:fitsSystemWindows="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/shop_toolbar"
            tools:src="@drawable/ic_example_logo" />

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/product_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/big16dpMargin"
            android:layout_marginBottom="12dp"
            android:ellipsize="end"
            android:fontFamily="sans-serif-medium"
            android:maxLines="2"
            android:text="@{product.name}"
            android:textAlignment="textStart"
            android:textColor="@color/color_text_dark"
            android:textSize="@dimen/textHeadlineNormal1"
            app:layout_constraintBottom_toTopOf="@id/scrollview_shop"
            app:layout_constraintEnd_toEndOf="@id/barrier"
            app:layout_constraintStart_toStartOf="@id/margin_left"
            app:layout_constraintTop_toBottomOf="@+id/product_image" …
Run Code Online (Sandbox Code Playgroud)

android android-animation kotlin android-motionlayout

6
推荐指数
1
解决办法
2432
查看次数