2012-12-21 3 views
1

Je voudrais connaître la nouvelle taille de mon image lorsqu'elle est définie dans une vue d'image.nouvelle taille d'image dans imageView

J'ai essayé ceci:

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 de imageView:

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

J'ai ce résultat

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

xml2 de imageView:

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

Même résultat:

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

Il est évident que mon image ne figure pas dans mon écran avec la même taille en cas de xml1 et xml2. Je devine quand je getWidth ou getHeight c'est la taille réelle de l'image, mais je voudrais la taille de l'image affichée sur mon écran.

Des idées?

Mise à jour 1:

j'ai oublié de dire que loadedImage est le bitmap de mon image

Mise à jour 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> 

Répondre

1

Pour obtenir la taille des objets à l'écran après avoir été que vous avez créé besoin d'utiliser un observateur de vue comme ceci:

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 
     } 
}); 
} 
+0

Merci pour votre réponse, j'ai essayé votre solution et j'ai le même résultat w: 536, h: 846 pour toutes mes images différentes .. – GermainGum

+0

Quelle est la taille réelle de l'image? Cela vaut peut-être la peine d'examiner la méthode setAdjustViewBounds (boolean adjustViewBounds) de ImageView. – alistair

+0

J'ai plusieurs images différentes, donc de taille différente, mais l'exemple que j'ai mis dans ce post est: taille réelle: 400 * 577 – GermainGum

Questions connexes