如何在特定帧位置使用动态布局旋转文本视图



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

这是我的运动布局文件

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

设置了多个关键帧。第一个关键帧声明在动画完成 40% 之前不旋转文本视图,第二个关键帧声明在第 50 帧完成旋转。如果要保留该旋转,则还需要在 ConstaintSet 中添加约束end旋转 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>

最新更新