【发布时间】: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>
【问题讨论】: