I am creating a custom component in HarmonyOS using Java SDK. Where I have to work on Animation for animate component. I have to animates component between int values. For that I need the instance of AnimatorValue(In Android ValueAnimator).
In Android we can create instance of ValueAnimator and pass int values like this:
ValueAnimator finalPositionAnimator = ValueAnimator.ofInt(10, 100);
but, In HMOS I am able to create instance of AnimatorValue using below code:
AnimatorValue finalPositionAnimator = new AnimatorValue();
but, I am not able to set int values.
I did as follow.
First create getAnimatedValue() util function as follow.
public static float getAnimatedValue(float fraction, int... values) {
if (values == null || values.length == 0) {
return 0;
}
if (values.length == 1) {
return values[0] * fraction;
} else {
if (fraction == 1) {
return values[values.length - 1];
}
float oneFraction = 1f / (values.length - 1);
float offFraction = 0;
for (int i = 0; i < values.length - 1; i++) {
if (offFraction + oneFraction >= fraction) {
return values[i] + (fraction - offFraction) * (values.length - 1) * (values[i + 1] - values[i]);
}
offFraction += oneFraction;
}
}
return 0;
}
Then, you can use above method like this way
AnimatorValue animatorValue = new AnimatorValue();
animatorValue.setValueUpdateListener((animatorValue, v) -> {
float value = AnimationUtils.getAnimatedValue(v, 10, 100);
});
You can see more methods similar like ofInt() under AnimatorValue here