如何在特定帧位置使用MotionLayout旋转TextView

时间:2019-03-24 10:29:56

标签: android-constraintlayout android-motionlayout

我正在尝试将约束布局2.0用于布局和元素动画。我已经创建了一个以运动为父的布局,其中包含另一个线性布局,其中一个线性用于抽屉,另一个用于主视图。当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>

这是我的动态布局场景

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

1 个答案:

答案 0 :(得分:0)

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

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