0

I created an imageview image, but I want to scale it to be no greater than a fifth of the width and a sixth of the height of the android device's screen size.

Here I get the width and height of the screen:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

here I create my imageview:

    ImageView imageView = new ImageView(this);
    imageView.setImageResource(icons[0]);

and here I try to scale my imageview:

    imageView.getDrawable().setBounds(0, 0, width/5, height/6);

This last part is the stickler. After typing this in I get no errors and my program runs normally - but the image is not scaled. Basically that last line of code seems to have no effect and I have no idea why, any pointers?

Rest of my code:

    imageView.setAdjustViewBounds(true);

    int mh = imageView.getDrawable().getIntrinsicHeight();
    int mw = imageView.getDrawable().getIntrinsicWidth();

    imageView.setX(width/2 - Math.round(width/5));
    imageView.setY(height/2 - Math.round(height/6) - mActionBarSize);

    relativeLayout.addView(imageView);
    setContentView(relativeLayout);
user3488148
  • 59
  • 1
  • 8

2 Answers2

1

do this :

imageView.getLayoutParams().height = (int) Math.round(height/6);
imageView.getLayoutParams().width = (int) Math.round(width/5);
imageView.requestLayout();  // If you're setting the height/width after the layout has already been 'laid out'

see this link to learn more about requestLayout():

Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.

But ususally the view is calling it automatically, you don't have to care about that... (So for me I use it to force the view to do what I want to do)

ahmed_khan_89
  • 2,755
  • 26
  • 49
  • Could you explain that last part? For instance why did you use "image_view" instead of imageView? And what does that last part do exactly? – user3488148 Apr 28 '14 at 23:16
  • i was wrong in using "image_view" , I have just updated in my answer. I added some informations about View.requestLayout() – ahmed_khan_89 Apr 29 '14 at 08:06
0

Use this

 ImageView imgView = (ImageView) findViewById(R.id.pageImage);
    imgView.getLayoutParams().height = 100;
    imgView.getLayoutParams().width = 100;
Santosh Kathait
  • 1,444
  • 1
  • 11
  • 22
  • my program crashes when I try running this, here is my code: imageView.setAdjustViewBounds(true); imageView.getLayoutParams().height = (int) Math.round(height/6); imageView.getLayoutParams().width = (int) Math.round(width/5); imageView.requestLayout(); int mh = imageView.getDrawable().getIntrinsicHeight(); int mw = imageView.getDrawable().getIntrinsicWidth(); imageView.setX(width/2 - mw/2); imageView.setY(height/2 - mh/2 - mActionBarSize); relativeLayout.addView(imageView); setContentView(relativeLayout); – user3488148 Apr 28 '14 at 23:21