4

I have a row View that I am using in a ListView.

This RowView consists of an ImageView on the left and a TextView on the right in a horizontal LinearLayout where the image is given 40% of the space and the text the remaining 60%.

I want the layout to handle the resizing of the image in the ImageView in the following manner:

  1. Stretch the image horizontally so that it fills up given 40% of the LinearLayout
  2. Resize the ImageView vertically to maintain the original aspect ratio

This is my approach to the layouts:

protected class RowView extends LinearLayout {
public ImageView iv = null;
public TextView tv = null;

private LinearLayout.LayoutParams ivlp = null;
private LinearLayout.LayoutParams tvlp = null;

public RowView(Context ct) {
    super(ct);

    setOrientation(LinearLayout.HORIZONTAL);

    // Create the views
    iv = new ImageView(ct);
    ivlp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.4f);
    iv.setLayoutParams(ivlp);
    iv.setAdjustViewBounds(true);
    iv.setScaleType(ScaleType.FIT_XY);

    tv = new TextView(ct);
    tvlp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.4f);
    tv.setLayoutParams(tvlp);
    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);

    // Add the views
    addView(iv);
    addView(tv);
}

}

The result I am getting is something like this:

enter image description here

From what I understand:

  • The MATCH_PARENT is correctly forcing the image to horizontally stretch to fill the 40% space
  • The ImageView.setAdjustViewBounds(true) is supposed to allow any resizing of the ImageView that is necessary which is what I want. The problem is none of the ImageView.ScaleType's seem to do what I want
  • ImageView.ScaleType(ScaleType.FIT_XY) is not what I want because it doesn't maintain the aspect ratio. But all the others that do, don't seem to stretch the height of the ImageView (which is making ImageView.setAdjustViewBounds(true) pretty much useless).

What am I doing wrong? It seems wrong that something this simple could be so difficult?

Or do I actually have to go to the effort of extending an ImageView and Overriding onMeasure to force the resizing and aspect ratio?

Or is there a clever trick with Matrix to get the job done?

btalb
  • 6,937
  • 11
  • 36
  • 44
  • this response is working for me: http://stackoverflow.com/a/12283909/888245 – OriolJ Oct 09 '13 at 09:26
  • Check the accepted answer: http://stackoverflow.com/questions/18077325/scale-image-to-fill-imageview-width-and-keep-aspect-ratio It worked for me. – StarDust Nov 15 '13 at 02:35

2 Answers2

4

I believe this is what you are looking for:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myImage"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true" />
erkinyldz
  • 678
  • 1
  • 9
  • 18
0

Remove the scale type completely. Also, I think you might have misunderstood what setAdjustViewBounds does. If an image is scaled, the bitmap in the ImageView is scaled, but not the ImageView. Setting setAdjustViewBounds to true will assure that the ImageView's bounds will always match the drawn bitmap's bounds.

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • 1
    I have tried this, but it doesn't achieve what I want. I want it to scale so that it does fill up the entire 40% of the row. Removing `ScaleType` does not scale the image at all which is not what I want. `setAdjustViewBounds` does seem right to me because I want the height bounds of the view to scale up or down as appropriate? – btalb Jan 03 '13 at 09:32