我正在使用此库生成渐变: https://github.com/revely-inc/co.revely.gradient
用于设置渐变动画的kotlin代码如下:
val color1 = Color.parseColor("#00c6ff")
val color2 = Color.parseColor("#ff72ff")
val valueAnimator = ValueAnimator.ofFloat(0f, 360f)
valueAnimator.duration = 15000
valueAnimator.repeatCount = ValueAnimator.INFINITE
valueAnimator.interpolator = LinearInterpolator()
RevelyGradient.sweep()
.colors(intArrayOf(color1, color2, color1))
.animate(valueAnimator, { _valueAnimator, _gradientDrawable ->
_gradientDrawable.angle = _valueAnimator.animatedValue as Float
})
.onBackgroundOf(container)
valueAnimator.start()
到目前为止我所获得的java代码:
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.ofFloat(0f, 360f);
valueAnimator.setDuration(15000);
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
valueAnimator.setInterpolator(new LinearInterpolator());
RevelyGradient.sweep()
.colors(new int[] {Color.parseColor("#FF2525"), Color.parseColor("#6078EA")})
.animate(valueAnimator, { _valueAnimator, _gradientDrawable ->
_gradientDrawable.angle = _valueAnimator.animatedValue as Float
})
.onBackgroundOf(rootView);
valueAnimator.start();
正如你注意到的那样,我无法转换它(因为它对kotlin局外人没有意义):
{ _valueAnimator, _gradientDrawable ->
_gradientDrawable.angle = _valueAnimator.animatedValue as Float
}
这里应该做什么?
错误:
必需:'kotlin.jvm.functions.Function2?超 android.animation.ValueAnimator,?超 co.revely.gradient.drawables.Gradient,kotlin.Unit>
答案 0 :(得分:2)
这是一个匿名函数(" lambda表达式")。看起来RevelyGradient.animate()
有两个参数:
在您传入的特定功能中,Gradient的angle
属性是从ValueAnimator的animatedValue property设置的(之前会被转换为Float分配)。
那你怎么用Java重写呢?由于这是Android问题,因此您需要enable Java 8 language features来处理您正在处理的模块。然后你应该能够重写那行代码看起来像这样:
RevelyGradient
.animate(valueAnimator,
(_valueAnimator, _gradientDrawable) -> {
_gradientDrawable.setAngle((Float) _valueAnimator.getAnimatedValue());
return Unit.INSTANCE;
});