0

我有一个函数可以在我的背景上创建一个渐变MainActivity。渐变以编程方式创建为GradientDrawable. App 中的某些交互会改变渐变的颜色。

渐变和颜色已正确显示,并且它们确实会相应更改,但是,我希望对颜色更改进行过渡效果。现在,颜色立即过渡,没有交叉淡入淡出。

fun createGradient() {
    gd.shape = GradientDrawable.RECTANGLE
    mainActivityID.background = gd
    gd.gradientType = GradientDrawable.LINEAR_GRADIENT
    gd.orientation = (GradientDrawable.Orientation.BL_TR)
}

根据App中的交互,我用一个when语句来改变颜色GradientDrawable

fun changeGradient(){

    //val td = gd as TransitionDrawable
    //td.isCrossFadeEnabled = true
    //td.startTransition(1500)

    when(calories){
        in 0..50 -> gd.colors = intArrayOf(
            ContextCompat.getColor(this, R.color.dBlue),
            ContextCompat.getColor(this, R.color.lBlue)
        )
        in 51..100 -> gd.colors = intArrayOf(
            ContextCompat.getColor(this, R.color.dRed),
            ContextCompat.getColor(this, R.color.lRed)
        )
        // etc...
    }
}

我尝试将 GradientDrawable 转换为 TransitionDrawable (如上面注释的代码中所示),但崩溃与:

java.lang.ClassCastException: android.graphics.drawable.GradientDrawable 不能转换为 android.graphics.drawable.TransitionDrawable

如何在 GradientDrawable 颜色变化之间添加 1500 毫秒的过渡?

进度崩溃java.lang.NegativeArraySizeException: -16777216

when(calories){
    in 0..50 -> animateGradient(gd, IntArray(Color.BLACK), IntArray(Color.CYAN))
    in 51..100 -> animateGradient(gd, IntArray(Color.RED), IntArray(Color.BLUE))
    in 101..150 -> animateGradient(gd, IntArray(Color.GREEN), IntArray(Color.YELLOW))
    // etc... about 15 more statements.
}
4

1 回答 1

1

我不认为渐变 drawable 内置了过渡方法,但是您可以运行自己的值动画器来执行颜色过渡。

var anim : Animator? = null

fun animateGradient(gd: GradientDrawable, from : IntArray, to: IntArray){
    require(from.size == to.size)
    anim?.cancel()
    val arraySize = from.size
    val props = Array<PropertyValuesHolder>(arraySize){
        PropertyValuesHolder.ofObject(it.toString(), ArgbEvaluator(), from[it], to[it])
    }
    val anim = ValueAnimator.ofPropertyValuesHolder(*props)
    anim.addUpdateListener {
        gd.colors = IntArray(arraySize){i ->
            it.getAnimatedValue(i.toString()) as Int
        }
    }
    anim.duration = 1500
    anim.start()
    this.anim = anim
}

编辑:修改方法,使其跟踪当前颜色本身,减少参数数量。

var anim : Animator? = null
// variable that tracks current color - must be initialized with default gradient colors
var currentGradient = intArrayOf(
        ContextCompat.getColor(this, R.color.dBlue),
        ContextCompat.getColor(this, R.color.lBlue)
    )

fun animateGradient(targetColors: IntArray){
    val from = currentGradient
    require(from.size == targetColors.size)
    anim?.cancel()
    val arraySize = from.size
    val props = Array<PropertyValuesHolder>(arraySize){
        PropertyValuesHolder.ofObject(it.toString(), ArgbEvaluator(), from[it], targetColors[it])
    }
    val anim = ValueAnimator.ofPropertyValuesHolder(*props)
    anim.addUpdateListener {valueAnim ->
        IntArray(arraySize){i ->
            valueAnim.getAnimatedValue(i.toString()) as Int
        }.let{
            currentGradient = it
            gd.colors = it
        }
    }
    anim.duration = 1500
    anim.start()
    this.anim = anim
}

调用站点:

when(calories){
    in 0..50 -> animateGradient(intArrayOf(
        ContextCompat.getColor(this, R.color.dBlue),
        ContextCompat.getColor(this, R.color.lBlue)
    ))
    in 51..100 -> animateGradient(intArrayOf(
        ContextCompat.getColor(this, R.color.dRed),
        ContextCompat.getColor(this, R.color.lRed)
    ))
    // etc...
}
于 2019-05-14T22:20:33.803 回答