Kotlin to Java (Library Help)

138
January 13, 2018, at 09:38 AM

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>

Answer 1

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())
        );
READ ALSO
How to show SnackBar before another page destination loaded?

How to show SnackBar before another page destination loaded?

I'm really happy if I can show the snackbar before the destination page did loadMy current snackbar code here:

72
What should I specify as --tag parameter in Android SDK's avdmanager?

What should I specify as --tag parameter in Android SDK's avdmanager?

As GUI tools are gone in recent versions of Android SDK and I don't want to use their Android Studio IDE, I'm trying to create virtual device for emulator

86
Notification that apears one a week [on hold]

Notification that apears one a week [on hold]

Please help me I want that in my application will be a notification(notification with a text) that will apear one a week even if the aplication is closedI know that I need a class that will extand BroadCastReceiver

75
Still can't find a solution for “INTERACT_ACROSS_USERS_FULL”

Still can't find a solution for “INTERACT_ACROSS_USERS_FULL”

This issue is related to a previous topicI have an app with a Google Play fragment and it shows blank when opened

73