其实用的计算最终图片大小的代码跟以前的 Java 中收取邮件并自动缩放图片的代码(原创) 没有两样, 发现代码写多了, 好多思路都一样. 主要是想用作 Code Manager .SWT 的图片缩略图显示列表中的显示组件.
最后显示效果如图:
拖动窗口大小里面缩略图也会跟着缩放. 获取图片的代码部分可以自己写, 不用来自Jigloo 的 SWTResourceManager .
package test;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import com.cloudgarden.resource.SWTResourceManager;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ImageComposite extends org.eclipse.swt.widgets.Composite {
{
// Register as a resource user - SWTResourceManager will
// handle the obtaining and disposing of resources
SWTResourceManager.registerResourceUser(this);
}
private CLabel cLabelImage;
private Text textImageName;
private Image image;
/**
* Auto-generated main method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void main(String[] args) {
showGUI();
}
/**
* Auto-generated method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void showGUI() {
Display display = Display.getDefault();
Shell shell = new Shell(display);
ImageComposite inst = new ImageComposite(shell, SWT.NULL);
Point size = inst.getSize();
shell.setLayout(new FillLayout());
shell.layout();
if(size.x == 0 && size.y == 0) {
inst.pack();
shell.pack();
} else {
Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
shell.setSize(shellBounds.width, shellBounds.height);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public ImageComposite(org.eclipse.swt.widgets.Composite parent, int style) {
super(parent, style);
image = SWTResourceManager.getImage("test.jpg");
initGUI();
}
private void initGUI() {
try {
GridLayout thisLayout = new GridLayout();
this.setLayout(thisLayout);
this.setSize(190, 88);
{
GridData cLabelImageLData = new GridData();
cLabelImageLData.horizontalAlignment = GridData.FILL;
cLabelImageLData.verticalAlignment = GridData.FILL;
cLabelImageLData.grabExcessVerticalSpace = true;
cLabelImageLData.grabExcessHorizontalSpace = true;
cLabelImage = new CLabel(this, SWT.NONE);
cLabelImage.setLayoutData(cLabelImageLData);
cLabelImage.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent evt) {
cLabelImagePaintControl(evt);
}
});
}
{
textImageName = new Text(this, SWT.NONE);
GridData textImageNameLData = new GridData();
textImageNameLData.horizontalAlignment = GridData.CENTER;
textImageName.setText("test.jpg");
}
this.layout();
} catch (Exception e) {
e.printStackTrace();
}
}
private void cLabelImagePaintControl(PaintEvent evt) {
Rectangle bounds = image.getBounds();
Point size = cLabelImage.getSize();
int MAX_WIDTH = size.x;// TODO: 缩放后的图片最大宽度
int MAX_HEIGHT = size.y;// TODO: 缩放后的图片最大高度
int imageWidth = bounds.width;
int imageHeight = bounds.height;
// determine thumbnail size from MAX_WIDTH and MAX_HEIGHT
int thumbWidth = MAX_WIDTH;
int thumbHeight = MAX_HEIGHT;
double thumbRatio = (double) thumbWidth / (double) thumbHeight;
double imageRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int) (thumbWidth / imageRatio);
} else {
thumbWidth = (int) (thumbHeight * imageRatio);
}
// 如果图片小于所略图大小, 不作处理
if (imageWidth < MAX_WIDTH && imageHeight < MAX_HEIGHT) {
thumbWidth = imageWidth;
thumbHeight = imageHeight;
}
evt.gc.drawImage(image, 0, 0, bounds.width, bounds.height, 0, 0, thumbWidth, thumbHeight);
// evt.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight)
}
}
更好的图片组件例子请参考出国留学人员 Chengdong Li (cdli@ccs.uky.edu) 撰写的: A basic image viewer; Also please see: Taking a look at SWT Images .