Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of images in imageview android

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 nowThis is how i want it to look

like image 295
stianboe Avatar asked Sep 13 '13 08:09

stianboe


People also ask

How do I resize an image in ImageView to keep the aspect ratio?

However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="..." . src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView.

What is ImageView in android?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.


1 Answers

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

like image 100
Dimanoid Avatar answered Oct 01 '22 19:10

Dimanoid