【问题标题】:animating the layout's setRotation property动画布局的 setRotation 属性
【发布时间】:2020-08-29 14:59:34
【问题描述】:

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

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

但是,问题是,我无法对此进行动画处理,我想从0180 度的平滑过渡,但它只是捕捉到 180 度。如何为该属性或旋转设置动画?

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    在约束布局内的 .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() ,这就是它不起作用的原因。

    【讨论】:

    • 我需要在运行时为布局设置动画。这不会在通货膨胀期间对旋转进行硬编码吗?
    • 我已经编辑了我的答案并添加了 java 代码并删除了 .xml 文件中的旋转。希望它有效。
    • 唯一的问题是您忘记在 .rotation() 之前添加 .animate() ,这就是它不起作用的原因。
    • 这很好用!但是,我如何设置旋转的枢轴。在RotateAnimation 中我们可以使用pivotX、pivotY 和Animate.RELATIVE_TO_SELF 来设置它,有没有类似的东西?
    • 可以添加一个onImageClicked()函数,在我们点击屏幕时启动代码is.ConstraintLayout constraintLayout; constraintLayout.animate().animate().translationYBy(180).translationXBy(180).setDuration(2000);然后在 onCreate 函数中添加:constraintLayout.setX(-1000); constraintLayout.animate().translationXBy(1000).rotation(3600).setDuration(2000); 这应该可以工作,我希望它有所帮助。
    猜你喜欢
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多