有关gif disposal method

转自:http://blog.itpub.net/13685345/viewspace-514315

利用gif图片制作简单动画是常用的渲染手段,swing虽然支持gif图片格式并可以自动地实现动画效果。
通常最简单地将gif图片放到swing组件上是调用JButton或JLabel的setIcon(Icon icon)方法。
还有一种方法是重写paintComponent(Graphics g)或paint(Graphics g)方法。例如
public class ShowGifPanel extends JPanel{
    ImageIcon image = new ImageIcon("/root/opt/loading.gif");

    @Override
    public void paint(Graphics g) {
        g.drawImage(image.getImage(), 0, 0, this);
    }
}
通过上述方法呈现如下两个gif。


但是事实情况却是:不要企图通过这样简单的处理达到理想的效果。如果你这样做的话马上会发现gif的刷新率往往非常快,看上去gif图片桢刷新很快,或者应该说太快了。
swing还提供了一种实现手段是设置一组相似的gif,通过轮循显示来呈现,通过下图就明了了。

这样虽然可以呈现,但是对于一个动画来说就必须提供多个gif。对于占用空间和给美工的负担都不利。

如果你使用SWT呈现Gif,Eclipse提供了一个方案。
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java?view=co
其基本原理就是将Gif的各个桢轮循地显示,如果你以这个程序运行loading.gif,看上去还是很快,可以通过修改第131~137行之间的代码来调节刷新率。这样SWT就能完美实现处理Gif了。
不幸的是,将SWT的那种方式移植到Swing中却达不到很好的效果。在Swing中要想完美实现处理Gif需要额外的一些工作。

首先需要对Gif这种图片格式有一些基本认识。
第一:Gif由一系列Image组成,也就是桢,Gif动画就是连续地显示这些桢,但是这还不够。
第二:无论某一时刻轮循到哪一桢,第1桢,总是要当作背景画出来,而且第1桢也是所有桢当中最长最高的,它的尺寸也是整个Gif图象的尺寸,位置从(0, 0)开始,其余各桢可能只是描述与相临各桢变化的部分,所以长和高要小且不完整,起始位置是该桢相对整体背景的位置。(这点SWT也是这样做的)
第三:Gif动画连续显示不一定是各个桢轮循单独显示,而是不仅仅显示当前该显示的桢,还要向前追溯到"第一桢",从"第一桢"开始到当前应该显示的桢组成的连续一系列"桢簇",所以某一时刻单单显示背景和当前桢是不够的,而是显示背景和当前"桢簇"。""桢簇""是我自己取的名字,而且我看SWT轮循的例子中并没有用到"桢簇",而是传统的单桢轮循。但是同样的方法对Swing不奏效,现在我对此还不得其解。关于"第一桢",是和com.sun.imageio.plugins.gif.GIFImageMetadata类的disposalMethod属性有关,在SWT中这个属性是org.eclipse.swt.graphics.ImageData.disposalMethod。disposalMethod据我的研究是描述处理桢的方法,常见的disposalMethod取值有none(取值0,不处理)、Background(取值2,背景)两种,所谓的当前桢的"第一桢"就是向前追溯到最近的disposalMethod取值为2的那一桢的下一桢,也就是说或者"第一桢"的前一桢的disposalMethod取值为2,或者"第一桢"就是Gif索引为2的桢,因为Gif的第1桢总要当背景显示。
第四:桢的元数据在SWT中用org.eclipse.swt.graphics.ImageData类封装,在Swing中对应的是com.sun.imageio.plugins.gif.GIFImageMetadata(可是截止到JDK6.0 u11,这个类的版本号还是0.5,有些另人失望:(),可以通过次类获取到delayTime这个属性,也就是下一桢的间隔时间,但是有很多Gif,这个值总是0,所以Swing显示频率相当的快。

以下是本人写的2个参考实现,其中GifAnalysis.java是gif的分析工具,它将Gif的各个桢单独拿出来分析比对,并列出了上面提到的一些属性。如下图



通过比较发现loading.gif各个桢的delayTime均为0,因此单纯地将loading.gif设置为JLabel等组件的icon属性效果必定会出问题,可以通过美工解决。


转自:http://www.webreference.com/content/studio/disposal.html

This is an excerpt from Richard Koman's new book, GIF Animation Studio, due out in mid-October from Songline Studios and O'Reilly & Associates. The book is the first in a new series called the Web Review Studio series. Upcoming books will tackle Shockwave, Java and streaming audio.

One of the most powerful aspects of GIF animation is its layering capability. That is, each frame of the animation doesn't have to contain the full image. Much liketraditional cel animation, GIF animation lets underlying layers show through while new layers contain just the part of the animation that changes. This is accomplished with a somewhat mysterious option called disposal method.

In GIFBuilder, disposal method is set by highlighting one or all of the frames in the Frames window and choosing one of the options -- Do Not Dispose, Dispose to Background, Dispose to Previous or Unspecified.

The important thing to realize about disposal methods is that they are related to the transparency mode. That is, if your frames don't have transparency, you don't have to worry much about disposal methods. But if you are using transparency, then it's critical to set the right disposal mode.

What is disposal method? It is simply the answer to the question: What do you do with the previous frame? The choices are:

Unspecified. Use this option to replace one full-size, non-transparent frame with another.

One of the most powerful aspects of GIF animation is its layering capability. That is, each frame of the animation doesn't have to contain the full image. Much liketraditional cel animation, GIF animation lets underlying layers show through while new layers contain just the part of the animation that changes. This is accomplished with a somewhat mysterious option called disposal method.

In GIFBuilder, disposal method is set by highlighting one or all of the frames in the Frames window and choosing one of the options -- Do Not Dispose, Dispose to Background, Dispose to Previous or Unspecified.


Do Not Dispose. In this option, any pixels not covered up by the next frame continue to display. This is the setting used most often for optimized animations. In the flashing light animation, we wanted to keep the first frame displaying, so the subsequent optimized frames would just replace the part that we wanted to change. That's what Do Not Dispose does.

Restore to Background. The background color or background tile - rather than a previous frame - shows through transparent pixels. In the GIF specification, you can set a background color. In Netscape, it's the page's background color or background GIF that shows through.


Restore to Previous. Restores to the state of a previous, undisposed frame. Figures 1 and 2 show the effect of this option. Figure 1 shows the three component frames of the animation. The first frame is a full-frame image of the letter A. For the second frame, we took just the top half of the letter and applied a Gaussian blur in Photoshop. For the third frame we took just the bottom half of the letter and applied Photoshop's filter.

The first frame is set to Do Not Dispose, while the other two frames are set to Restore to Previous. Figure 2 shows the effect of these settings. The second frame displays the blurred top of the letter, while the bottom of the normal A from the first frame is displayed on the bottom. The third frame displays the rippled bottom of the A with the normal A from Frame 1 showing through the top.

The thing to remember about Restore to Previous is that it's not necessarily the first frame of the animation that will be restored but the last frame set to Unspecified or Do Not Dispose. And the most important fact is that Netscape doesn't correctly support Restore to Previous. It treats Restore to Previous as Do Not Dispose, so the the last frame -- not the last undisposed frame -- shows through empty or transparent areas. Microsoft Internet Explorer 3.0 does handle Restore to Previous correctly.


[Transparency settings]
Now let's look at transparency. The first thing to know is that although you can set transparency in a variety of programs -- such as Adobe's GIF89a and BoxTop's PhotoGIF plug-ins for Photoshop -- GIFBuilder discards these transparency settings. To make transparent animations, you must set transparency in GIFBuilder.

To do this, select one or all of your frames in the Frames Window and choose Options/Transparent Background. This brings up another menu with the following options:


No. No transparency.

White. White pixels are transparent. (The RGB values for white are 255,255,255.)

Based on first pixel. The color of the "first pixel" of the animation -- that is, the top left pixel, the one at coordinates 0,0 -- is transparent. This is a handy option since often you'll have an image in the center and the four corners will be transparent.

Other. This option brings up a color picker so you can select a color for transparency.

[Putting disposal andtransparency together]
In the quest for the most efficient graphics, it makes sense to use transparency, optimization and disposal method together. Take a simple example: a spinning globe on a blue background.

Many designers will include the blue background as part of every frame and replace the entire image with each frame. But if you set the blue background to transparent (Options/Transparency/Based on First Pixel) and set disposal method to Revert to Background, you'll reduce the file size of each frame and improve the performance of the animation.

Since Web browsers use the background color of the page for the Revert to Background, you can set the background of the animation using the BGCOLOR tag. This has the advantage of divorcing the animation's background from the GIF file. If you decide you want a yellow background on your page, the animation's background changes automatically.

But Revert to Background isn't always the right answer.

If you've optimized your animation, Revert to Background would only display each optimized image surrounded by the background color. In this case, Do Not Dispose is the right choice. Let's look at an example that puts all of these concepts together.




Let's look at the tower animation in depth. SiteSpecific actually put this animation up as a full-frame animation without transparency or optimization. Optimizing the animation in GIFBuilder reduced file size from 20K to 16K. The first three frames of the animation are shown in Figure 3 and the corresponding optimized frames are shown in Figure 4.
   


With the animation optimized, the disposal method needs to be set to Do Not Dispose, so underlying images continue to display. The transparency setting is Based on First Pixel. It's certainly possible to leave transparency off (since it does little to affect file size). If the background color is also used in the image, leave transparency off. In this case, however, there are clean divisions between black and white and transparency works fine.
   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值