如何使用关键帧控制边距?

时间:2019-05-27 10:21:38

标签: android android-motionlayout

我想先更改layout_marginStart,而layout_marginStart直到第60帧才应该更改。但是,随后的关键帧将被忽略,并且两个边距同时设置动画。

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


    <Transition

        app:constraintSetEnd="@+id/end"
        app:constraintSetStart="@+id/start"

        >
        <OnSwipe
            app:dragDirection="dragUp"
            app:touchAnchorId="@id/scrollable"
            app:touchAnchorSide="top" />
        <KeyFrameSet>
            <KeyAttribute
                app:curveFit="linear"
                app:framePosition="10"
                app:motionTarget="@+id/text">
                <CustomAttribute
                    app:attributeName="layoutMarginStart"
                    app:customDimension="68dip" />
            </KeyAttribute>
            <KeyAttribute
                app:curveFit="linear"
                app:framePosition="60"
                app:motionTarget="@+id/text">
                <CustomAttribute
                    app:attributeName="layoutMarginEnd"
                    app:customDimension="0dip" />
            </KeyAttribute>

        </KeyFrameSet>
        <ConstraintSet android:id="@+id/start">
            <Constraint
                android:id="@+id/text"
                android:layout_width="0dip"
                android:layout_height="40dip"
                android:layout_marginStart="16dip"

                android:layout_marginTop="24dip"
                android:layout_marginEnd="16dip"
                android:layout_marginBottom="12dip"
                android:background="#BEBEBE"
                app:layout_constraintEnd_toEndOf="@+id/parent"
                app:layout_constraintStart_toStartOf="@+id/parent"
                app:layout_constraintTop_toTopOf="parent" />


        </ConstraintSet>

        <ConstraintSet android:id="@+id/end">
            <Constraint
                android:id="@+id/text"
                android:layout_width="0dip"
                android:layout_height="40dip"
                android:layout_marginStart="60dip"
                android:layout_marginTop="24dip"
                android:layout_marginEnd="60dip"
                android:layout_marginBottom="12dip"
                android:background="#BEBEBE"
                app:layout_constraintEnd_toEndOf="@+id/parent"
                app:layout_constraintStart_toStartOf="@+id/parent"
                app:layout_constraintTop_toTopOf="parent" />


        </ConstraintSet>
    </Transition>

</MotionScene>

1 个答案:

答案 0 :(得分:0)

您的视图上没有setLayoutMarginStartsetLayoutMarginEnd方法。 app:attributeName参数将通过反射工作并搜索那些方法。

一种实现此目的的方法是简单地将View子类化并编写自己的setter。

fun setMarginTop(newMarginTop: Int) {
    val lp = layoutParams as? ViewGroup.MarginLayoutParams ?: return
    lp.run { 
        setMargins(leftMargin, newMarginTop, rightMargin, bottomMargin)
    }
    layoutParams = lp
}

或者如果您使用kotlin-ktx:

fun setMarginTop(newMarginTop: Int) {
    updateLayoutParams<ViewGroup.MarginLayoutParams> {
        updateMargins(top = newMarginTop)
    }
}