Image CodeName One-缩放时图像将像素化

Image CodeName One-缩放时图像将像素化,image,codenameone,Image,Codenameone,我正在从Web服务动态获取图像。之后,我应该使用它作为屏幕上半部分的背景。我的问题是,我只得到了一个大小的图像(这对于许多智能手机来说应该是合适的),但当我调整它的大小以使它占据屏幕的一半时,它会被像素化。 我曾尝试将其作为容器的背景(将填充添加到预期大小),并使用背景\图像\缩放\填充和/或使用图像方法scaledHeight/scaledWidth、填充和缩放。 不管我把它放大还是缩小,它仍然会明显恶化 这是意料之中的事吗?还有其他方法可以缩放图像吗 public class CopyOfV

我正在从Web服务动态获取图像。之后,我应该使用它作为屏幕上半部分的背景。我的问题是,我只得到了一个大小的图像(这对于许多智能手机来说应该是合适的),但当我调整它的大小以使它占据屏幕的一半时,它会被像素化。 我曾尝试将其作为容器的背景(将填充添加到预期大小),并使用背景\图像\缩放\填充和/或使用图像方法scaledHeight/scaledWidth、填充和缩放。 不管我把它放大还是缩小,它仍然会明显恶化

这是意料之中的事吗?还有其他方法可以缩放图像吗

public class CopyOfVistaPantallaPrincipal extends VistaDisparaEventos {

    private Container canvasPantallaPrincipal;

    public CopyOfVistaPantallaPrincipal() {
        canvasPantallaPrincipal = new Container();
        canvasPantallaPrincipal.setName("canvasPantallaPrincipal");
        super.canvas = this.canvasPantallaPrincipal;
        initPantallaPrincipal();
    }

    private void initPantallaPrincipal() {

        canvasPantallaPrincipal.setLayout(new LayeredLayout());
        canvasPantallaPrincipal.getUnselectedStyle().setBgTransparency(0);

        ModeloNovedades modelo = new ModeloNovedades();

        Image imgPrincipal = createImagenPrincipal(modelo);
        canvasPantallaPrincipal.setName("canvas pantalla principal");

        Container otro = new Container(new BorderLayout());
        if (imgPrincipal != null) {
            img.getUnselectedStyle().setBorder(null);
            img.getUnselectedStyle().setPadding(0, 0, 0, 0);
            img.getUnselectedStyle().setMargin(0, 0, 0, 0);
            canvasPantallaPrincipal.addComponent(img);
        }
        canvasPantallaPrincipal.addComponent(otro);
    }

    private Container createImagenPrincipal(ModeloNovedades modelo) {
        return loadTopImage(modelo, modelo.getDestacado());
    }

    private Container loadTopImage(ModeloNovedades modelo, Hashtable destacado) {       
        int width = Display.getInstance().getDisplayWidth();
        int imgHeight = (int) ((Display.getInstance().getDisplayHeight() / 2) * 0.95) ;

        //Default image
        Image foregroundImage = FormFactory.loadImage("/imagenPrincipaLogoOld.png");

        if (! modelo.getDestacado().isEmpty()){
            String destacadaURL = "" + destacado.get("imagen");
            if (! destacadaURL.equals("")){
                //After updating it loads the downloaded image here
                byte[] data = (byte[]) DataCenter.getInstance().get(destacadaURL, false);
                foregroundImage = FormFactory.loadImage(data, 100);
            }
         }

        imageContainer.getAllStyles().setPadding(imgHeight/2, imgHeight/2, width/2, width()/2);
        imageContainer.getAllStyles().setBgImage(foregroundImage);
        imageContainer.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL);

        return imageContainer;
    }
}

如果我不返回图像,而是返回一个以其为背景的容器,我会将其添加到如下容器中:

imageContainer.getAllStyles().setPadding(imgHeight/2, imgHeight/2, width/2, width/2);
imageContainer.getAllStyles().setBgImage(foregroundImage);
imageContainer.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED); //only if it was scaled 
原始图像(请忽略其上方的黑色条,它是最近添加的,我没有保存以前的版本,因为我说过它是动态出现的):

按比例:

裁剪:

上次更新时的图像:

删除所有操作图像、需要缩放、缩放等的代码。。。这都是多余的。你每次操作一个图像都会让它有点变质

您只需更改一行,并且需要始终这样做,这样图像将始终“适合”,而无需进行裁剪或任何其他更改: imageContainer.getAllStyles().setBackgroundType(Style.BACKGROUND\u IMAGE\u SCALE\u TO\u FILL)


请注意,您可以使用
ScaleImageLabel
而不是图像容器,后者的作用大致相同。

缩放会降低图像质量。这是既定的。如果你将提供代码,实际的图像文件和截图,我将能够帮助更多。添加所需的信息。(很抱歉,如果这导致弹出双重通知,我不确定如何工作)我想你是指背景\图像\缩放\填充,因为另一个常量似乎没有定义。在问题中更新了我的代码,这是你的意思吗?如上一张截图所示,它仍然有点错误。从代码中不清楚loadImage是做什么的,也不清楚如何下载图像,其中可能包含一些缩放逻辑。还要确保您的模拟器启用了滚动,以更好地显示图像在设备上的外观,而不必担心模拟器会缩放并包含瑕疵。请注意,如果使用模拟器的屏幕截图功能,滚动标志并不重要