Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageView adjusting parent's height and fitting width

Update : I solved this issue by using the method described in this answer

I'm a bit stuck with this issue, which I think should be pretty simple.

So my app downloads an image, and renders the bitmap in an ImageView, a child element of a RelativeLayout. I would like the ImageView to fit the parent width, and to adapt it's size to keep the aspect ratio.

Here is my XML :

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/banner" android:layout_width="fill_parent" android:layout_height="wrap_content"></RelativeLayout> <TextView   android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/hello" /> </LinearLayout> 

And the code :

public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);     setContentView(R.layout.main);       RelativeLayout banner = (RelativeLayout) findViewById(R.id.banner);     ImageView imgV = new ImageView(this);      imgV.setScaleType(ImageView.ScaleType.CENTER_CROP);     // I tried all the scale types : CENTER_INSIDE : same effect, FIT_CENTER : same effect...       imgV.setBackgroundColor(0x00FFFF00);      imgV.setAdjustViewBounds(Color.BLUE);       RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);      banner.addView(imgV,params);      // Some code downloading the image stream      bitmap = BitmapFactory.decodeStream(stream);       imgV.setImageBitmap(bitmap);      } 

Desired :

Desired result

Result :

Current result

like image 826
Julien Avatar asked Apr 05 '11 15:04

Julien


People also ask

How can I set image width and height in android?

If you want to just fit the image in image view you can use" wrap content" in height and width property with scale-type but if you want to set manually you have to use LayoutParams. Layoutparams is efficient for setting the layout height and width programmatically.

Which attribute is important for ImageView other than width and height *?

Bookmark this question.

What is adjustViewBounds in android?

android:adjustViewBounds. Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable. android:baseline. The offset of the baseline within this view.

How can you tell the size of a photo on android?

On android go to photos, select your photo and click the ... in the top right. Scroll to bottom of page to find image size.


1 Answers

Thanks to @Julien and @js. Here is complete solution of ImageView that will stretch bitmap height preserving aspect ratio even if bitmap is smaller than ImageView.

public class ResizableImageView extends ImageView {      public ResizableImageView(Context context, AttributeSet attrs) {         super(context, attrs);     }      @Override      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){          Drawable d = getDrawable();           if(d!=null){                  // ceil not round - avoid thin vertical gaps along the left/right edges                  int width = MeasureSpec.getSize(widthMeasureSpec);                  int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());                  setMeasuredDimension(width, height);          }else{                  super.onMeasure(widthMeasureSpec, heightMeasureSpec);          }     }  } 

You can use this class in your xml layouts instead ImageView.

<com.example.ResizableImageView     android:id="@+id/banner"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:src="@drawable/banner" /> 
like image 178
Serafim Suhenky Avatar answered Oct 14 '22 10:10

Serafim Suhenky