如何在Kotlin中动态创建KeyFrameSet

rvpgvaaj  于 2023-05-18  发布在  Kotlin
关注(0)|答案(1)|浏览(193)

我使用XML为AndroidKotlinMotion创建了一个测试程序。我需要添加许多关键位置和关键属性。我需要以编程方式完成这项工作,而不是通过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>
p8h8hvxi

p8h8hvxi1#

是的,您可以通过编程方式创建关键帧。但请注意,请确保使用最新的constraintlayout版本(例如: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
    }
}

相关问题