2016-08-04 30 views
2

我从webservice动态获取图像。之后,我应该将它用作屏幕上半部分的背景。我的问题是我只能得到一个尺寸的图像(对于许多智能手机来说应该是大致正确的),但是当我调整它的大小以使其填满屏幕的一半时,它就会像素化。 我试图把它作为一个容器的背景(添加填充到预期的大小)和使用BACKGROUND_IMAGE_SCALED_FILL和/或使用图像方法scaledHeight/scaledWidth,填充和缩放。 如果我将它缩放到更大或更小的尺寸,它仍然会显着恶化,这并不重要。CodeName One - 缩放时图像得到像素化

这是预期的吗?是否有另一种缩放图像的方法?

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 

原图(请忽略它是最近添加的,我没有保存以前的版本,因为我说它是动态的): http://i68.tinypic.com/16lmeqr.jpg

Scaled: http://i68.tinypic.com/w7zafm.jpg

七分裤: http://i65.tinypic.com/2nkp79l.png

图像作为最后更新: http://i68.tinypic.com/2ik7eiq.png

+0

缩放会降低图像质量。这是一个给定的。如果你会提供代码,实际的图像文件和屏幕截图,我将能够提供更多帮助。 –

+0

添加了请求的信息。 (我很抱歉,如果这导致双通知弹出,我不知道如何工作) –

回答

1

删除所有的代码操纵的形象,需要的缩放,缩放等等......这是所有多余的。你在操作图像时所做的每一步都会使图像恶化一点点...

您只需要一次换线,并且需要始终这样做,以便图像始终“适合”,而不会裁剪或进行任何其他更改: imageContainer 。.getAllStyles()setBackgroundType(Style.BACKGROUND_IMAGE_SCALE_TO_FILL);

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

+0

我想你的意思是BACKGROUND_IMAGE_SCALED_FILL作为另一个常数似乎并没有被定义。在问题中更新了我的代码,这是你的意思吗?如上一张截图所示,它仍然有点不对。 –

+0

从代码中不清楚loadImage的作用以及如何下载可能包含一些缩放逻辑的图像。还要确保您的模拟器启用了滚动功能,以更好地表示图像在设备上的显示方式,而模拟器不会缩放并包含工件。请注意,如果您使用模拟器的屏幕截图功能,则滚动标志无关紧要 –