0

I'm having the same problem as this question but the answers there don't solve the problem. I've subclassed ImageView, and overriden onMeasure like so:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    this.setMeasuredDimension(this.getMeasuredHeight(), this.getMeasuredHeight());
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

This doesn't change anything, and if I try using the code from the answers of the linked post, the ImageView becomes invisible (0 width/height) because the heightMeasureSpec is 0, and MeasureSpec.getSize(heightMeasureSpec) is also 0.

The goal is to have the ImageView be as tall as the parent view, and have the width be the same (as the image is square).Screenshot

Edit: here is my ImageView in the layout XML:

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignBottom="@+id/bottomtext"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/toptext"
    android:adjustViewBounds="true"
    android:padding="4dp"
    android:scaleType="centerCrop" />
Community
  • 1
  • 1
cmike
  • 51
  • 6
  • Why are you overriding onMeasure? Can't you just use `layout_height="match_parent"` in XML? – npace Jan 20 '14 at 12:40
  • "match_parent" and "wrap_content" give the same results, which don't fill the height of the list item. I achieve this by using alignTop and alignBottom. – cmike Jan 20 '14 at 13:43

3 Answers3

2

use following field in ImageView

android:scaleType="fitXY"
android:adjustViewBounds="true"
Shyam
  • 6,376
  • 1
  • 24
  • 38
  • Actually this doesn't work. Above I'm using centerCrop scaletype which cuts the image's left and right edges off, and fitXY makes the image width shrink and look distorted. – cmike Jan 20 '14 at 13:02
0

No need to subclass the `ImageView'. Just do this

<ImageView
            android:id="@+id/slider_image"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:scaleType="fitXY" />
ravindra.kamble
  • 1,023
  • 3
  • 11
  • 20
  • I don't want to set a width like this, I want it to match the height – cmike Jan 20 '14 at 13:04
  • I think the issue is that I'm using alignBottom and alignTop. height="match_parent" doesn't seem to work here, same size as using wrap_content. – cmike Jan 20 '14 at 13:41
0

As described here,you can get the height of the layout at runtime. Get it and set it to imageview's width. And the image will become exact square.

Community
  • 1
  • 1
Mandar Kakade
  • 502
  • 4
  • 12