GermainGum
GermainGum

Reputation: 1399

new size of an image in imageView

I would like to know the new size of my image when it is set in an imageview.

I tried this :

System.out.println("Imagesize 1 w:" + imageView.getDrawable().getIntrinsicWidth()+" h "+imageView.getDrawable().getIntrinsicHeight());
System.out.println("Imagesize 2  w:" +loadedImage.getWidth()+ " h " +loadedImage.getHeight());

xml1 of imageView:

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

I have this result

Imagesize 1 w:400 h 577
Imagesize 2 w:400 h 577

xml2 of imageView:

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

Same result:

Imagesize 1 w:400 h 577
Imagesize 2 w:400 h 577

Obviously my image is not displayed in my screen with the same size in case of xml1 and xml2. I guess when I getWidth or getHeight it is the real size of the image, but I would like the size of the image displayed on my screen.

Any ideas?

Update 1:

I forgot to say that loadedImage is the bitmap of my image

Update 2:

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>

Upvotes: 1

Views: 115

Answers (1)

alistair
alistair

Reputation: 1172

To get the size of screen objects after they have been created you need to use a view observer like so:

ViewTreeObserver viewTreeObserver = YOURLAYOUT.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      public void onGlobalLayout() {
         YOURLAYOUT.getViewTreeObserver().removeGlobalOnLayoutListener(this);
         //do some things
         //YOURLAYOUT.getWidth() should give a correct answer
      }
});
}

Upvotes: 1

Related Questions