Android ImageView 中图片的尺寸问题

7

我有一个imageView在listView中。设置如下:

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>

这个功能很好,可以通过http请求从url中填充许多图像。但是我有一个关于图像大小的问题。我希望它们可以填满屏幕,而不考虑分辨率或尺寸。尝试过不同的布局高度、宽度和scaleType,但无法使其正常工作。
第一张图片是现在的样子,第二张图片是我想要的样子。
编辑:尝试使用scaleType="fitXY",这给了我100%的宽度,但长图的高度不好看。 This is how it looks now This is how i want it to look

你的问题有任何解决方案吗? - prabhakaran
7个回答

1

我遇到了同样的问题,没有找到其他解决方案,只能自己编写一个类。

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);
}

}

然后将属性添加到/values文件夹中的某个文件中。

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

它会按比例调整高度以适应图像宽度。

我认为这是正确的方法,但是无法正常工作。我会尝试更多。谢谢 :) - stianboe

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

1

0

你已经将它用作:

<?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>

试试这个,我已经做了一些更改:

<?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="200dp"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"    
    android:scaleType="FITXY"    
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

设置imageView的固定大小,然后缩放到FITXY。我希望这个问题可以通过这种方式解决。

这会使每张图片的高度相同,我想要全宽,并根据图片不同而有不同的高度。如果我的问题表述不够清晰,那我很抱歉。 - stianboe

0
根据您的要求,您应该将您的代码放置在这样的位置,
<?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="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"

    android:scaleType="fitXY"    
    android:src="@drawable/ic_launcher" />
</RelativeLayout>

0
在代码中,用android:background="@drawable/ic_launcher"代替android:src="@drawable/ic_launcher",同时在java文件中使用以下代码:
yourImageView.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));

如果我们使用android:src,它会根据其分辨率设置图像以适应屏幕。而如果我们将图像设置为背景,则可以设置所需的widthheight

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接