2011-11-23 100 views
0

我想創建一個png圖像作爲LWUIT窗體的背景圖像。問題在於圖像被改變:將圖像設置爲窗體的背景圖像後,圖像中有污點。下面是代碼:當設置爲LWUIT窗體的背景圖像時,PNG圖像染色嚴重

public class DetailPhotoClient extends Form implements ActionListener { 

    private Command options, delete, back, annuler, ok; 
    private GaleriePhotos backForm; 
    private FileConnection fcFile; 
    private Image sourceImage, fullImage; 
    private InputStream is; 
    private PopupMenu popup; 

    public DetailPhotoClient(GaleriePhotos prevForm, String absolutePathphotoName) 
    { 
     super(); 
     back = new Command("Retour"); 
     options = new Command("Options"); 
     this.addCommand(back); 
     this.addCommand(options); 
     this.addCommandListener(this); 

     delete = new Command("Supprimer"); 
     annuler = new Command("Annuler"); 
     ok = new Command("Ok"); 

     backForm = prevForm; 

     try { 
      fcFile = (FileConnection) Connector.open(absolutePathphotoName, Connector.READ); 
      is = fcFile.openInputStream(); 
      sourceImage = Image.createImage(is); 
      fullImage = createThumbnail(sourceImage); 
      setBgImage(fullImage); 
      is.close(); 
      fcFile.close(); 
     } catch (IOException ex) { 
      handleException(); 
     } catch (OutOfMemoryError oom) { 
      handleOOM(); 
     } 
    } 
    private Image createThumbnail(Image image) { 
     Image thumb = image.scaled(this.getPreferredW(), this.getPreferredH()); 
     return thumb; 
    } 
    ... 
} 

我注意到,當我手動打開照片,就是從手機存儲的照片文件夾,然後將照片不會被改變!

那麼如何在設置窗體背景圖像時不改變圖像呢?

回答

1

默認情況下,LWUIT對背景圖像使用縮放。除非您明確要求樣式表達不同的行爲,否則它總是會縮放圖像。瓦片,對齊等嘗試:

myForm.getStyle().setBackgroundType(Style.BACKGROUND_IMAGE_ALIGNED_CENTER); 
1

驗證從文件讀取的圖像,即sourceImage是否如本地電話設備中所見。如果不是這樣,問題就在這裏。

如果您能夠看到sourceImage正確,那麼在獲取縮放圖像時需要評估一些內容。

  1. 如果您形式的contentPane的寬度/高度比圖像更小的寬度/高度比一個更好的辦法是使用

    Image thumb = image.scaledSmallerRatio(this.getWidth(), this.getHeight()); 注:是的,它的getWidth(),而不是getPreferredW( )。如果你沒有得到想要的結果,則嘗試使用getPreferredW()進行縮放。

  2. 如果圖片的寬度/高度小於表格的contentPane的寬度/高度,您可能不會縮放它,因爲圖片會適合屏幕。

+0

圖像總是改變,即使我用'scaledSmallerRatio(this.getWidth(),this.getHeight());' – pheromix

相關問題