Codenameone 代码名一个图像大小调整性能

Codenameone 代码名一个图像大小调整性能,codenameone,Codenameone,我认为,将图像调整到给定尺寸(以像素为单位)以显示所需大小比将该图像用作具有所需首选大小的标签的背景成本更高。你能证实这是真的吗?如果是,为什么是真的 我的意思是,使用下面这样的类(在性能方面)比调整包含图像的大小要好。你认为更快的方法是可能的吗?我需要以固定大小显示大量照片,但我的应用程序不如智能手机的本机gallery应用程序快 /** * Button useful to show the given Image at the given size, without the need t

我认为,将图像调整到给定尺寸(以像素为单位)以显示所需大小比将该图像用作具有所需首选大小的
标签的背景成本更高。你能证实这是真的吗?如果是,为什么是真的

我的意思是,使用下面这样的类(在性能方面)比调整包含图像的大小要好。你认为更快的方法是可能的吗?我需要以固定大小显示大量照片,但我的应用程序不如智能手机的本机gallery应用程序快

/**
 * 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);
    }

}
/**
*按钮用于以给定大小显示给定图像,无需
*调整它的大小。
*
*@作者弗朗西斯科·加尔加尼
*/
公共类FixedSizeButton扩展按钮{
私有最终整数图像宽度;
私人最终成像高度;
私有图像;
/**
*创建以给定固定大小显示图像的按钮;至少
*必须指定imageWidth或imageHeight中的一个。
*
*@param图像
*@param imageWidth(像素),可为-1以自动调整大小
*保持纵横比
*@param imageHeight(像素),可为-1以自动调整大小
*保持纵横比
*/
公共固定大小按钮(图像、int-imageWidth、int-imageHeight){
这(图像、图像宽度、图像高度、空值);
}
/**
*创建以给定固定大小显示图像的按钮;至少
*必须指定imageWidth或imageHeight中的一个。
*
*@param图像
*@param imageWidth(像素),可为-1以自动调整大小
*保持纵横比
*@param imageHeight(像素),可为-1以自动调整大小
*保持纵横比
*@param-uiid
*/
public FixedSizeButton(图像图像、int-imageWidth、int-imageHeight、字符串uiid){
这(图像、图像宽度、图像高度、假、uiid);
}
/**
*创建以给定固定大小显示图像的按钮;至少
*必须指定imageWidth或imageHeight中的一个。
*
*@param图像
*@param imageWidth(像素),可为-1以自动调整大小
*保持纵横比
*@param imageHeight(像素),可为-1以自动调整大小
*保持纵横比
*@param scaledsmalerratio强制图像保持纵横比
*在给定的维度内(要求imageWidth和
*图像高度(已指定)
*@param-uiid
*/
public FixedSizeButton(图像图像、int-imageWidth、int-imageHeight、布尔缩放小勘误表、字符串uiid){
if(image==null){
抛出新的IllegalArgumentException(“图像不能为空”);
}

如果(imageWidth性能占用/开销不同。当您执行
Image.scaled
(我假设这就是您所说的调整大小…)时,图像可能会逐像素遍历并收缩为新的较小字节数组。然后它可能会被重新编码为PNG或JPEG。所有这些都是昂贵的任务

请注意,我使用了“可能”一词,因为iOS并不总是这样做,而且在某些情况下使用硬件扩展

当您使用绘图时,图像作为纹理加载到GPU中,GPU绘制/缩放图像。这没有开销,速度非常快。这里的最大缺点是RAM占用常规堆和GPU内存。因此,您需要进行权衡