0

我想将png图像作为 LWUIT 表单的背景图像。问题是图像被更改:将其设置为表单的背景图像后,图像中有污点。以下是代码:

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;
    }
    ...
}

我注意到当我从手机内存的照片文件夹中手动打开照片时,照片并没有被改变!

那么如何在将其设置为Form的背景图像时使图像不改变?

4

2 回答 2

1

验证从文件中读取的图像,即sourceImage,是否与在本机电话设备中看到的一样。如果不是这样,问题就在这里。

如果您能够看到sourceImage正确的,那么在获得缩放图像时需要评估一些事情。

  1. 如果您的表单 contentPane 的宽度/高度小于图像宽度/高度,则可以使用更好的选项

    Image thumb = image.scaledSmallerRatio(this.getWidth(), this.getHeight()); 注意:是的,它的 getWidth() 而不是 getPreferredW()。如果您没有得到想要的结果,请尝试使用 getPreferredW() 进行缩放。

  2. 如果您的图像的宽度/高度小于表单 contentPane 的宽度/高度,您最好不要缩放它,因为图像将适合屏幕。

于 2011-11-23T21:10:48.763 回答
1

LWUIT 默认使用缩放背景图像。除非您明确要求不同行为的样式,例如平铺、对齐等,否则它将始终缩放图像。尝试:

myForm.getStyle().setBackgroundType(Style.BACKGROUND_IMAGE_ALIGNED_CENTER);
于 2011-11-24T09:32:01.897 回答