首页 文章

为什么我在Codename One模拟器中获得的不同于在真正的Android设备上?

提问于
浏览
2

我想弄清楚为什么我在Android真实设备上使用以下代码在模拟器(iPhone,Nexus,Nexus5,...皮肤)VS中获得不同的行为(我的目标是在背景图像上绘制文本和保存整个背景图像分辨率):

请注意,GUI是使用Designer完成的 .

protected void beforeMain(Form f) {

    // The drawing label will contain the whole photo montage
    f.setLayout(new LayeredLayout());
    final Label drawing = new Label();
    f.addComponent(drawing);

    String nom = "Hello World";

    // Image mutable dans laquelle on va dessiner (fond blancpar défaut)
    // synthe is an Image
    Image mutableImage = Image.createImage(synthe.getWidth(), synthe.getHeight());
    drawing.getUnselectedStyle().setBgImage(mutableImage);
    drawing.getUnselectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);

    // Draws over the background image and put all together on the mutable image.
    paintSyntheOnBackground(mutableImage.getGraphics(), 
            synthe,
            nom,
            synthe.getWidth(), 
            synthe.getHeight());

    long time = new Date().getTime();
    OutputStream os;
    try {
        os = Storage.getInstance().createOutputStream("screenshot_" + Long.toString(time) + ".png");
        ImageIO.getImageIO().save(mutableImage, os, ImageIO.FORMAT_PNG, 1.0f);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

} // end of beforeMain

这是我调用在图像上绘制文本的方法

public void paintSyntheOnBackground(Graphics g, 
        Image synthe,
        final String pNom,
        int width, int height) {

     Font myFont = g.getFont();
        g.setFont(myFont);
        int w = myFont.stringWidth(pNom);
        int h = myFont.getHeight();

        // Added just to see the background
        g.setColor(0x0000FF);
        g.fillRect(0, 0, width, height);

        g.setColor(0xff0000);
        int x = g.getTranslateX() + width / 2 - w / 2;
        int y = g.getTranslateY() + height / 2 - h / 2;

        g.drawRect(x, y, w, h);
        g.drawString(pNom, x, y);

} // end of paintSyntheOnBackground

以下是模拟器(GoogleNexus7)上的结果:
outcome on the simulator (GoogleNexus7)

这是设备上的结果(Android 4.4):
outcome on the device (Android 4.4)

我的开发系统在Linux上使用带有Codename One V3-4的Eclipse .

我知道模拟器无法重现具体的情况,但这里没有什么特别的不是吗?我可以做些什么来使模拟器上的行为反映真实的行为,因为在模拟器中测试会更方便?

编辑:将我的每个CN1项目库从版本114升级到115(参见this question for details on how to do the upgrade)后,我现在能够在模拟器和设备中获得相同的行为!很棒的错误修复工作CN1团队!请注意:在我的情况下(Eclipse - Linux)我必须在 each and every Codename One项目中升级项目库 .

任何帮助将不胜感激!

干杯,

1 回答

  • 1

    这是一个非常烦人的错误,我们just fixed now所以它可以实现今天的发布 .

    只有在模拟器处于比例模式的情况下绘制可变图像时才会出现问题,我们不经常这样做,因为比例模式不太准确且可变图像通常较慢 .

    感谢您跟上这一步 .

相关问题