动画布局的 setRotation 属性
animating the layout's setRotation property
可以使用 android 中的 RotateAnimation
对象旋转视图,但使用此 class 似乎只能在视觉上旋转视图,例如单击旋转视图(使用除中心以外的枢轴旋转)实际上不会单击该视图,该视图仍处于其原始位置,但仅在其他地方视觉上。
因此,为了将视图及其子视图一起旋转到新位置,我找到了使用 layout.setRotation(angle)
的解决方案,在我的例子中 layout
是一个 constraintLayout
我将不得不轮换 180
.
但是,问题是,我无法对此进行动画处理,我想从 0
度平滑过渡到 180
度,但它只是捕捉到 180度。如何为这个 属性 或可能的旋转设置动画?
在约束布局内的 .xml 文件中添加以下代码。
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"/>
MainActivity.java :
ConstraintLayout constraintLayout = findViewById(R.id.layout);
constraintLayout.animate().rotation(100).setDuration(2000);
唯一的问题是您忘记在 .rotation() 之前添加 .animate(),这就是它不起作用的原因。
可以使用 android 中的 RotateAnimation
对象旋转视图,但使用此 class 似乎只能在视觉上旋转视图,例如单击旋转视图(使用除中心以外的枢轴旋转)实际上不会单击该视图,该视图仍处于其原始位置,但仅在其他地方视觉上。
因此,为了将视图及其子视图一起旋转到新位置,我找到了使用 layout.setRotation(angle)
的解决方案,在我的例子中 layout
是一个 constraintLayout
我将不得不轮换 180
.
但是,问题是,我无法对此进行动画处理,我想从 0
度平滑过渡到 180
度,但它只是捕捉到 180度。如何为这个 属性 或可能的旋转设置动画?
在约束布局内的 .xml 文件中添加以下代码。
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"/>
MainActivity.java :
ConstraintLayout constraintLayout = findViewById(R.id.layout);
constraintLayout.animate().rotation(100).setDuration(2000);
唯一的问题是您忘记在 .rotation() 之前添加 .animate(),这就是它不起作用的原因。