我正在尝试在JavaFX中创建动画。
然而,我对一个方法的理解有困难。
有人能解释一下底部的interpolate方法具体是做什么的吗?
更具体地说,它如何使用模数?
import javafx.animation.Interpolator;
import javafx.animation.Transition;
import javafx.geometry.Rectangle2D;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
public class SpriteAnimation extends Transition {
private ImageView imageView;
private int count;
private int columns;
private int offsetX;
private int offsetY;
private int width;
private int height;
private int lastIndex;
public SpriteAnimation(
ImageView imageView,
Duration duration,
int count, int columns,
int offsetX, int offsetY,
int width, int height) {
this.imageView = imageView;
this.count = count;
this.columns = columns;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.width = width;
this.height = height;
setCycleDuration(duration);
setInterpolator(Interpolator.LINEAR);
}
protected void interpolate(double k) {
//
int index = Math.min((int) Math.floor(k * count), count - 1);
if (index != lastIndex) {
int x = (index % columns) * width + offsetX;
int y = (index / columns) * height + offsetY;
imageView.setViewport(new Rectangle2D(x, y, width, height));
lastIndex = index;
}
}
}