Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自动转换在运动布局中不起作用_Android_Android Motionlayout - Fatal编程技术网

Android 自动转换在运动布局中不起作用

Android 自动转换在运动布局中不起作用,android,android-motionlayout,Android,Android Motionlayout,我目前正在学习运动布局。我创建了一个小应用程序,其中中间的图像旋转5秒钟。onClick和onswip属性在设备上工作正常,但是当我在设备上运行应用程序时,autotransition=animateToEnd没有发生任何事情。基本上,我希望在活动开始时自动进行转换。 这是我的运动布局文件: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.motion.widget.MotionLayout x

我目前正在学习运动布局。我创建了一个小应用程序,其中中间的图像旋转5秒钟。onClick和onswip属性在设备上工作正常,但是当我在设备上运行应用程序时,autotransition=animateToEnd没有发生任何事情。基本上,我希望在活动开始时自动进行转换。 这是我的运动布局文件:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene">

    <ImageView
        android:id="@+id/back"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="TODO"
        android:src="@drawable/background" />

    <ImageView
        android:id="@+id/moon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_moon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.motion.widget.MotionLayout>

以下是我的场景:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="5000"
        motion:autoTransition="animateToEnd">
       <KeyFrameSet>
           <KeyCycle
               motion:motionTarget="@+id/moon"
               motion:framePosition="0"
               motion:wavePeriod="2"
               motion:waveShape="sawtooth"
               android:rotation="180"
               motion:waveOffset="0" />
       </KeyFrameSet>
    </Transition>

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

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

如果有任何其他方式运行运动布局过渡,请告诉我。
**用于运行我用来创建调试apk的应用程序,然后在我的设备上运行它。

如果不在约束集中设置像imageview这样的对象,自动转换将不起作用。添加任何要转换的小部件在约束集中使用约束标记,它将起作用 在开始约束集上,将图像放在第一个位置
结束时约束设置在5秒后放置图像视图的位置在您的情况下使用android:rotation=“”

不要将KeyCycle置于运动:framePosition=“0”这是一个已知问题

KeyCycle确实需要2或3个值才能表示完整 通常位置0和位置100,中间位置。 如果只放一个,它将复制开始(0)和结束(100)


在这种情况下,KeyCycles的值是可以随时间改变旋转速度。这样你就可以提高速度,或者快速启动,然后随着时间的推移减速

您是否使用了constraintLayout:2.0.0-beta7?我认为那个版本有一个bug。降级到2.0.0-beta6应该可以解决问题。

只需添加

app:motionInterpolator="linear"

对于转换标记来说,这是因为它的版本。使用androidx.constraintlayout.constraintlayout-2.0.1。

注意:当constraintlayout的版本为2.0.0-beta6及更高版本时,我发现MotionLayout中存在一些差异。存在这样的条件:this.mCurrentTransition==MotionScene中的transition#autoTransition(…)。我怀疑该情况会使操作重新开始

有两种解决办法。 1.降级至2.0.0-beta6或更低。 2.以编程方式控制它:

motionLayout.setTransitionListener(object : TransitionAdapter() {
    override fun onTransitionCompleted(motionLayout: MotionLayout, currentId: Int) {
        super.onTransitionCompleted(motionLayout, currentId)
        if (currentId == R.id.end) {
            motionLayout.transitionToStart()
        }
    }
})

我已经这么做了,但还是不行。同样在这里,这个人没有提到约束标签中的任何小部件,但动画仍然可以很好地进行自动转换。此外,我还尝试设置transitionListener并用transition change烤消息。我发现transition开始了,但移动屏幕上没有任何移动。@Ishantivei在sens xml文件上使用EasyOut方法,如果不起作用,请尝试使用onclick或onswipe进行测试,以查看其工作情况!不要使用2.0.0-beta7。使用2.0.0-beta4。它很好用,对我很有效。但是你能解释一下为什么它能工作吗?最好的方法是谷歌开发者解决它,并支持
自动翻译
对我来说只有这个解决方案有效,但我改变了TransitionStart到TransitionEnd。。。但仍然不知道为什么这是有效的,但自动转换没有。。。