|
在做一個SWT的通用DB(jdbc)工具時,為了在主畫面上加個logo,用上了俺家貓貓狗狗的照片。為了展示出這些照片,這個工具成了一個副產(chǎn)品。 SWT的image功能是俺以前沒有碰過的,第一次用的時候碰到了不少問題。比如說閃屏,比如說淡入淡出的實現(xiàn)。 說實話,現(xiàn)在的淡入淡出感覺比較完美——前圖的淡出和后圖的淡入非常連貫。但是這也只是個偶然的bug造成的結(jié)果,并不是俺一開始就希望得到的結(jié)果,一開始,俺希望是白淡入,黑淡出,再白淡入。最后,黑淡出并沒有實現(xiàn),有些遺憾。 淡入淡出功能的實現(xiàn),主要是設(shè)置imagedata的alphaData。研究alphaData的時候順便也看一點mask的文檔,用mask似乎可以實現(xiàn)不規(guī)則窗口,以后再好好研究一下。 閃屏的問題,一直想用雙緩存去解決,最后畫圖用的canvas只要設(shè)置成SWT.NO_BACKGROUND就OK了,于是雙緩存也沒有去實現(xiàn)。其實,雙緩存也試過,不知道是俺寫得不對,還是別的什么原因,雙緩存也不能解決非SWT.NO_BACKGROUND的閃屏問題。
package per.chenxin.test;![]() import java.io.IOException;![]() import org.eclipse.swt.SWT; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell;![]() public class ImageTest { public ImageTest imageTest; public Display display; public Canvas canvas; Util util = null; ImageData imageData = null; byte[] alphaData = null; public int fade = 0;![]() // public boolean bMouseFlg = false; // public int intMouseX = -1; // public int intMouseY = -1; FadeThread fadeThread = null; public boolean isFirst = true;![]() ![]() public ImageTest() throws IOException ...{ imageTest = this; util = Util.getInstance(); imageData = new ImageData(util.getInputStream()); alphaData = new byte[imageData.data.length]; imageData.alphaData = alphaData; }![]() ![]() public void open() ...{ display = Display.getDefault(); final Shell shell = new Shell();![]() shell.addDisposeListener(new DisposeListener() ...{![]() public void widgetDisposed(final DisposeEvent e) ...{![]() try ...{ util.finalize();![]() } catch (Throwable e1) ...{ e1.printStackTrace(); } } }); shell.setText("Image Show"); //![]() shell.open();![]() canvas = new Canvas(shell, SWT.NO_BACKGROUND);![]() canvas.addDisposeListener(new DisposeListener() ...{![]() public void widgetDisposed(final DisposeEvent e) ...{ fadeThread.isActive = false; } }); canvas.setBackground(new Color(display, 0, 0, 0));![]() // canvas.addMouseMoveListener(new MouseMoveListener() { // public void mouseMove(final MouseEvent e) { // if ((e.x - intMouseX) * (e.x - intMouseX) + (e.y - intMouseY) // * (e.y - intMouseY) > 10) { // canvas.redraw(); // bMouseFlg = true; // intMouseX = e.x; // intMouseY = e.y; // } // // } // });![]() canvas.addMouseListener(new MouseAdapter() ...{![]() public void mouseDown(final MouseEvent e) ...{![]() try ...{ if (!util.next()) util = Util.getInstance(); fadeThread.isActive = false; fadeThread = new FadeThread(imageTest); new Thread(fadeThread).start(); fade = 0; imageData = new ImageData(util.getInputStream()); for (int i = 0; i < alphaData.length; i++) alphaData[i] = (byte) fade; imageData.alphaData = alphaData; fade = 0; // bMouseFlg = true; canvas.redraw();![]() } catch (IOException e1) ...{ e1.printStackTrace(); } } });![]() canvas.addPaintListener(new PaintListener() ...{![]() public void paintControl(final PaintEvent e) ...{![]() try ...{![]() if (imageData == null) ...{ imageData = new ImageData(util.getInputStream()); alphaData = new byte[imageData.data.length]; imageData.alphaData = alphaData; fade = 0; } canvas.setSize(imageData.width, imageData.height); shell.setSize(imageData.width, imageData.height);![]() if (isFirst) ...{ ImageData imageData2 = (ImageData) imageData.clone();![]() for (int i = 0; i < imageData2.data.length; i++) ...{ imageData2.data[i] = (byte) 255; imageData2.alphaData[i] = (byte) 255; } Image image = new Image(display, imageData2); e.gc.drawImage(image, 0, 0);![]() isFirst = false;![]() } else ...{ Image image = new Image(display, imageData); e.gc.drawImage(image, 0, 0); }![]() // if (bMouseFlg) { // e.gc.drawOval(intMouseX - 50, intMouseY - 50, 100, 100); // bMouseFlg = false; // }![]() ![]() } catch (IOException e1) ...{ e1.printStackTrace(); }![]() } }); canvas.setBounds(0, 0, imageData.width, imageData.height); shell.layout();![]() fadeThread = new FadeThread(this); new Thread(fadeThread).start();![]() ![]() while (!shell.isDisposed()) ...{ if (!display.readAndDispatch()) display.sleep(); } }![]() ![]() /** *//** * Launch the application * * @param args */![]() public static void main(String[] args) ...{![]() try ...{ (new ImageTest()).open();![]() } catch (IOException e) ...{ e.printStackTrace(); }![]() }![]() ![]() static class FadeThread implements Runnable ...{ ImageTest imageTest; public boolean isActive = false;![]() ![]() public FadeThread(ImageTest imageTest) ...{ this.imageTest = imageTest; }![]() ![]() public void run() ...{ isActive = true;![]() while (imageTest.fade <= 255) ...{![]() try ...{ Thread.sleep(50);![]() } catch (InterruptedException e) ...{ } if (imageTest.isFirst) continue; if (!isActive) break; for (int i = 0; i < imageTest.alphaData.length; i++) imageTest.alphaData[i] = (byte) imageTest.fade; // if (imageTest.intMouseX >= 0 || imageTest.intMouseY >= 0) { // imageTest.bMouseFlg = true; // }![]() imageTest.display.asyncExec(new Runnable() ...{![]() public void run() ...{ imageTest.canvas.redraw(); } }); imageTest.fade += 5; } isActive = false; } }![]() }![]()
package per.chenxin.test;![]() import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream;![]() public class Util { private static boolean isInited = false; private static Util util; private static String STR_IMAGE = "IMG_0328.zip";![]() public byte[] bytesZipInputStream = null; public String strZipEntryName = null;![]() private ZipInputStream zipInputStream = null; private ZipEntry zipEntry = null;![]() ![]() private Util() throws IOException ...{ zipInputStream = new ZipInputStream(new FileInputStream(STR_IMAGE)); zipEntry = zipInputStream.getNextEntry(); strZipEntryName = zipEntry.getName(); zipEntry = zipInputStream.getNextEntry(); if (zipEntry != null) strZipEntryName = zipEntry.getName(); }![]() ![]() public InputStream getInputStream() throws IOException ...{ while (zipInputStream.available() == 0) next(); return zipInputStream; }![]() ![]() public boolean next() throws IOException ...{ zipEntry = zipInputStream.getNextEntry();![]() if (zipEntry == null) ...{![]() try ...{ finalize();![]() } catch (Throwable e) ...{ e.printStackTrace(); } isInited = false; init(); return false; } else strZipEntryName = zipEntry.getName(); return true; }![]() ![]() public static Util getInstance() throws IOException ...{ Util.init(); return util; }![]() ![]() public static void init() throws IOException ...{![]() ![]() if (!isInited) ...{ util = new Util(); isInited = true; } }![]() ![]() static ...{![]() try ...{ init();![]() } catch (FileNotFoundException e) ...{ e.printStackTrace();![]() } catch (IOException e) ...{ e.printStackTrace(); } }![]() ![]() protected void finalize() throws Throwable ...{![]() if (zipInputStream != null) ...{ if (zipEntry != null) zipInputStream.closeEntry(); zipInputStream.close(); } }![]() ![]() public byte[] getBytesZipInputStream() throws IOException ...{ bytesZipInputStream = new byte[zipInputStream.available()]; zipInputStream.read(bytesZipInputStream); return bytesZipInputStream; }![]() ![]() public String getStrZipEntryName() ...{ return strZipEntryName; } }![]() 上面代碼被注釋掉的部分是在mouse移動時,在mouse處加個圓圈,是用來實現(xiàn)放大鏡功能的。但是imagedata的構(gòu)造還沒有完全搞清楚,放大鏡功能如何實現(xiàn),也想到網(wǎng)上找找資料,所以先注釋掉了。 |
|
|