packageatomQQswt.ui.core;importjava.io.InputStream;importorg.eclipse.swt.SWT;importorg.eclipse.swt.graphics.Color;importorg.eclipse.swt.graphics.GC;importorg.eclipse.swt.graphics.Image;importorg.eclipse.swt.graphics.ImageData;importorg.eclipse.swt.graphics.ImageLoader;importorg.eclipse.swt.graphics.Point;importorg.eclipse.swt.graphics.Rectangle;importorg.eclipse.swt.widgets.Canvas;importorg.eclipse.swt.widgets.Composite;importorg.eclipse.swt.widgets.Display;importorg.eclipse.swt.widgets.Event;importorg.eclipse.swt.widgets.Listener;publicclassImageCanvasextendsCanvas {privateImageLoader loader;protectedPoint origin=newPoint(0,0);protectedImage image;protectedImageData[] imageDatas;protectedImage[] images;protectedintcurrent;privateintrepeatCount;privateRunnable animationTimer;privateColor bg;privateDisplay display;publicImageCanvas(Composite parent) {super(parent, SWT.DOUBLE_BUFFERED|SWT.INHERIT_FORCE|SWT.INHERIT_DEFAULT);
bg=getBackground();
display=getDisplay();
addListeners();
}/*** 传入路径获取图片.
*
*@parampath the new image*/publicvoidsetImage(String path){
loader=newImageLoader();
imageDatas=loader.load(path);if(imageDatas.length==0)return;elseif(imageDatas.length==1){
checkWidget();
stopAnimationTimer();this.image=newImage(display, imageDatas[0]);this.imageDatas=null;this.images=null;
redraw();
}else{
checkWidget();this.image=null;this.repeatCount=loader.repeatCount;
convertImageDatasToImages();
startAnimationTimer();
redraw();
}
}/*** 传入InputStream获取图片.
*
*@paramisin the new image*/publicvoidsetImage(InputStream isin){
loader=newImageLoader();
imageDatas=loader.load(isin);if(imageDatas.length==0)return;elseif(imageDatas.length==1){
checkWidget();
stopAnimationTimer();this.image=newImage(display, imageDatas[0]);this.imageDatas=null;this.images=null;
redraw();
}else{
checkWidget();this.image=null;this.repeatCount=loader.repeatCount;
convertImageDatasToImages();
startAnimationTimer();
redraw();
}
}
@OverridepublicPoint computeSize(intwHint,inthHint,booleanchanged) {
checkWidget();
Image image=getCurrentImage();if(image!=null) {
Rectangle rect=image.getBounds();
Rectangle trim=computeTrim(0,0, rect.width, rect.height);returnnewPoint(trim.width, trim.height);
}returnnewPoint(wHint, hHint);
}
@Overridepublicvoiddispose() {if(image!=null)
image.dispose();if(images!=null)for(inti=0; i
images[i].dispose();super.dispose();
}protectedvoidpaint(Event e) {
Image image=getCurrentImage();if(image==null)return;
GC gc=e.gc;
gc.drawImage(image, origin.x, origin.y);
gc.setBackground(bg);
Rectangle rect=image.getBounds();
Rectangle client=getClientArea();intmarginWidth=client.width-rect.width;if(marginWidth>0) {
gc.fillRectangle(rect.width,0, marginWidth, client.height);
}intmarginHeight=client.height-rect.height;if(marginHeight>0) {
gc.fillRectangle(0, rect.height, client.width, marginHeight);
}
}voidaddListeners() {
addListener(SWT.Paint,newListener() {publicvoidhandleEvent(Event e) {
paint(e);
}
});
}voidconvertImageDatasToImages() {
images=newImage[imageDatas.length];//Step 1: Determine the size of the resulting images.intwidth=imageDatas[0].width;intheight=imageDatas[0].height;//Step 2: Construct each image.inttransition=SWT.DM_FILL_BACKGROUND;for(inti=0; i
ImageData id=imageDatas[i];
images[i]=newImage(display, width, height);
GC gc=newGC(images[i]);//Do the transition from the previous image.switch(transition) {caseSWT.DM_FILL_NONE:caseSWT.DM_UNSPECIFIED://Start from last image.gc.drawImage(images[i-1],0,0);break;caseSWT.DM_FILL_PREVIOUS://Start from second last image.gc.drawImage(images[i-2],0,0);break;default://DM_FILL_BACKGROUND or anything else,//just fill with default background.gc.setBackground(bg);
gc.fillRectangle(0,0, width, height);break;
}//Draw the current image and clean up.Image img=newImage(display, id);
gc.drawImage(img,0,0, id.width, id.height, id.x, id.y, id.width,
id.height);
img.dispose();
gc.dispose();//Compute the next transition.//Special case: Can't do DM_FILL_PREVIOUS on the//second image since there is no "second last"//image to use.transition=id.disposalMethod;if(i==0&&transition==SWT.DM_FILL_PREVIOUS)
transition=SWT.DM_FILL_NONE;
}
}
Image getCurrentImage() {if(image!=null)returnimage;if(images==null)returnnull;returnimages[current];
}voidstartAnimationTimer() {if(images==null||images.length<2)return;intdelay=imageDatas[current].delayTime*20;if(delay<5) delay=100;finalintms=delay;
display.timerExec(ms, animationTimer=newRunnable() {publicvoidrun() {if(isDisposed())return;
current=(current+1)%images.length;
redraw();if(current+1==images.length&&repeatCount!=0&&--repeatCount<=0)return;
display.timerExec(ms,this);
}
});
}voidstopAnimationTimer() {if(animationTimer!=null)
display.timerExec(-1, animationTimer);
}
}