功能实现:
1、图片加载类ImageLoader实现:
1)用阻塞队列存储要图片:BlockingQueue images = new ArrayBlockingQueue<>(2);
2)用图片eof表示图片队列结束:Image eof = new WritableImage(1, 1);
3)循环读取指定图片,由于是阻塞队列,所以当队列满的时候线程会自动阻塞.
public void run() {
int id = 0;
try {
while (true) {
String path = resources[id];
InputStream is = getClass().getResourceAsStream(path);
if (is != null) {
Image image = new Image(is, width, height, true, true);
if (!image.isError()) {
images.put(image);
}
}
id++;
if (id >= resources.length) {
id = 0;
}
}
} catch (Exception e) {
} finally {
if (!cancelled) {
try {
images.put(eof);
} catch (InterruptedException e) {
}
}
}
}
2、特效实现 以弧形切换图片为例: 首先定义LengthTransition变化特效:设置变化时间,以及弧度数跟时间的变化关系。
class LengthTransition extends Transition {
Arc arc;
public LengthTransition(Duration d, Arc arc) {
this.arc = arc;
setCycleDuration(d);
}
@Override
protected void interpolate(double d) {
arc.setLength(d * 360);
}
}
然后设置图片层叠效果:
group.setBlendMode(BlendMode.SRC_OVER);
next.setBlendMode(BlendMode.SRC_ATOP);
以及之前那张图片的淡出特效:
FadeTransition ft = new FadeTransition(Duration.seconds(0.2), mask2);
最后同时执行这两个特效:
ParallelTransition pt = new ParallelTransition(lt, ft);