7

I'm trying to calculate the dimension for this image, which will be downloaded from the web using this lineimgLoader.DisplayImage(url, R.drawable.thumbnail_background, image);. The problem is that orgHeight becomes zero and you can't divide by zero. But why is this orgHeight 0?

// Add the imageview and calculate its dimensions
        //assuming your layout is in a LinearLayout as its root
        LinearLayout layout = (LinearLayout)findViewById(R.id.layout);

        ImageView image = (ImageView)findViewById(R.id.photo);

        ImageLoader imgLoader = new ImageLoader(getApplicationContext());
        imgLoader.DisplayImage(url, R.drawable.thumbnail_background, image);

        int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
        int orgWidth = image.getWidth();
        int orgHeight = image.getHeight();

        //double check my math, this should be right, though
        int newWidth = (int) Math.floor((orgWidth * newHeight) / orgHeight);

        //Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            newWidth, newHeight);
        image.setLayoutParams(params);
        image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        layout.updateViewLayout(image, params);     

My imageView xml is like this:

<ImageView
            android:id="@+id/photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
        />  
user
  • 86,916
  • 18
  • 197
  • 190
user6827
  • 1,576
  • 5
  • 19
  • 25

2 Answers2

13

The views aren't yet laid in the onCreate() method so their dimensions are 0. Post a Runnable from onCreate() to get the proper values:

image.post(new Runnable() {

 @Override
 public void run() {
    int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
    int orgWidth = image.getWidth();
    int orgHeight = image.getHeight();

    //double check my math, this should be right, though
    int newWidth = (int) Math.floor((orgWidth * newHeight) / orgHeight);

    //Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        newWidth, newHeight);
    image.setLayoutParams(params);
    image.setScaleType(ImageView.ScaleType.CENTER_CROP);
    layout.updateViewLayout(image, params);      
 } 

});
user
  • 86,916
  • 18
  • 197
  • 190
  • Thanks, it works, but now i have one more problem, the height of the picture is not calculated correctly, i have - it looks like - padding on top and bottom. – user6827 Apr 27 '13 at 13:52
  • @user6827 Try to use `ImageView.ScaleType.FIT_XY`. – user Apr 27 '13 at 14:04
  • I see the problem, my `image.getWidth()` and `image.getHeight()` are 1, that is not correct. How could i fix this? – user6827 Apr 27 '13 at 14:17
  • @user6827 You read about `ImageLoader` and see if it exposes a listener for the load complete event. I don't use it. – user Apr 27 '13 at 14:43
  • The key for me to reload layoutParams was layout.updateViewLayout(image, params); Thanks!!! – PaNaVTEC Mar 10 '14 at 10:59
  • That is right in onCreate but actually is in an event (onTouchListener)! The event works when the view is created and drawn on the screen. Now why getLayoutParams couldn't return view height and width on firing an event?! – Mohammad Afrashteh Oct 24 '18 at 09:45
  • @MohammadAfrashteh Please ask a new question and provide more details about what you're trying to do and the actual code you use. – user Oct 24 '18 at 11:22
3

You can also use the ViewTreeObserver to get the values as soon as the layout is done.

Ref - How can you tell when a layout has been drawn?

Community
  • 1
  • 1
Sagar Waghmare
  • 4,702
  • 1
  • 19
  • 20