【问题标题】:Why do I get a different behviour in Codename One simulator than on a real Android device?为什么我在 Codename One 模拟器中的行为与在真正的 Android 设备上的行为不同?
【发布时间】:2016-10-25 11:58:10
【问题描述】:

我试图弄清楚为什么我在模拟器(iPhone、Nexus、Nexus5、... skins)VS 上使用以下代码在 Android 真实设备上得到不同的行为(我的目标是在背景图像并以背景图像分辨率保存整体):

请注意,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) 上的结果:

这是设备上的结果(Android 4.4):

我的开发系统在 Linux 上使用代号 One V3-4 的 Eclipse。

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

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

任何帮助将不胜感激!

干杯,

【问题讨论】:

    标签: drawing codenameone simulator


    【解决方案1】:

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

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

    感谢您跟上这一点。

    【讨论】:

    • 你真的很摇滚代号一队!您无法想象修复此错误后您让我的生活变得多么轻松!我更新到版本 115,它现在按预期工作。非常感谢@Shai!
    • 感谢您关注这个问题 ;-)
    • 感谢您的修复。我遇到了同样的问题。我只是选择忽略它,因为它在实际设备上运行良好,但修复它肯定会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2013-03-26
    • 1970-01-01
    • 2016-07-21
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多