我使用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>