我有一个使用MotionLayout进行动画处理的ImageFilterView视图。它以一个小正方形视图开始,并将roundPercent设置为1.0(因此它也是一个圆形),该视图隐藏在一个圆形后面,然后动画显示为全屏正方形。第一次制作动画时效果很好,但每隔一次都保持为矩形。
您可以在此处查看有关该问题的视频:VIDEO
我不确定这是不是ImageFilterView或MotionLayout上的错误,或者我打错了什么。值得一提的是,我不是在Transition中使用onClick,而是在Activity中以编程方式调用它,例如]
imageBorder.setOnClickListener {
if (motionContainer.progress > 0.75)
motionContainer.transitionToStart()
else
motionContainer.transitionToEnd()
}
我的motionScene代码如下所示
<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="1000"
motion:motionInterpolator="easeInOut" />
<ConstraintSet android:id="@+id/start">
<Constraint android:id="@id/translucentOverlay">
<Layout
android:layout_width="5dp"
android:layout_height="5dp"
motion:layout_constraintBottom_toBottomOf="@id/imageBorder"
motion:layout_constraintEnd_toEndOf="@id/imageBorder"
motion:layout_constraintStart_toStartOf="@id/imageBorder"
motion:layout_constraintTop_toTopOf="@id/imageBorder" />
<CustomAttribute
motion:attributeName="roundPercent"
motion:customFloatValue="1.0" />
<Motion motion:motionStagger="2" />
</Constraint>
<Constraint android:id="@id/imageBorder">
<Layout
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
<CustomAttribute
motion:attributeName="crossfade"
motion:customFloatValue="0" />
<Motion motion:motionStagger="2" />
</Constraint>
<Constraint android:id="@id/imageBackground">
<Layout
android:layout_width="32dp"
android:layout_height="32dp"
motion:layout_constraintBottom_toBottomOf="@id/imageBorder"
motion:layout_constraintEnd_toEndOf="@id/imageBorder"
motion:layout_constraintStart_toStartOf="@id/imageBorder"
motion:layout_constraintTop_toTopOf="@id/imageBorder" />
<Motion motion:motionStagger="2" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint android:id="@id/translucentOverlay">
<Layout
android:layout_width="match_parent"
android:layout_height="match_parent" />
<CustomAttribute
motion:attributeName="roundPercent"
motion:customFloatValue="0.0" />
<Motion motion:motionStagger="2" />
</Constraint>
<Constraint android:id="@id/imageBorder">
<Layout
android:layout_width="88dp"
android:layout_height="88dp"
motion:layout_constraintBottom_toBottomOf="@id/imageBackground"
motion:layout_constraintEnd_toEndOf="@id/imageBackground"
motion:layout_constraintStart_toStartOf="@id/imageBackground"
motion:layout_constraintTop_toTopOf="@id/imageBackground" />
<CustomAttribute
motion:attributeName="crossfade"
motion:customFloatValue="1" />
<Motion motion:motionStagger="2" />
</Constraint>
<Constraint android:id="@id/imageBackground">
<Layout
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="64dp"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
<Motion motion:motionStagger="2" />
</Constraint>
</ConstraintSet>
而且我的MotionLayout看起来像这样:
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/motionContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
app:layoutDescription="@xml/motion_scene">
<androidx.constraintlayout.utils.widget.ImageFilterView
android:id="@+id/translucentOverlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/translucent_background_square"/>
<androidx.constraintlayout.utils.widget.ImageFilterView
android:id="@+id/imageBorder"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="8dp"
android:padding="4dp"
android:src="@drawable/circle_start"
app:altSrc="@drawable/circle_end" />
<ImageView
android:id="@+id/imageBackground"
android:layout_width="32dp"
android:layout_height="32dp"
android:importantForAccessibility="no"
android:tint="@color/primaryDarkColor"
app:srcCompat="@drawable/circle_opaque" />
</androidx.constraintlayout.motion.widget.MotionLayout>
很烦人,它在第一次之后就停止工作了。很长时间以来,我一直在尝试解决这个圆平方问题,我以为我终于找到了解决方法,但似乎没有。
经过更多摆弄之后,这似乎是ImageFilterView中的错误,如果将roundPercent设置为0.0,则在其余时间视图将保持矩形。我想这是除以0或类似错误的问题。我已将此问题报告给Google Developers,希望很快会得到解决。
同时,您可以通过将percentRound设置为0.000001来解决被零除的错误,从而解决该问题。