如何在Kotlin中动态创建KeyFrameSet

3

我使用XML设计创建了一个Android Kotlin Motion的测试程序。我需要添加许多KeyPositions和KeyAttributes。我需要通过编程而非XML来完成此操作。我已经查阅了文档,但没有找到什么有用的信息。是否有人可以提供示例或将我指向相关文档?(在Kotlin中以编程方式实现,而非XML)

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.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:id="@+id/motion_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/btn_1"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:background="@drawable/btn_round"
        android:backgroundTint="#F4511E"
        android:text="Button"
        android:src="@drawable/jonathan"/>
</androidx.constraintlayout.motion.widget.MotionLayout>

activity_main_scene.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"
    android:id="@+id/this_scene">
    <Transition
        android:id="@+id/this_trans"
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/end"
        motion:duration="2000"
        motion:motionInterpolator="linear"
        >
        <OnClick
            motion:targetId="@id/btn_1"
            motion:clickAction="toggle" />
        <KeyFrameSet>
            <KeyPosition
                motion:framePosition="50"
                motion:motionTarget="@id/btn_1"
                motion:keyPositionType="parentRelative"
                motion:percentX="0.8"
                motion:percentY="0.4"
                />
<!--*** Here I need many more (programmatically)-->
            <KeyAttribute
                motion:framePosition="50"
                motion:motionTarget="@id/btn_1"
                android:scaleY="2.0"
                android:scaleX="2.0"
                android:rotation="-360"
                />
<!--*** Here I need many more (programmatically)-->
        </KeyFrameSet>
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/btn_1"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="0dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintEnd_toEndOf="parent" />
          </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/btn_1"
            android:layout_width="108dp"
            android:layout_height="108dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            />
          </ConstraintSet>
</MotionScene>

请在此处查看 https://medium.com/google-developers/introduction-to-motionlayout-part-ii-a31acc084f59,希望对您有所帮助。 - Karthick Nagarajan
很遗憾,该示例仅使用XML,而不是动态的。 - Robert
1个回答

0

是的,您可以通过编程方式创建关键帧。但请注意,确保使用最新的约束布局版本(例如:目前为2.1.4),否则某些方法对开发人员不可见。

 fun createMotionScene(motionLayout: MotionLayout) {
    val motionScene = MotionScene(motionLayout)
    // create your transition start state here
    val startSet = ConstraintSet()
    val startRoundedAttr = ConstraintAttribute("RoundPercent", ConstraintAttribute.AttributeType.FLOAT_TYPE, 0.000001f, false)
    startSet.getConstraint(R.id.iv_screen_v3).mCustomConstraints["RoundPercent"] = startRoundedAttr

    // create your transition end state
    val endSet = ConstraintSet()
    val endRoundedAttr = ConstraintAttribute("RoundPercent", ConstraintAttribute.AttributeType.FLOAT_TYPE, 1f, false)
    endSet.getConstraint(R.id.iv_screen_v3).mCustomConstraints["RoundPercent"] = endRoundedAttr

    TransitionBuilder.buildTransition(
        motionScene,
        generateViewId(),
        generateViewId(),
        startSet,
        generateViewId(),
        endSet,
    ).apply {
        val frames = KeyFrames()
        val midFrame = KeyAttributes()
        midFrame.setViewId(R.id.iv_screen_v3)
        midFrame.framePosition = 90
        midFrame.setValue(KeyAttributes.ALPHA, 1)
        frames.addKey(midFrame)
        addKeyFrame(frames)
        duration = 8000
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接