auto resize image on label to fit into screen size

794 views
Skip to first unread message

hera...@gmail.com

unread,
Mar 4, 2013, 5:51:36 AM3/4/13
to codenameone...@googlegroups.com
hello, i create label and adding icon (large size image) into it. i want to use this label as a logo. but the image size is larger then the screen size. how to auto re-size the image and fit it into the screen? in android we can scale an image using dynamic pixel, how i do that in codename one?

thank you

eba...@etceterum.fr

unread,
Mar 4, 2013, 6:14:12 AM3/4/13
to codenameone...@googlegroups.com, hera...@gmail.com
        Image i = ;//This is your image

        Image scaled;
        
        Display display = Display.getInstance();
        if (display.getDisplayWidth() > display.getDisplayHeight()) {
            scaled = i
                    .scaledWidth(display.getDisplayHeight());
        } else {
            scaled = i.scaledWidth(display.getDisplayWidth());
        }

        label.setIcon(scaled);

Shai Almog

unread,
Mar 4, 2013, 8:58:57 AM3/4/13
to codenameone...@googlegroups.com, hera...@gmail.com, eba...@etceterum.fr
I suggest you use:
form.getStyle().setBgImage(...);
form
.getStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);

The reasoning is that this method uses the drawImage method that accepts width/height where appropriate and is thus MUCH faster and more memory efficient than Image.scaled().

Niwo'ogaba Joel

unread,
May 25, 2014, 5:21:12 PM5/25/14
to codenameone...@googlegroups.com, hera...@gmail.com, eba...@etceterum.fr
Hi i was trying to load grid of images from the file system 

        try {
            //Adds the file path of the file system . . .
            final Image icon = Image.createImage(currentFileData.getFilePath());
            if (icon != null) {
                fileName.setPreferredSize(dime);
                fileName.getStyle().setBgImage(icon);
                fileName.getStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
                fileName.repaint();
                //fileName.setIcon(icon);
            }
            //fileSize.setText(FileUtill.fileSizeInMBs(currentFileData.getFilePath()));
        } catch (IOException ex) {
            Log.p("Error loading image . . ");
        }


But it not showing any this and added repaint it still not showing . . .
I need some help ?

Shai Almog

unread,
May 26, 2014, 2:25:41 AM5/26/14
to codenameone...@googlegroups.com, hera...@gmail.com, eba...@etceterum.fr
Hi,
please open a new thread next time instead of reviving an old thread. Use setText(" ") since I'm assuming you are using a label and the labels are hidden by default.
Reply all
Reply to author
Forward
0 new messages