Codename One 图像调整大小性能
Codename One Image resizing performance
我认为将图像调整为给定尺寸(以像素为单位)以以所需尺寸显示它比使用该图像作为具有所需首选尺寸的 Label
背景更昂贵。你能证实这是真的吗?而且,如果是这样,为什么是真的?
我的意思是像下面这样使用 class 比调整包含的图像的大小更好(就性能而言)。您认为可以采用更快的方法吗?我需要以固定大小显示很多照片,但我的应用程序没有智能手机原生图库应用程序快。
/**
* Button useful to show the given Image at the given size, without the need to
* resize it.
*
* @author Francesco Galgani
*/
public class FixedSizeButton extends Button {
private final int imageWidth;
private final int imageHeight;
private Image image;
/**
* Creates a Button displaying an Image at the given fixed size; at least
* one of imageWidth or imageHeight must be specified.
*
* @param image
* @param imageWidth in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param imageHeight in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
*/
public FixedSizeButton(Image image, int imageWidth, int imageHeight) {
this(image, imageWidth, imageHeight, null);
}
/**
* Creates a Button displaying an Image at the given fixed size; at least
* one of imageWidth or imageHeight must be specified.
*
* @param image
* @param imageWidth in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param imageHeight in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param uiid
*/
public FixedSizeButton(Image image, int imageWidth, int imageHeight, String uiid) {
this(image, imageWidth, imageHeight, false, uiid);
}
/**
* Creates a Button displaying an Image at the given fixed size; at least
* one of imageWidth or imageHeight must be specified.
*
* @param image
* @param imageWidth in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param imageHeight in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param scaledSmallerRatio force the image to maintain the aspect ratio
* within the given dimension (it requires that both imageWidth and
* imageHeight are specified)
* @param uiid
*/
public FixedSizeButton(Image image, int imageWidth, int imageHeight, boolean scaledSmallerRatio, String uiid) {
if (image == null) {
throw new IllegalArgumentException("image cannot be null");
}
if (imageWidth <= 0 && imageHeight <= 0) {
throw new IllegalArgumentException("invalid imageWidth and imageHeight");
}
this.image = image;
setShowEvenIfBlank(true);
if (uiid != null) {
super.setUIID(uiid);
}
if (imageWidth < 1) {
imageWidth = image.getWidth() * imageHeight / image.getHeight();
} else if (imageHeight < 1) {
imageHeight = image.getHeight() * imageWidth / image.getWidth();
}
if (scaledSmallerRatio) {
float hRatio = ((float) imageHeight) / ((float) image.getHeight());
float wRatio = ((float) imageWidth) / ((float) image.getWidth());
if (hRatio < wRatio) {
imageWidth = (int) (image.getWidth() * hRatio);
} else {
imageHeight = (int) (image.getHeight() * wRatio);
}
}
this.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
this.getAllStyles().setBgImage(image);
this.imageWidth = imageWidth;
this.imageHeight = imageHeight;
}
@Override
public Dimension calcPreferredSize() {
int width = imageWidth + this.getStyle().getPaddingLeftNoRTL() + this.getStyle().getPaddingRightNoRTL();
int height = imageHeight + this.getStyle().getPaddingTop() + this.getStyle().getPaddingBottom();
return new Dimension(width, height);
}
/**
* Returns the background image
*
* @return the bg image
*/
@Override
public Image getIcon() {
return image;
}
@Override
public void setText(String text) {
throw new IllegalStateException("Not supported");
}
@Override
public void setUIID(String id) {
super.setUIID(id);
this.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
this.getAllStyles().setBgImage(image);
}
}
性能footprint/overhead 各不相同。当您执行 Image.scaled
(我假设这就是调整大小的意思...)时,图像可能会逐个像素地遍历并缩小为一个新的更小的字节数组。然后它可能会被重新编码为 PNG 或 JPEG。所有这些都是昂贵的任务。
请注意,我使用了 "might" 这个词,因为 iOS 并不总是这样做,并且在某些情况下使用硬件缩放...
当您使用绘图时,图像会作为纹理加载到 draws/scales 图像的 GPU 中。这具有零开销并且相当不错 fast.The 这里最大的缺点是 RAM,它在常规堆和 GPU 内存中都被占用。所以这是你需要做出的权衡。
我认为将图像调整为给定尺寸(以像素为单位)以以所需尺寸显示它比使用该图像作为具有所需首选尺寸的 Label
背景更昂贵。你能证实这是真的吗?而且,如果是这样,为什么是真的?
我的意思是像下面这样使用 class 比调整包含的图像的大小更好(就性能而言)。您认为可以采用更快的方法吗?我需要以固定大小显示很多照片,但我的应用程序没有智能手机原生图库应用程序快。
/**
* Button useful to show the given Image at the given size, without the need to
* resize it.
*
* @author Francesco Galgani
*/
public class FixedSizeButton extends Button {
private final int imageWidth;
private final int imageHeight;
private Image image;
/**
* Creates a Button displaying an Image at the given fixed size; at least
* one of imageWidth or imageHeight must be specified.
*
* @param image
* @param imageWidth in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param imageHeight in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
*/
public FixedSizeButton(Image image, int imageWidth, int imageHeight) {
this(image, imageWidth, imageHeight, null);
}
/**
* Creates a Button displaying an Image at the given fixed size; at least
* one of imageWidth or imageHeight must be specified.
*
* @param image
* @param imageWidth in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param imageHeight in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param uiid
*/
public FixedSizeButton(Image image, int imageWidth, int imageHeight, String uiid) {
this(image, imageWidth, imageHeight, false, uiid);
}
/**
* Creates a Button displaying an Image at the given fixed size; at least
* one of imageWidth or imageHeight must be specified.
*
* @param image
* @param imageWidth in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param imageHeight in pixels, can be -1 to automatically resize
* maintaining the aspect ratio
* @param scaledSmallerRatio force the image to maintain the aspect ratio
* within the given dimension (it requires that both imageWidth and
* imageHeight are specified)
* @param uiid
*/
public FixedSizeButton(Image image, int imageWidth, int imageHeight, boolean scaledSmallerRatio, String uiid) {
if (image == null) {
throw new IllegalArgumentException("image cannot be null");
}
if (imageWidth <= 0 && imageHeight <= 0) {
throw new IllegalArgumentException("invalid imageWidth and imageHeight");
}
this.image = image;
setShowEvenIfBlank(true);
if (uiid != null) {
super.setUIID(uiid);
}
if (imageWidth < 1) {
imageWidth = image.getWidth() * imageHeight / image.getHeight();
} else if (imageHeight < 1) {
imageHeight = image.getHeight() * imageWidth / image.getWidth();
}
if (scaledSmallerRatio) {
float hRatio = ((float) imageHeight) / ((float) image.getHeight());
float wRatio = ((float) imageWidth) / ((float) image.getWidth());
if (hRatio < wRatio) {
imageWidth = (int) (image.getWidth() * hRatio);
} else {
imageHeight = (int) (image.getHeight() * wRatio);
}
}
this.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
this.getAllStyles().setBgImage(image);
this.imageWidth = imageWidth;
this.imageHeight = imageHeight;
}
@Override
public Dimension calcPreferredSize() {
int width = imageWidth + this.getStyle().getPaddingLeftNoRTL() + this.getStyle().getPaddingRightNoRTL();
int height = imageHeight + this.getStyle().getPaddingTop() + this.getStyle().getPaddingBottom();
return new Dimension(width, height);
}
/**
* Returns the background image
*
* @return the bg image
*/
@Override
public Image getIcon() {
return image;
}
@Override
public void setText(String text) {
throw new IllegalStateException("Not supported");
}
@Override
public void setUIID(String id) {
super.setUIID(id);
this.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
this.getAllStyles().setBgImage(image);
}
}
性能footprint/overhead 各不相同。当您执行 Image.scaled
(我假设这就是调整大小的意思...)时,图像可能会逐个像素地遍历并缩小为一个新的更小的字节数组。然后它可能会被重新编码为 PNG 或 JPEG。所有这些都是昂贵的任务。
请注意,我使用了 "might" 这个词,因为 iOS 并不总是这样做,并且在某些情况下使用硬件缩放...
当您使用绘图时,图像会作为纹理加载到 draws/scales 图像的 GPU 中。这具有零开销并且相当不错 fast.The 这里最大的缺点是 RAM,它在常规堆和 GPU 内存中都被占用。所以这是你需要做出的权衡。