Sta*_*ool 11 animation android android-constraintlayout androidx android-motionlayout
我有一个 MotionScene,其中有 4 个 ConstraintSet,代表屏幕的 4 个状态(加载状态),并且它们之间有 3 个转换。当我的应用程序状态从例如加载更改为处理时,我想运行转换 1 (set1 -> set2),当状态再次更改时,我想运行转换 2 (set2 -> set3)。我找不到办法做到这一点。
我接下来尝试:
设置当前转换
motion_layout.setTransition(R.id.set1, R.id.set2)
motion_layout.transitionToState(R.id.set2)
Run Code Online (Sandbox Code Playgroud)
只设置过渡
motion_layout.setTransition(R.id.set1)
Run Code Online (Sandbox Code Playgroud)
转换到某种状态:
motion_layout.transitionToState(R.id.set1)
Run Code Online (Sandbox Code Playgroud)
但是上述所有方法都运行我的所有集合,即使我使用 app:autoTransition="none" 。
我尝试将所有内容都放在一个 Transition 中并设置 app:progress = 0 on ,然后通过进度控制动画状态:
motion_layout.setProgress(0.25f, 1.0f)
Run Code Online (Sandbox Code Playgroud)
它只是将所有动画运行到最后,或者我尝试过
motion_layout.progress = 0.25f
Run Code Online (Sandbox Code Playgroud)
它没有动画,它只向我显示 0.25 动画进度,没有任何动作。
如何控制动画的流向?如何运行特定的集合?使用进步会更好吗?如何解决?
PS我用
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta7'
Run Code Online (Sandbox Code Playgroud)
我不确定你在做什么(没有足够的代码),但这里有一个尽可能简单的 5 状态示例。
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetStart="@id/state1"
motion:constraintSetEnd="@+id/state2"/>
<Transition
motion:constraintSetStart="@id/state2"
motion:constraintSetEnd="@+id/state3"/>
<Transition
motion:constraintSetStart="@id/state3"
motion:constraintSetEnd="@+id/state4"/>
<Transition
motion:constraintSetStart="@id/state4"
motion:constraintSetEnd="@+id/state5"/>
<Transition
motion:constraintSetStart="@id/state1"
motion:constraintSetEnd="@+id/state5"/>
<ConstraintSet android:id="@+id/state1">
<Constraint android:id="@+id/view">
<CustomAttribute motion:attributeName="text" motion:customStringValue="state1" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/state2">
<Constraint android:id="@+id/view">
<Transform android:translationX="-100dp"/>
<CustomAttribute motion:attributeName="text" motion:customStringValue="state2" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/state3">
<Constraint android:id="@+id/view">
<Transform android:translationX="100dp"/>
<CustomAttribute motion:attributeName="text" motion:customStringValue="state3" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/state4">
<Constraint android:id="@+id/view">
<Transform android:translationY="-100dp"/>
<CustomAttribute motion:attributeName="text" motion:customStringValue="state4" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/state5">
<Constraint android:id="@+id/view">
<Transform android:translationY="100dp"/>
<CustomAttribute motion:attributeName="text" motion:customStringValue="state5" />
</Constraint>
</ConstraintSet>
Run Code Online (Sandbox Code Playgroud)
这个场景文件适用于这个 main_activity.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene"
android:id="@+id/motionlayout"
tools:context=".MainActivity">
<TextView
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4DD3D3"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="transition"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:onClick="nextState"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
Run Code Online (Sandbox Code Playgroud)
和这个 MainActivity.kt
package com.example.multistate
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun nextState(view: View) {
when (motionlayout.currentState ) {
R.id.state1 -> motionlayout.transitionToState(R.id.state2)
R.id.state2 -> motionlayout.transitionToState(R.id.state3)
R.id.state3 -> motionlayout.transitionToState(R.id.state4)
R.id.state4 -> motionlayout.transitionToState(R.id.state5)
R.id.state5 -> motionlayout.transitionToState(R.id.state1)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4817 次 |
| 最近记录: |