android - 视图背景渐变的旋转动画
问题描述
我想在我的应用程序中添加一个动画,我希望我的屏幕的背景渐变可以旋转。我尝试了一些东西,比如 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>
解决方案
activity
您是否为or的根视图设置动画fragment
?如果是,那是有道理的。整个屏幕将被旋转。
如果你想为视图的背景设置动画,你应该添加一个只包含背景的新视图,然后旋转它。
<?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>
推荐阅读
- c++ - Compile Virtual Keyboard for Qt WebAssembly
- azure - azureupload 对于大文件在一个小时左右后失败,错误代码为 403
- laravel - 如何调用 Laravel 8.x 模型
- python - 如何有效地合并字典中的所有列表?
- google-chrome - 如何在基本 HTTP 身份验证对话框中在地址栏中显示锁定图标?
- powerbi - DAX Measure 计算平均值,其中包含参数
- laravel - 如何从 Laravel 的数组中插入缺失的月份?
- python - AlreadyFinalized("上下文已经完成。")
- c++ - 为什么 rand() 给我负数?
- python - Python 中特殊字符和 os.path.getsize() 的问题