动画期间似乎失去了一些限制

发布于 2025-01-31 04:02:56 字数 4448 浏览 5 评论 0原文

我有一个问题: 我有一个文本视图,其内容正在通过Livedata不断变化。当动画不执行时,这看起来还不错,但是当MotionLayout开始执行时,我的文本会被阻止一点。 并且我的文本视图和按钮的约束被包装。

活动:

class ErrorActivity : AppCompatActivity() {

    private val mViewModel by viewModels<ErrorViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        DataBindingUtil.setContentView<ActivityErrorBinding>(this, R.layout.activity_error).apply {
            lifecycleOwner = this@ErrorActivity
            viewModel = mViewModel
        }
    }

    fun startStopAnim(v: View) {
        mViewModel.anim()
    }
}

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:binding="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="viewModel"
            type="com.test.test.ErrorViewModel" />

    </data>

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/scene"
        binding:anim="@{viewModel.mAnim}">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{@string/test(viewModel.mCountDown)}"
            android:textSize="32sp"
            app:layout_constraintEnd_toStartOf="@+id/btn"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@string/test" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tv"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/anim"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startStopAnim"
            android:text="startOrStop"
            android:textAllCaps="false"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

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

ViewModel:

class ErrorViewModel : ViewModel() {

    private val _countDown: MutableLiveData<String> = MutableLiveData()
    val mCountDown: LiveData<String> = _countDown

    private val _anim: MutableLiveData<Boolean> = MutableLiveData()
    val mAnim: LiveData<Boolean> = _anim

    private var count = 0
    private var state = false

    init {
        viewModelScope.launch(Dispatchers.IO) {
            while (true) {
                delay(1000)
                _countDown.postValue(count++.toString())
            }
        }
    }

    fun anim() {
        state = !state
        _anim.value = state
    }
}

@BindingAdapter("anim")
fun anim(layout: MotionLayout, play: Boolean?) {
    play?.let {
        if (it) {
            layout.transitionToEnd()
        } else {
            layout.transitionToStart()
        }
    }
}

Motionscene:

为简单起见,场景只有一个持续时间

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:app="http://schemas.android.com/apk/res-auto">
    <Transition
        app:constraintSetEnd="@+id/end"
        app:constraintSetStart="@+id/start"
        app:duration="2000" />
</MotionScene>

gif 正如您在GIF中看到的那样,当没有单击时,所有数字都可以正常工作,但是当我单击按钮时,开始了2秒的动画,在此期间,我的文本不正确显示。 当然,这只是一个演示例子。在真实的场景中,不仅没有完全显示文本,而且即使是文本视图和imageView放错了位置,并且一旦执行动画,就无法恢复。 有人可以帮我吗

?只要执行此动画一次,文本视图A的约束关系肯定会出现问题并且不能恢复(当前找到Onpause后可以恢复resume)

I have a problem:
I have a TextView whose content is constantly changing via LiveData. This looks all right when the animation isn't executing, but when the MotionLayout starts executing, my text gets blocked a little bit.
And the constraints of my TextView and Button are packed.

activity:

class ErrorActivity : AppCompatActivity() {

    private val mViewModel by viewModels<ErrorViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        DataBindingUtil.setContentView<ActivityErrorBinding>(this, R.layout.activity_error).apply {
            lifecycleOwner = this@ErrorActivity
            viewModel = mViewModel
        }
    }

    fun startStopAnim(v: View) {
        mViewModel.anim()
    }
}

activity layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:binding="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="viewModel"
            type="com.test.test.ErrorViewModel" />

    </data>

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/scene"
        binding:anim="@{viewModel.mAnim}">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{@string/test(viewModel.mCountDown)}"
            android:textSize="32sp"
            app:layout_constraintEnd_toStartOf="@+id/btn"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@string/test" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tv"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/anim"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startStopAnim"
            android:text="startOrStop"
            android:textAllCaps="false"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

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

viewModel:

class ErrorViewModel : ViewModel() {

    private val _countDown: MutableLiveData<String> = MutableLiveData()
    val mCountDown: LiveData<String> = _countDown

    private val _anim: MutableLiveData<Boolean> = MutableLiveData()
    val mAnim: LiveData<Boolean> = _anim

    private var count = 0
    private var state = false

    init {
        viewModelScope.launch(Dispatchers.IO) {
            while (true) {
                delay(1000)
                _countDown.postValue(count++.toString())
            }
        }
    }

    fun anim() {
        state = !state
        _anim.value = state
    }
}

@BindingAdapter("anim")
fun anim(layout: MotionLayout, play: Boolean?) {
    play?.let {
        if (it) {
            layout.transitionToEnd()
        } else {
            layout.transitionToStart()
        }
    }
}

motionScene:

For simplicity, Scene has only one duration

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:app="http://schemas.android.com/apk/res-auto">
    <Transition
        app:constraintSetEnd="@+id/end"
        app:constraintSetStart="@+id/start"
        app:duration="2000" />
</MotionScene>

gif
As you can see in the gif, when there is no click, everything works fine for the numbers, but when I click the button, a 2 second animation starts, during which time my text doesn't display properly.
Of course, this is just a demo example. In a real scene, not only the text is not displayed completely, but even the TextView And ImageView are misplaced, and once the animation is executed, it cannot be recovered.
Can someone help me...

Actually in a real scene, the parent layout (B) of the problematic TextView (A) would perform a displacement animation. As long as this animation is executed once, the constraint relationship of TextView A will definitely have problems and cannot be restored (onResume can be restored after onPause is currently found)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱人如己 2025-02-07 04:02:56

在动画中,Motionlayout期间的设计不符合请求layout。
因为在大多数应用中,不需要,并且会对性能产生重大影响。

在过渡中启用它

<Transition  ...   motion:layoutDuringTransition="honorRequest" \>

By design during animation MotionLayout does not honor requestLayout.
Because in the majority of applications it is not needed and would have a significant impact on performance.

To enable it in the transition add

<Transition  ...   motion:layoutDuringTransition="honorRequest" \>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文