Creating imageview with auto width and height?

You must Login before you can answer or comment on any questions.

I've created an image view with Ti.UI.createImageView() and i'm loading an image in it. I want the image view to scale to the size of the image. But how do i do this? I've tried the following, but that doesn't work:

var logo = Ti.UI.createImageView({
        backgroundImage: '/images/logo.png',
        width: 'auto',
        height: 'auto'
    }); 
    win.add(logo);
This way nothing is shown. But when i set the width and height hardcoded, then it does display my image.

Is there any way to set the width and height to something like 'auto', so that the imageView automatically has the width and height of the image?

1 Answer

Accepted Answer

Change to:

var logo = Ti.UI.createImageView({
        image: '/images/logo.png',
        width: Ti.UI.SIZE,
        height: Ti.UI.SIZE
    }); 
win.add(logo);

— answered 11 months ago by Eric Whitlock
answer permalink
2 Comments
  • I just tried that, but then i still don't get to see my image... Only if i specify the width height of the image. Like:

    var logo = Ti.UI.createImageView({
            backgroundImage: '/images/logo.png',
            width: '300dp',
            height: '200dp'
        });
    Then it does show up. But i really don't want to specify the width and height like that. Because the image looks weird on devices which are mdpi or ldpi. Those images have different dimensions ofcourse...

    Any other ideas??

    BTW, tried it on an HTC One X and HTC Desizre Z device.

    — commented 11 months ago by david david

  • Oh nevermind, it works. I didn't see you were specifying 'image' and i was using 'backgroundImage'.

    — commented 11 months ago by david david

Your Answer

Think you can help? Login to answer this question!