如何使用 animatorSet 改变 View 的背景颜色
How to use animatorSet to change background color of a View
我的目标是同时为不同的事物制作动画。
我已经成功地使用 AnimatorSet 来放大我的 textView
val textViewAnimatorX = ObjectAnimator.ofFloat(txtView, View.SCALE_X, 0.8f, 1.2f)
val textViewAnimatorY = ObjectAnimator.ofFloat(txtView, View.SCALE_Y, 0.8f, 1.2f)
val animatorSet = AnimatorSet()
animatorSet.playTogether(textViewAnimatorX, textViewAnimatorY)
animatorSet.setDuration(500)
animatorSet.start()
如何使用 animator set 同时更改根视图背景颜色?
假定这是您的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is text view" />
</LinearLayout>
这是同时更改根视图背景颜色的代码。
val textViewAnimatorX = ObjectAnimator.ofFloat(txtView, View.SCALE_X, 0.8f, 1.2f)
val textViewAnimatorY = ObjectAnimator.ofFloat(txtView, View.SCALE_Y, 0.8f, 1.2f)
// Create a new ObjectAnimator instance to change background color of root view.
val currentColor = rootView.solidColor
val newColor = Color.GREEN
val rootViewBackground = ObjectAnimator.ofObject(
rootView,
"backgroundColor",
ArgbEvaluator(),
currentColor,
newColor
)
val animatorSet = AnimatorSet()
// Simultaneously change the root view background color.
animatorSet.playTogether(textViewAnimatorX, textViewAnimatorY, rootViewBackground)
animatorSet.setDuration(500)
animatorSet.start()
创建 ObjectAnimator
实例来改变颜色
val backgroundColorAnimator = ObjectAnimator.ofObject(view,"backgroundColor",
ArgbEvaluator(),
0xFFFFFFFF,//color you like
0xff78c5f9)
val animatorSet = AnimatorSet()
animatorSet.playTogether(textViewAnimatorX, textViewAnimatorY,backgroundColorAnimator)// add your backgroundColorAnimator here
animatorSet.setDuration(500)
animatorSet.start()
我的目标是同时为不同的事物制作动画。 我已经成功地使用 AnimatorSet 来放大我的 textView
val textViewAnimatorX = ObjectAnimator.ofFloat(txtView, View.SCALE_X, 0.8f, 1.2f)
val textViewAnimatorY = ObjectAnimator.ofFloat(txtView, View.SCALE_Y, 0.8f, 1.2f)
val animatorSet = AnimatorSet()
animatorSet.playTogether(textViewAnimatorX, textViewAnimatorY)
animatorSet.setDuration(500)
animatorSet.start()
如何使用 animator set 同时更改根视图背景颜色?
假定这是您的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is text view" />
</LinearLayout>
这是同时更改根视图背景颜色的代码。
val textViewAnimatorX = ObjectAnimator.ofFloat(txtView, View.SCALE_X, 0.8f, 1.2f)
val textViewAnimatorY = ObjectAnimator.ofFloat(txtView, View.SCALE_Y, 0.8f, 1.2f)
// Create a new ObjectAnimator instance to change background color of root view.
val currentColor = rootView.solidColor
val newColor = Color.GREEN
val rootViewBackground = ObjectAnimator.ofObject(
rootView,
"backgroundColor",
ArgbEvaluator(),
currentColor,
newColor
)
val animatorSet = AnimatorSet()
// Simultaneously change the root view background color.
animatorSet.playTogether(textViewAnimatorX, textViewAnimatorY, rootViewBackground)
animatorSet.setDuration(500)
animatorSet.start()
创建 ObjectAnimator
实例来改变颜色
val backgroundColorAnimator = ObjectAnimator.ofObject(view,"backgroundColor",
ArgbEvaluator(),
0xFFFFFFFF,//color you like
0xff78c5f9)
val animatorSet = AnimatorSet()
animatorSet.playTogether(textViewAnimatorX, textViewAnimatorY,backgroundColorAnimator)// add your backgroundColorAnimator here
animatorSet.setDuration(500)
animatorSet.start()