Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

drawing - Why do I get a different behviour in Codename One simulator than on a real Android device?

I am trying to figure out why I get a different behaviour in the simulator (iPhone, Nexus, Nexus5, ... skins ) VS on an Android real device with the following code (my goal is to draw a text over a background image and save the whole in background image resolution) :

Please note that the GUI was done with the 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

And here is the method I call to draw a text over an image

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

Here is the outcome on the simulator (GoogleNexus7) : outcome on the simulator (GoogleNexus7)

And here is the outcome on the device (Android 4.4) : outcome on the device (Android 4.4)

My development system features Eclipse on Linux with Codename One V3-4.

I know the simulator cannot reproduce specific case, but here there is nothing special isn't it ? What can I do to make the behaviour on the simulator reflect the real behaviour since it would be much handier to test in the simulator ?

EDIT : After upgrading each of my CN1 project libs from version 114 to 115 (see this question for details on how to do the upgrade), I am now able to get the same behaviour in both simulator and device! Great bug fixing job CN1 team! Please note : In my case (Eclipse - Linux) I had to upgrade the project libs in each and every Codename One project.

Any help would be greatly appreciated!

Cheers,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This was a really annoying bug that we just fixed now so it can make it to today's release.

The problem only occurs when drawing on a mutable image in a case where the simulator is in scale mode both of which we don't do often as scale mode is less accurate and mutable images are generally slower.

Thanks for keeping up with this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...