【发布时间】:2019-01-12 17:40:16
【问题描述】:
我知道以前有人问过这个问题,但所有答案都使用了 java,我对此完全没有经验。此外,android studio 对 kotlin 的自动翻译对这些答案中的代码不起作用。
我正在创建一个问答游戏,我想为其制作动画。我有一个视图(一个小脉冲点),我想移动到“正确”按钮的中心,并在点击它时直接加速并离开屏幕。现在最后一部分没问题,但我想不出移动按钮上的点的好方法。如果我像下面的例子那样做,它不会适应不同的屏幕尺寸甚至屏幕方向。那么有没有办法将坐标设置为屏幕的百分比/分数?或者获取按钮中心的坐标并移动到那里?
这就是我目前使用的:
var soultoX = ObjectAnimator.ofFloat(mContentView, "x", 150f).apply {
duration = 1000
}
var soultoY = ObjectAnimator.ofFloat(mContentView, "y", 1055f).apply {
duration = 1000
}
fun soulToButton() = AnimatorSet().apply {
play(soultoX).with(soultoY)
start()
}
一个示例按钮的xml:
<Button
android:text="@string/fa"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/fabutton"
style="@style/Widget.AppCompat.Button.Colored"
app:layout_constraintTop_toTopOf="@+id/guideline8"
app:layout_constraintBottom_toBottomOf="@+id/guideline7"
android:textColorLink="@color/colorAccent"
android:textColor="@color/colorPrimary"
app:layout_constraintVertical_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintEnd_toStartOf="@+id/guideline4"/>
还有观点:
<Button
android:text="@string/fa"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/fabutton"
style="@style/Widget.AppCompat.Button.Colored"
app:layout_constraintTop_toTopOf="@+id/guideline8"
app:layout_constraintBottom_toBottomOf="@+id/guideline7"
android:textColorLink="@color/colorAccent"
android:textColor="@color/colorPrimary"
app:layout_constraintVertical_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintEnd_toStartOf="@+id/guideline4"/>
【问题讨论】:
标签: android kotlin android-animation screen-size objectanimator