Java實現多種幻燈片切換特效(附源碼)
功能說明: 代碼實現了多種幻燈片變換特效. 如:淡入淡出、緩慢覆蓋、旋轉覆蓋等10多種變換效果。
功能實現:
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);
效果圖:
源碼下載: 進入下載頁面
原文鏈接:http://www.cnblogs.com/javafx/archive/2013/03/30/2990259.html