动画布局的setRotation属性

问题描述

可以使用android中的RotateAnimation对象旋转

视图,但是使用此类似乎只能在视觉上旋转视图,例如单击旋转视图(用除中心以外的枢轴旋转的旋转视图)实际上不会单击该视图,该视图仍处于其原始位置,但仅在其他地方视觉上

因此,为了将视图及其子视图旋转到新位置,我找到了使用layout.setRotation(angle)解决方案,在我的情况下layoutconstraintLayout则必须旋转180

但是,问题是,我无法对此进行动画处理,我想从0180平滑过渡,但只能捕捉到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(),这就是为什么它不起作用的原因。