Size of images in imageview android

17,918

Solution 1

// try this
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true" />

Solution 2

I had the same problem and didnt find another solution except making my own class.

public class Banner extends View {

private final Drawable mDrawable;

public Banner(Context context) {
    this(context, null);
}

public Banner(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public Banner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Banner, 0, 0);
    try {
        mDrawable = a.getDrawable(R.styleable.Banner_image);
    } finally {
        a.recycle();
    }

    setBackgroundDrawable(mDrawable);
}

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = 0;
    int height = 0;
    if(mDrawable != null) {
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = width * mDrawable.getIntrinsicHeight() / mDrawable.getIntrinsicWidth();
    }
    setMeasuredDimension(width, height);
}

}

then add the attributes to some file in /values folder

<declare-styleable name="Banner">
    <attr name="image" format="reference" />
</declare-styleable>

and use this new conrol like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:banner="http://schemas.android.com/apk/res/your.project.name.here"
    android:layout_width="fill_parent"
    android:layout_ height="wrap_content"
    android:orientation="vertical" >

<your.project.name.here.Banner
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"    
    banner:image="@drawable/ic_launcher" />

</RelativeLayout>

it will adjust height proportionally to the image width

Solution 3

Use NinePatch Images

Link Here

Share:
17,918
stianboe
Author by

stianboe

Updated on June 16, 2022

Comments

  • stianboe
    stianboe almost 2 years

    I have an imageView inside a listView. Set up like this: main.xml

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

    image.xml

    <?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="wrap_content"
      android:orientation="vertical" >
    
      <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"    
        android:scaleType="center"    
        android:src="@drawable/ic_launcher" />
    
    </RelativeLayout>
    

    This works great, filling in many images from url with http request. But I have a problem with the size of the images. I want them to fill the screen regardless the resolution or size. Tried with different layout hight, width and scaleType but cant get it to work properly.

    First image is how it looks now, second image is how i want it to look.

    EDIT: tried with scaleType="fitXY" that gave me 100% width, but a bad hight on the longer images.

    This is how it looks now This is how i want it to look

  • stianboe
    stianboe over 10 years
    This gives med the same hight on every image, I want full width, and different height depending on image. Sorry if not clear enough on that in my question.
  • stianboe
    stianboe over 10 years
    Think this is they way to go yes, but cant get it to work properly. I'll try a bit more. Thanks :)