0

I have square images and need them to scale up to fill the view whilst being positioned in the centre.

I've tried all of the XML options and nothing changes - The image always fills the view and stretches when I switch orientation between Portrait and Landscape modes.

I've now implemented a custom ImageView to override the onMeasure method and set the Drawable image dimensions depending on the screen size. But I have the same problem - the image still stretches in both portrait and landscape mode.

Here is the code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.mypackage.BackgroundImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/backgroundImage"/>
    ...

The custom ImageView class:

public class BackgroundImageView extends ImageView {

    public BackgroundImageView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        final Drawable drawable = this.getDrawable();
        if (drawable != null) {
            float imageSideRatio = (float)drawable.getIntrinsicWidth() / (float)drawable.getIntrinsicHeight();
            float viewSideRatio = (float)MeasureSpec.getSize(widthMeasureSpec) / (float)MeasureSpec.getSize(heightMeasureSpec);
            if (imageSideRatio >= viewSideRatio) {
                // Image is wider than the display (ratio)
                int width = MeasureSpec.getSize(widthMeasureSpec);
                int height = (int)(width / imageSideRatio);
                setMeasuredDimension(width, height);
            } else {
                // Image is taller than the display (ratio)
                int height = MeasureSpec.getSize(heightMeasureSpec);
                int width = (int)(height * imageSideRatio);
                setMeasuredDimension(width, height);
            }
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

}

How can I prevent the image from stretching and instead allow it to overflow the imageview (or crop it)?

Ozzy
  • 8,244
  • 7
  • 55
  • 95
  • image view has a property called "scaleType". The centerCrop sounds like exactly what you need. – Gil Moshayof Mar 15 '15 at 15:09
  • see my answer here : http://stackoverflow.com/questions/28517181/measuring-margin-of-a-fitcenter-imageview-in-android/28663666#28663666 – Mahmoud Mar 15 '15 at 15:11
  • @MahmoudShahoud that's the opposite of what I'm trying to do. Look at your state 2: before - that's what I'm trying to do. Make the image larger so that it covers the whole layout. – Ozzy Mar 15 '15 at 16:01
  • use scaleType attribute of image view with value center or center crop : for more details : http://etcodehome.blogspot.com.tr/2011/05/android-imageview-scaletype-samples.html – Mahmoud Mar 16 '15 at 07:30

0 Answers0