2

Hi i'm working on an application that will be loading images and i'm looking to scale each image to it's largest possible size for instance if the image is a landscape image (if width is larger than height) i would like to stretch the width to fill the width of the phone and scale height to keep it's aspect ratio. If the height is larger than the width i.e. a portrait image the image should be scaled to fit the height of the phone and width should then adjust to keep the aspect ratio i've had a bit of trouble getting this to work here's what i've done so far though any help would be greatly appreciated.

            final ImageView i = new ImageView(mContext);
            i.setImageResource(mImageIds[position]);
            i.setBackgroundResource(android.R.color.black);
            //TODO need to get actual size of drawable not view size which is 0
            ViewTreeObserver vto = i.getViewTreeObserver();
            vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                public boolean onPreDraw() {
                    int w = i.getMeasuredWidth();
                    int h = i.getMeasuredHeight();
                    Display display = getWindowManager().getDefaultDisplay(); 
                    int width = display.getWidth();
                    int height = display.getHeight();
                    if(w <= h){
                        //TODO need to think about landscape
                        h = height - convertDpToPixel(50, context);
                        w = w*(width);
                    }else{
                        h = h*(height);
                        w = width;
                    }
                    //TODO set imageview to w and h

                    return true;
                }
            });
user577732
  • 3,956
  • 11
  • 55
  • 76
  • When you say you've had trouble to get your code to work, what problem(s) have you had exactly? Exception thrown? Image not re-s-zed as you expected? – D-Dᴙum May 27 '12 at 07:15
  • Ru looking for this http://stackoverflow.com/a/9478320/1012284 – Padma Kumar May 27 '12 at 07:23
  • i tried using bitmap resizedbitmap and what not and was getting oom errors so i was more wonderng how i should set the image view to my w and h – user577732 May 27 '12 at 22:12

2 Answers2

1

Increase either the height or the width to their maximum, depending on which is already greater.

Now, let's say you're working with a landscape image:

You've already expanded the width to it's maximum size, the display's width

The side you expanded is the width of the image, so assuming the display width = width and the image's width = w:

width / w = r

So r would be the ratio between the display width and the image width. Now you need to use this to increase your height.

Simply multiply the height by the ratio, and set the image's new height to the resulting number.

If you end up with a portait image... well, I think you'll be able to figure it out. It's pretty much the same thing.

After all that, I think it would just be a matter of positioning the image in the center of the screen.

Michael
  • 3,334
  • 20
  • 27
1

for get Display width , height

DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int wwidth = displaymetrics.widthPixels;

For Resize Imageview

ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
  • the issue is that i need to know what the images dimensions are so that i can maintain the aspect ratio i don't want to distort it – user577732 May 27 '12 at 22:27