【问题标题】:How to use animatorSet to change background color of a View如何使用 animatorSet 更改视图的背景颜色
【发布时间】:2019-12-15 13:14:14
【问题描述】:

我的目标是同时为不同的事物制作动画。 我已成功使用 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 同时更改根视图背景颜色?

【问题讨论】:

    标签: android android-animation animator


    【解决方案1】:

    假设这是您的布局文件。

    <?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()
    

    【讨论】:

    • 感谢您的回答。执行此操作后如何将视图重置为默认值?
    【解决方案2】:

    创建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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 2020-05-15
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多