整理了一个工具类, 用于在SWT中显示GIF图片

整理了一个工具类, 用于在SWT中显示GIF图片

Java代码 复制代码 收藏代码
  1. /**
  2. * 用于显示GIF图片的util类
  3. *
  4. * @author mark.wang
  5. */
  6. public class ImageViewer extends Canvas {
  7. protected Point origin = new Point(0, 0);
  8. protected Image image;
  9. protected ImageData[] imageDatas;
  10. protected Image[] images;
  11. protected int current;
  12. private int repeatCount;
  13. private Runnable animationTimer;
  14. private Color bg;
  15. private Display display;
  16. public ImageViewer(Composite parent) {
  17. super(parent, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE);
  18. bg = getBackground();
  19. display = getDisplay();
  20. addListeners();
  21. }
  22. /**
  23. * @param repeatCount
  24. * 0 forever
  25. */
  26. public void setImages(ImageData[] imageDatas, int repeatCount) {
  27. checkWidget();
  28. this.image = null;
  29. this.imageDatas = imageDatas;
  30. this.repeatCount = repeatCount;
  31. convertImageDatasToImages();
  32. startAnimationTimer();
  33. redraw();
  34. }
  35. @Override
  36. public Point computeSize(int wHint, int hHint, boolean changed) {
  37. checkWidget();
  38. Image image = getCurrentImage();
  39. if (image != null) {
  40. Rectangle rect = image.getBounds();
  41. Rectangle trim = computeTrim(0, 0, rect.width, rect.height);
  42. return new Point(trim.width, trim.height);
  43. }
  44. return new Point(wHint, hHint);
  45. }
  46. @Override
  47. public void dispose() {
  48. if (image != null)
  49. image.dispose();
  50. if (images != null)
  51. for (int i = 0; i < images.length; i++)
  52. images[i].dispose();
  53. super.dispose();
  54. }
  55. protected void paint(Event e) {
  56. Image image = getCurrentImage();
  57. if (image == null)
  58. return;
  59. GC gc = e.gc;
  60. gc.drawImage(image, origin.x, origin.y);
  61. gc.setBackground(bg);
  62. Rectangle rect = image.getBounds();
  63. Rectangle client = getClientArea();
  64. int marginWidth = client.width - rect.width;
  65. if (marginWidth > 0) {
  66. gc.fillRectangle(rect.width, 0, marginWidth, client.height);
  67. }
  68. int marginHeight = client.height - rect.height;
  69. if (marginHeight > 0) {
  70. gc.fillRectangle(0, rect.height, client.width, marginHeight);
  71. }
  72. }
  73. void addListeners() {
  74. addListener(SWT.Paint, new Listener() {
  75. public void handleEvent(Event e) {
  76. paint(e);
  77. }
  78. });
  79. }
  80. void convertImageDatasToImages() {
  81. images = new Image[imageDatas.length];
  82. // Step 1: Determine the size of the resulting images.
  83. int width = imageDatas[0].width;
  84. int height = imageDatas[0].height;
  85. // Step 2: Construct each image.
  86. int transition = SWT.DM_FILL_BACKGROUND;
  87. for (int i = 0; i < imageDatas.length; i++) {
  88. ImageData id = imageDatas[i];
  89. images[i] = new Image(display, width, height);
  90. GC gc = new GC(images[i]);
  91. // Do the transition from the previous image.
  92. switch (transition) {
  93. case SWT.DM_FILL_NONE:
  94. case SWT.DM_UNSPECIFIED:
  95. // Start from last image.
  96. gc.drawImage(images[i - 1], 0, 0);
  97. break;
  98. case SWT.DM_FILL_PREVIOUS:
  99. // Start from second last image.
  100. gc.drawImage(images[i - 2], 0, 0);
  101. break;
  102. default:
  103. // DM_FILL_BACKGROUND or anything else,
  104. // just fill with default background.
  105. gc.setBackground(bg);
  106. gc.fillRectangle(0, 0, width, height);
  107. break;
  108. }
  109. // Draw the current image and clean up.
  110. Image img = new Image(display, id);
  111. gc.drawImage(img, 0, 0, id.width, id.height, id.x, id.y, id.width,
  112. id.height);
  113. img.dispose();
  114. gc.dispose();
  115. // Compute the next transition.
  116. // Special case: Can't do DM_FILL_PREVIOUS on the
  117. // second image since there is no "second last"
  118. // image to use.
  119. transition = id.disposalMethod;
  120. if (i == 0 && transition == SWT.DM_FILL_PREVIOUS)
  121. transition = SWT.DM_FILL_NONE;
  122. }
  123. }
  124. Image getCurrentImage() {
  125. if (image != null)
  126. return image;
  127. if (images == null)
  128. return null;
  129. return images[current];
  130. }
  131. void startAnimationTimer() {
  132. if (images == null || images.length < 2)
  133. return;
  134. final int delay = imageDatas[current].delayTime * 10;
  135. display.timerExec(delay, animationTimer = new Runnable() {
  136. public void run() {
  137. if (isDisposed())
  138. return;
  139. current = (current + 1) % images.length;
  140. redraw();
  141. if (current + 1 == images.length && repeatCount != 0
  142. && --repeatCount <= 0)
  143. return;
  144. display.timerExec(delay, this);
  145. }
  146. });
  147. }
  148. void stopAnimationTimer() {
  149. if (animationTimer != null)
  150. display.timerExec(-1, animationTimer);
  151. }
  152. public static void main(String[] args) {
  153. Display display = new Display();
  154. final Shell shell = new Shell(display);
  155. CopyOfImageViewer ic = new CopyOfImageViewer(shell);
  156. shell.setLayout(new FillLayout());
  157. FileDialog dialog = new FileDialog(shell, SWT.OPEN);
  158. dialog.setText(" Open an gif image file or cancel, only gif ");
  159. String string = dialog.open();
  160. ImageLoader loader = new ImageLoader();
  161. ImageData[] imageDatas = loader.load(string);
  162. ic.setImages(imageDatas, loader.repeatCount);
  163. ic.pack();
  164. shell.pack();
  165. shell.open();
  166. while (!shell.isDisposed()) {
  167. if (!display.readAndDispatch())
  168. display.sleep();
  169. }
  170. display.dispose();
  171. }
  172. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值