【问题标题】:MotionLayout Swipe left and right animation problemMotionLayout 左右滑动动画问题
【发布时间】:2020-07-09 03:46:43
【问题描述】:

我正在创建一个用户可以向左或向右滑动的动作布局,当我添加 layoutDescription layout-width 不再工作时。我希望能够在滑动时调整布局的android:layout_width。这是 MotionSceneMotionLayout 的 XML

   <androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layoutDescription="@xml/table_view_odd_list_shuffle"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="@id/guideline5"
                    app:layout_constraintTop_toBottomOf="@id/constraint_layout_spinners">

                    <RelativeLayout
                        android:id="@+id/relative_layout_odds_column"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent">

                        <androidx.constraintlayout.widget.ConstraintLayout
                            android:id="@+id/constraint_layout_odds"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <androidx.recyclerview.widget.RecyclerView
                                android:id="@+id/table_view_recycle_view_odds_1"
                                android:layout_width="0dp"
                                android:layout_height="0dp"
                                android:nestedScrollingEnabled="false"
                                android:orientation="horizontal"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintEnd_toStartOf="@id/table_view_recycle_view_odds_2"
                                app:layout_constraintStart_toStartOf="parent"
                                app:layout_constraintTop_toTopOf="parent" />

                            <androidx.recyclerview.widget.RecyclerView
                                android:id="@+id/table_view_recycle_view_odds_2"
                                android:layout_width="0dp"
                                android:layout_height="0dp"
                                android:nestedScrollingEnabled="false"
                                android:orientation="horizontal"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintEnd_toEndOf="parent"
                                app:layout_constraintStart_toEndOf="@id/table_view_recycle_view_odds_1"
                                app:layout_constraintTop_toTopOf="parent" />

                            <androidx.constraintlayout.widget.Guideline
                                android:id="@+id/guideline6"
                                android:layout_width="0dp"
                                android:layout_height="0dp"
                                android:orientation="vertical"
                                app:layout_constraintGuide_percent="0.5"
                                app:layout_constraintTop_toBottomOf="parent" />
                        </androidx.constraintlayout.widget.ConstraintLayout>
                    </RelativeLayout>
                </androidx.constraintlayout.motion.widget.MotionLayout>

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

    <!-- center to left-->
    <Transition
        motion:constraintSetEnd="@id/left"
        motion:constraintSetStart="@id/center"
        motion:duration="200">
        <OnSwipe
            motion:dragDirection="dragLeft"
            motion:touchAnchorId="@id/relative_layout_odds_column"
            motion:touchAnchorSide="left" />
    </Transition>
    <!-- center to right -->
    <Transition
        motion:constraintSetEnd="@id/right"
        motion:constraintSetStart="@id/center"
        motion:duration="200">
        <OnSwipe
            motion:dragDirection="dragRight"
            motion:touchAnchorId="@id/relative_layout_odds_column"
            motion:touchAnchorSide="right" />
    </Transition>

    <ConstraintSet android:id="@+id/center">
        <Constraint
            android:id="@id/relative_layout_odds_column"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent">
            <PropertySet android:alpha="1" />
        </Constraint>
    </ConstraintSet>
    <!-- place the view on left-->
    <ConstraintSet android:id="@+id/left">
        <Constraint
            android:id="@id/relative_layout_odds_column"
            android:layout_width="10dp"
            android:layout_height="match_parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent">
            <PropertySet android:alpha="1" />
        </Constraint>
    </ConstraintSet>
    <!-- place the view on right-->
    <ConstraintSet android:id="@+id/right">
        <Constraint
            android:id="@id/relative_layout_odds_column"
            android:layout_width="10dp"
            android:layout_height="match_parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toTopOf="parent">
            <PropertySet android:alpha="1" />
        </Constraint>
    </ConstraintSet>


</MotionScene>

【问题讨论】:

    标签: android android-layout android-animation android-motionlayout


    【解决方案1】:

    场景文件中的约束定义存在问题。使用下面的代码而不是你现在拥有的代码。

            <Constraint
                android:id="@id/relative_layout_odds_column"
                android:layout_width="0dp"
                android:layout_height="0dp"
                motion:layout_constraintEnd_toEndOf="parent"
                motion:layout_constraintBottom_toBottomOf="parent"
                motion:layout_constraintStart_toStartOf="parent"
                motion:layout_constraintTop_toTopOf="parent">
                <PropertySet android:alpha="1" />
            </Constraint>
        </ConstraintSet>
        <!-- place the view on left-->
        <ConstraintSet android:id="@+id/left">
            <Constraint
                android:id="@id/relative_layout_odds_column"
                android:layout_width="10dp"
                android:layout_height="0dp"
                motion:layout_constraintBottom_toBottomOf="parent"
                motion:layout_constraintStart_toStartOf="parent"
                motion:layout_constraintTop_toTopOf="parent">
                <PropertySet android:alpha="1" />
            </Constraint>
        </ConstraintSet>
        <!-- place the view on right-->
        <ConstraintSet android:id="@+id/right">
            <Constraint
                android:id="@id/relative_layout_odds_column"
                android:layout_width="10dp"
                android:layout_height="0dp"
                motion:layout_constraintEnd_toEndOf="parent"
                motion:layout_constraintBottom_toBottomOf="parent"
                motion:layout_constraintTop_toTopOf="parent">
                <PropertySet android:alpha="1" />
            </Constraint>
        </ConstraintSet>
    

    【讨论】:

      猜你喜欢
      • 2011-07-06
      • 1970-01-01
      • 2021-07-05
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 2017-04-19
      相关资源
      最近更新 更多