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.


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


// 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" />

Use NinePatch Images

Link Here

链接地址: http://www.djcxy.com/p/15850.html

上一篇: Spring Data REST是否支持JPA @Version?

下一篇: imageview android中的图像大小