I have an ImageView with a dynamic src, and I want to display it, only if there is enough place to show all of it without scaling it.
How should I handle this?
Asked
Active
Viewed 433 times
2 Answers
1
you need to get the image size and screen size then check to see if its larger before displaying
If you want the the display dimensions in pixels you can use
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
If you don't have access to an activity to call getWindowManager on. You can use:
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
For the use case you're describing however a margin/padding in the layout seems more appropriate.
then do
if (ImageView.getDrawable().getIntrinsicHeight() > height || ImageView.getDrawable().getIntrinsicWidth() >width){
//dont display image
}
coppied and compiled from two post Get screen dimensions in pixels and Trying to get the display size of an image in an ImageView

Community
- 1
- 1

owen gerig
- 6,165
- 6
- 52
- 91
-
As I'm insdie getView, the getWidth and getHeight methods, give false values – Tomer Dec 01 '11 at 08:29
-
did u try both methods? for getting the values? can you call view.super either way you will need to post more code then to get further help – owen gerig Dec 01 '11 at 14:20