Akash Patra
Akash Patra

Reputation: 768

ImageView fills different screen sizes, but maintaing the aspect ratio

I have a rectangular image in an image view. I want to fit the image in the screen for different screen sizes while maintaining the aspect ratio.

In smaller screen its working fine, but its not getting stretched in biggerscreens. Some gap remains in the bottom of the image.

This is my code:

LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
LayoutParams params1 = new LayoutParam(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);


mMainLayout = new LinearLayout(context);
mMainLayout.setLayoutParams(params);


backgroundImage = new ImageView(context);
backgroundImage.setId(0);
backgroundImage.setAdjustViewBounds(true);
backgroundImage.setScaleType(ScaleType.FIT_CENTER);
backgroundImage.setLayoutParams(params1);
backgroundImage.setImageResource(R.drawable.background_circles_en); 

mMainLayout.addView(backgroundImage);

I have used many combinations of fill_parent, wrap_content with multiple scaleTypes: fitCenter, centerInsideand they all draw the images in the right aspect ratio, but none of them actually scale the images up and the ImageView itself, resulting in either the TextViews get pushed all the way down off the screen, blank spaces inside the ImageView, or image not scaled.

Please give a right combination so that it will work properly for different screen sizes.

Upvotes: 0

Views: 763

Answers (1)

jeet
jeet

Reputation: 29199

Privide ScaleType FIT_XY, it will scale image to x and y dimensions, irrespective of aspect ratio.

Upvotes: 2

Related Questions