Ishaan Patel
Ishaan Patel

Reputation: 225

Kotlin to Java (Library Help)

I am using this library to generate gradients: https://github.com/revely-inc/co.revely.gradient

The kotlin code to animate the gradient is as follows:

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

The java code that I have attained until now:

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

As you notice, I cannot convert this (since it doesn't make sense to a kotlin outsider):

{ _valueAnimator, _gradientDrawable ->
   _gradientDrawable.angle = _valueAnimator.animatedValue as Float
   }

What is supposed to be done here?

Error:

required: 'kotlin.jvm.functions.Function2? super android.animation.ValueAnimator,? super co.revely.gradient.drawables.Gradient,kotlin.Unit>

Upvotes: 0

Views: 892

Answers (1)

Jack Meister
Jack Meister

Reputation: 247

That's an anonymous function (a "lambda expression"). It looks like RevelyGradient.animate() takes two arguments:

  1. A ValueAnimator.
  2. A function: one that takes a ValueAnimator and a Gradient as arguments and returns nothing useful. (It returns a Unit.)

In the specific function you passed in, the Gradient's angle property is being set from the ValueAnimator's animatedValue property (which is cast to a Float before assignment).

So how do you rewrite this in Java? Since this is an Android question, you'll need to enable Java 8 language features for the module you're working on. Then you should be able to rewrite that line of code to look something like this:

RevelyGradient
    .animate(valueAnimator,
            (_valueAnimator, _gradientDrawable) -> {
               _gradientDrawable.setAngle((Float) _valueAnimator.getAnimatedValue());
               return Unit.INSTANCE;
            });

Upvotes: 3

Related Questions