1

I am trying to get the size of the image displayed on my screen, not the original size of the image.

I already did some research on it and I found the same question on the unresolved post two years old, so I was thinking maybe now there is a new way to do it.

I tried this 3 solutions :

//Get the same result for all my images (whereas images have difference sizes)
imageView.getWidth() + imageView.getHeight()

//Get the original size of the images  
imageView.getDrawable().getIntrinsicWidth() + imageView.getDrawable().getIntrinsicHeight()
bitMap.getWidth() + bitMap.getHeight()

My xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dip" >

<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/descr_image"
android:layout_alignParentBottom="true" />

<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>

Update 1

ViewTreeObserver viewTreeObserver = imageView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
     viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
       @SuppressWarnings("deprecation")
       public void onGlobalLayout() {      
           imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this)               
           System.out.println("TEST :" + imageView.getWidth() + " " + imageView.getHeight());
        }
    });
}
Community
  • 1
  • 1
GermainGum
  • 1,349
  • 3
  • 15
  • 40

1 Answers1

2

On your ImageView you have the width and height set to

android:layout_width="fill_parent"
android:layout_height="fill_parent"

The image view will fill the parent and the getWidth() and getHeight() will give you those values.

You will need to wrap your ImageView somehow to make it display properly but give the ImageView width/height values like

android:layout_width="wrap_content"
android:layout_height="wrap_content"

There is also an answer on ImageView bitmap scale dimensions that seems to solve this problem too.

From the answer by https://stackoverflow.com/users/321697/kcoppock :

ImageView iv = (ImageView)findViewById(R.id.imageview);
int scaledHeight, scaledWidth;
iv.getViewTreeObserver().addOnPreDrawListener(
    new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        Rect rect = iv.getDrawable().getBounds();
        scaledHeight = rect.height();
        scaledWidth = rect.width();
        iv.getViewTreeObserver().removeOnPreDrawListener(this);
        return true;
    }
});
Community
  • 1
  • 1
John Boker
  • 82,559
  • 17
  • 97
  • 130
  • 1
    Thank you it works, I am not sure why but I had to use a ViewTreeObserver otherwise the size was w:1 h:1. But with "wrap_content" my images are displayed in a "ugly way", isn't there possibility to get the size with fill_parent parameter? – GermainGum Dec 26 '12 at 19:20
  • I've added some more code that might help you. see the modified answer. – John Boker Dec 28 '12 at 18:27