0

Here is my code

onCreate()
{
    img = (ImageView)findViewById(R.id.img);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int w = size.x;
    int h = w * (9/16);
    img.getLayoutParams().height = h;
    img.getLayoutParams().width = w;
    img.requestLayout();

    //The rest of the code spawns a new thread and downloads an image for the image view.
}

and yeah I tried using the FrameLayout Params too (as my ImageView is in a FrameLayout), something like this img.setLayoutParams(new FrameLayout.LayoutParams(w,h)), didn't work

Well what Im trying to do is irrespective of the Image/Image SIZE being downloaded, I want to create an ImageView with w:h = 16:9 ratio. What is the problem with my logic? I tried doing an img.requestLayout() too, didn't work, moved the code to onAttachWindow(), didn't work. Whats the problem here? Would really appreciate any help. Thanks.

Cœur
  • 37,241
  • 25
  • 195
  • 267
uLYsseus
  • 995
  • 6
  • 16
  • 37

1 Answers1

1

It seems the only problem with your code is this line:

 int h = w * (9/16);

The (9/16) is being performed as integer division, so it's resulting in 0. Change it to:

 int h = (int)(w * 9f / 16f);
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • 1
    Ohhhhh shooot ! Why didnt I think of the int thing ! I will try this and let you know and I wonder whats wrong with the ImageView params in my code and why isnt that right. Really appreciate your help..! – uLYsseus Jul 16 '14 at 15:25
  • yeah this works, but when I use Picasso to download the image, the imageview disappaears ....for example when doing something like this Picasso.with(this).load(URL).fit().into(img, new Callback(){ Override public void onSuccess() { } Override public void onError() { //display error } }); – uLYsseus Jul 16 '14 at 21:33
  • I got it right, but Picasso is not helping me out, so an AsyncTask finally helped me ....want to find out the reason though.... – uLYsseus Jul 16 '14 at 21:59
  • Damn Again Bro ! You know what Picasso actually seems to handle all kinds of security exceptions and prevent the app from Nasty crashes ! So in my case I didnt add the internet permission and VOILA ! It works...simple... and easy peasy ! But I have one more question for you..whats wronf with my code other than int part? – uLYsseus Jul 16 '14 at 22:38