【问题标题】:Move View to the center of another view, or alternatively to coordinates relative to screen size将视图移动到另一个视图的中心,或者移动到相对于屏幕大小的坐标
【发布时间】: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


    【解决方案1】:

    【讨论】:

    • 请在您的答案中插入一些代码示例,并添加外部链接以获取更多详细信息(如果需要)
    • 我有ConstraintLayout 1.1.3,它还不支持MotionLayout,我想你想指点我吗? MotionLayout 将在 alpha 版本的 2.0 中添加。我不想对此感到困惑,因为我没有这方面的经验,所以我不会注意到我做错了什么或者只是有一个错误。还是你的意思是别的?如果有,请举个例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多