Java實(shí)現(xiàn)多種幻燈片切換特效(附源碼)
功能說(shuō)明: 代碼實(shí)現(xiàn)了多種幻燈片變換特效. 如:淡入淡出、緩慢覆蓋、旋轉(zhuǎn)覆蓋等10多種變換效果。
功能實(shí)現(xiàn):
1、圖片加載類(lèi)ImageLoader實(shí)現(xiàn):
1)用阻塞隊(duì)列存儲(chǔ)要圖片:BlockingQueue images = new ArrayBlockingQueue<>(2);
2)用圖片eof表示圖片隊(duì)列結(jié)束:Image eof = new WritableImage(1, 1);
3)循環(huán)讀取指定圖片,由于是阻塞隊(duì)列,所以當(dāng)隊(duì)列滿(mǎn)的時(shí)候線程會(huì)自動(dòng)阻塞.
- 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、特效實(shí)現(xiàn) 以弧形切換圖片為例: 首先定義LengthTransition變化特效:設(shè)置變化時(shí)間,以及弧度數(shù)跟時(shí)間的變化關(guān)系。
- 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);
- }
- }
然后設(shè)置圖片層疊效果:
- group.setBlendMode(BlendMode.SRC_OVER);
- next.setBlendMode(BlendMode.SRC_ATOP);
以及之前那張圖片的淡出特效:
- FadeTransition ft = new FadeTransition(Duration.seconds(0.2), mask2);
最后同時(shí)執(zhí)行這兩個(gè)特效:
- ParallelTransition pt = new ParallelTransition(lt, ft);
效果圖:
源碼下載: 進(jìn)入下載頁(yè)面
原文鏈接:http://www.cnblogs.com/javafx/archive/2013/03/30/2990259.html