【问题标题】:Android MotionLayout doesn't show animationAndroid MotionLayout 不显示动画
【发布时间】:2021-03-23 17:50:18
【问题描述】:

我正在使用 motionLayout 在 splashScreen 中创建动画。但是我的动画不起作用,而是立即在屏幕上显示元素的结束位置。这是我的代码:

SplashActivity:

class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_splash)

    motionLayout.transitionToEnd()

    val handler = Handler()
    handler.postDelayed( {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }, 3000)
}
}

activity_splash 布局:

    <?xml version="1.0" encoding="utf-8"?>
   <androidx.constraintlayout.motion.widget.MotionLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:background="@color/colorPrimary"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/motionLayout"
   app:layoutDescription="@xml/scene_splash"
   tools:context=".ui.main.MainActivity">

<TextView
    android:id="@+id/letter1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="S"
    android:textColor="#000"
    android:textSize="40sp" />


<TextView
    android:id="@+id/letter2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="T"
    android:textColor="#000"
    android:textSize="40sp" />

<TextView
    android:id="@+id/letter3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="O"
    android:textColor="#000"
    android:textSize="40sp"
    android:fontFamily="sans-serif-black" />

<TextView
    android:id="@+id/letter4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="R"
    android:textColor="#000"
    android:textSize="40sp" />

<TextView
    android:id="@+id/letter5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="I"
    android:textColor="#000"
    android:textSize="40sp" />

<TextView
    android:id="@+id/letter6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="E"
    android:textColor="#000"
    android:textSize="40sp" />

<TextView
    android:id="@+id/letter7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="S"
    android:textColor="#000"
    android:textSize="40sp"
    android:fontFamily="sans-serif-black" />

<TextView
    android:id="@+id/word"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="by UNRD"
    android:textColor="#000"
    android:textSize="40sp"
    android:fontFamily="sans-serif-black"/>

</androidx.constraintlayout.motion.widget.MotionLayout>

最后是我的 scene_splash 文件:

<?xml version="1.0" encoding="utf-8"?>

<Transition
    motion:constraintSetStart="@+id/start"
    motion:constraintSetEnd="@+id/end"
    motion:duration="2500">

    <KeyFrameSet>
        <KeyPosition
            motion:keyPositionType="parentRelative"
            motion:percentY="0.25"
            motion:framePosition="50"
            motion:motionTarget="@id/letter2"/>

        <KeyPosition
            motion:keyPositionType="parentRelative"
            motion:percentY="0.40"
            motion:percentX="0.1"
            motion:framePosition="50"
            motion:motionTarget="@id/letter3"/>

        <KeyPosition
            motion:keyPositionType="parentRelative"
            motion:percentY="0.70"
            motion:framePosition="50"
            motion:motionTarget="@id/letter6"/>
    </KeyFrameSet>
</Transition>

<ConstraintSet android:id="@+id/start">
    <Constraint
        android:id="@+id/letter1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"
        />

    <Constraint
        android:id="@+id/letter3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintVertical_bias="0.8"
        motion:layout_constraintBottom_toBottomOf="parent"/>

    <Constraint
        android:id="@+id/letter4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.15"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="20dp"
        motion:layout_constraintBottom_toBottomOf="parent"/>

    <Constraint
        android:id="@+id/letter6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"/>

    <Constraint
        android:id="@+id/letter7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="30dp"
        motion:layout_constraintBottom_toBottomOf="parent"/>

    <Constraint
        android:id="@+id/word"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="0"
        android:layout_marginTop="100dp"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintTop_toBottomOf="parent"
        android:fontFamily="sans-serif-black"/>

</ConstraintSet>

<ConstraintSet android:id="@+id/end">

    <Constraint
        android:id="@+id/letter1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintRight_toLeftOf="@id/letter2"
        motion:layout_constraintTop_toTopOf="parent"/>


    <Constraint
        android:id="@+id/letter2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintRight_toLeftOf="@id/letter3"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintRight_toLeftOf="@id/letter4"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintLeft_toRightOf="@id/letter4"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintLeft_toRightOf="@id/letter5"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintLeft_toRightOf="@id/letter6"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/word"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotation="20"
        android:layout_marginTop="20dp"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintTop_toBottomOf="@id/letter4"/>
</ConstraintSet>

有人知道为什么会这样吗?

【问题讨论】:

    标签: android kotlin android-motionlayout


    【解决方案1】:

    您可以简单地添加 在

    motion:autoTransition="animateToEnd"
    

    那么就不需要调用transitionToEnd了。

    或者你可以调用transtionToEnd

         onAttachedToWindow() {
            super.onAttachedToWindow()
            motionlayout.transitionToEnd()
    

    【讨论】:

    • 这些都无济于事。我的动画仍然没有发生...
    • 对不起。 motion:autoTransition="animateToEnd" 解决了这个问题。非常感谢,我会赞成并接受您的回答。如果您知道为什么我的方法不起作用,但是通过添加 xml 的这种方式它起作用,请解释我,因为我没有任何合乎逻辑的解释。
    • 场景仅在 onAttachedToWindow 中设置,因此 transitionToEnd 在创建时没有意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2020-01-07
    相关资源
    最近更新 更多