3

我正在尝试将约束布局 2.0 用于布局和元素动画。我创建了具有运动的布局作为父级,其中包含另一个线性布局,其中 1 个线性用作抽屉,另一个用作主视图。我想旋转文本视图当 linear_pink 框架位置为 50 时为 20 度。

这是我的 MotionLayoutFile

<android.support.constraint.motion.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"
    tools:context=".MainActivity"
    app:layoutDescription="@xml/scene_01"
    app:showPaths="true">

    <android.support.constraint.Guideline
        android:id="@+id/guideline_01"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.3"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline_02"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline_03"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.9"/>

    <LinearLayout
        android:id="@+id/linear_green"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorPrimary"
        android:gravity="center">

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="One"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:padding="4dp"/>
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Two"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:padding="4dp"/>

        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yhree"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:padding="4dp"/>
    </LinearLayout>


    <LinearLayout
        android:id="@+id/linear_pink"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.motion.MotionLayout>

这是我的motionlayout场景

<?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:constraintSetStart="@id/start"
        app:constraintSetEnd="@id/end"
        app:duration="1000">

        <OnSwipe
            app:touchAnchorId="@id/linear_pink"
            app:touchAnchorSide="right"
            app:dragDirection="dragRight"
            app:maxVelocity="1"/>

        <KeyFrameSet>

            <KeyAttribute
                app:target="@id/text1"
                app:framePosition="20"
                android:alpha="1"
                android:rotationX="90">
                    <CustomAttribute
                        app:attributeName="TextColor"
                        app:customColorValue="@android:color/holo_red_dark" />
            </KeyAttribute>

            <KeyAttribute
                app:target="@id/text2"
                app:framePosition="40"
                android:alpha="1"/>

            <KeyAttribute
                app:target="@id/text3"
                app:framePosition="60"
                android:alpha="1"/>

        </KeyFrameSet>

    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint android:id="@id/linear_green">
            <Layout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:background="@color/colorPrimary"/>
        </Constraint>
        <Constraint android:id="@id/linear_pink">
            <Layout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:background="@color/colorAccent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@id/linear_green">
            <Layout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="@id/guideline_03"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@id/guideline_01"
                app:layout_constraintTop_toTopOf="@id/guideline_02"
                android:background="@color/colorPrimary"/>
        </Constraint>
        <Constraint android:id="@id/linear_pink">
            <Layout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:background="@color/colorAccent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="@id/guideline_01"
                app:layout_constraintTop_toTopOf="parent"/>
        </Constraint>
    </ConstraintSet>

</MotionScene>
4

1 回答 1

4

为此设置几个关键帧。第一个关键帧声明在动画的 40% 之前不旋转 textview,第二个声明在第 50 帧完成旋转。如果要保持该旋转,还需要在 ConstaintSetend旋转 20中添加约束

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

    <KeyFrameSet>
        <KeyAttribute

            android:rotation="0"
            motion:framePosition="40"
            motion:target="@id/textview" />
    </KeyFrameSet>
    <KeyFrameSet>
        <KeyAttribute

            android:rotation="20"
            motion:framePosition="50"
            motion:target="@id/textview" />
    </KeyFrameSet>
</MotionScene>
于 2019-05-25T11:14:09.607 回答