概述
在SWT中下面两个方法都可以实现Image缩放,
GC.drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int dstX, int dstY, int dstWidth, int dstHeight);
ImageData.scaledTo(int width, int height)
但是为了保证缩放图像质量,还是用GC.drawImage好一些。
/**
* 根据指定的宽高对{@link Image}图像进行绽放
* @param src 原图对象
* @param width 目标图像宽度
* @param height 目标图像高度
* @return 返回缩放后的{@link Image}对象
*/
private Image resize(Image src, int width, int height) {
Image scaled = new Image(Display.getDefault(), width, height);
GC gc = new GC(scaled);
try{
gc.setAdvanced(true); 、// 打开高级绘图模式
gc.setAntialias(SWT.ON);// 设置消除锯齿
gc.setInterpolation(SWT.HIGH); // 设置插值
gc.drawImage(src, 0, 0, src.getBounds().width, src.getBounds().height,0, 0, width, height);
}finally{
gc.dispose();
}
return scaled;
}
/**
* 根据缩放比例对{@link Image}对象进行缩放
* @param src 原图对象
* @param zoom 缩放比例
* @return 返回缩放后的{@link Image}对象
*/
private Image resize(Image src, float zoom) {
Rectangle bounds = src.getBounds();
bounds.width*=zoom;
bounds.height*=zoom;
return resize(src,bounds.width,bounds.height);
}
参考资料:
http://www.ibm.com/developerworks/cn/opensource/os-cn-swtimage2/
http://blog.csdn.net/zhangzh332/article/details/6687812
最后
以上就是沉默导师为你收集整理的java:SWT 缩放图像(Image)的全部内容,希望文章能够帮你解决java:SWT 缩放图像(Image)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复