【问题标题】:Rotation animation for the background gradient of the view视图背景渐变的旋转动画
【发布时间】:2025-11-15 19:10:02
【问题描述】:

我想在我的应用程序中添加一个动画,我希望我的屏幕背景渐变可以旋转。我尝试了一些东西,比如 ValueAnimator、PropertyAnimator 等,但我遇到了一个问题。

当我运行动画时,整个视图开始旋转。看起来屏幕本身正在旋转。相反,我想要一个只有背景渐变而不是整个屏幕旋转的效果。

获得这种效果的最佳方法是什么?

这是我目前所拥有的

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val view: ConstraintLayout = findViewById(R.id.animation_view)
        startAnimation(view)
    }

    private fun startAnimation(view: View) {
        view.startAnimation(
            AnimationUtils.loadAnimation(
                this, R.anim.rotation_clockwise
            )
        )
    }
}

动画资源xml

<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="1200"/>

布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPurple_A400"
    tools:context=".MainActivity">

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/animation_view"
        android:background="@drawable/drawable_purple_gradient">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android animation kotlin


    【解决方案1】:

    您是否为activityfragment 的根视图设置动画? 如果是,那是有道理的。整个屏幕将被旋转。

    如果你想为视图的背景设置动画,你应该添加一个只包含背景的新视图,然后旋转它。

    <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/colorPurple_A400"
      tools:context=".MainActivity">
    
      <View
        android:id="@+id/animation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/drawable_purple_gradient"/>
    
      <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"/>
    
      </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 我试过了,结果还是一样。我已经在问题中添加了我的布局文件。
    • @Ezio 我更新了我的答案并使用了你的布局文件
    • 我尝试了您的解决方案,但仍然得到相同的结果。
    • 如果您正在为 id 为 animation_view 的视图制作动画,那么所有视图如何制作动画?这没有意义