安卓ImageView:适应宽度

88

我使用ImageView动态下载图片并将其设置为屏幕背景。我尝试了ScaleType来缩放图片。

如果图片的高度大于宽度,那么ScaleTypesfitStartfitEndfitCenter都不起作用。Android会缩小照片,并根据高度来适应照片,但是我看到一些额外的空白空间作为宽度的一部分。

我想根据宽度缩小照片,使其适应宽度。我不在意高度部分是否有额外的空白空间或者如果高度太长时是否超出视图(如果可能的话?)。

ScaleType.XY会缩放照片并适应ImageView中的所有内容,而不关心图片的高度/宽度比。

<ImageView 
                android:id="@+id/background"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitStart"
            />
7个回答

211

我最终使用了这段代码:

<ImageView 
                android:id="@+id/background"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/name"
            />

请确保使用 src 而不是 background 来设置图像。


2
这并不是成比例的,它只是裁剪图像以适应...我该如何修复这个问题? - Ruchir Baronia
哦,好的。谢谢回复。那么在屏幕上按比例适配图像是不可能的吗? - Ruchir Baronia
你有什么建议吗?我想在我的应用程序中放置一张全屏图片,但是图片显示时被拉伸了。有没有什么方法可以解决这个问题?非常感谢! - Ruchir Baronia
3
据我所知,应该是使用“fitCenter”而不是“centerFit”。 - Jacksonkr
@sharj。谢谢,它帮助了我。 - Vision Coderz
显示剩余3条评论

84

android:adjustViewBounds="true"可以实现此功能!


1
最佳答案: - Md Manna

14

这个优雅的解决方案可以在这里找到,它会对你非常有用。

基本上,你只需要创建一个小类扩展ImageView并简单地覆盖onMeasure方法以调整所需的宽度和高度。这里使用缩放来适应宽度:

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

你可以像下面这样使用这个特殊的ImageView

<your.activity.package.AspectRatioImageView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_vertical"
    android:src="@drawable/test" />

我有一个包含动态下载图片的ListView。因此,适配器返回一个布局,其中ImageView的大小可以动态改变。这是唯一对我有效的方法。谢谢。 - Dennis K
这并不优雅,这相当糟糕。看看被接受的答案。 - mpellegr

9

如果您只想填充屏幕宽度并保持高度成比例(且知道图像的比例),则可以在OnCreate()中使用以下简单方法:

setContentView(R.layout.truppview_activity);
trupImage = (ImageView) findViewById(R.id.trupImage);

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float scWidth = outMetrics.widthPixels;
trupImage.getLayoutParams().width = (int) scWidth;
trupImage.getLayoutParams().height = (int) (scWidth * 0.6f);

其中0.6是比例调整值(当然,如果您知道图像的w和h,也可以自动计算)。

附:
这是XML侧面:

  <ImageView
        android:id="@+id/trupImage"
        android:layout_width="match_parent"
        android:background="@color/orange"
        android:layout_height="match_parent" 
        android:adjustViewBounds="true" 
        android:scaleType="fitCenter" />

8

如果您想让图片适应整个父级块, 它将会拉伸图片。

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" 

如果您想将图像与其原始比例一起适合整个父级容器,它会裁切掉图像的某些部分。

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"

6

这对我有用。

创建一个继承自ImageView的类。

    package com.yourdomain.utils;

    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.widget.ImageView;

    public class ResizableImageView extends ImageView {

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

        @Override 
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Drawable d = getDrawable();

            if (d != null) {
                int width = MeasureSpec.getSize(widthMeasureSpec);
                int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
                setMeasuredDimension(width, height);
            } else {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }
    }

在xml中使用以下内容代替ImageView

    <com.yourdomain.utils.ResizableImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/test" />

1

我认为你不能仅使用XML来完成它,你需要自己调整位图大小。

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();

        try {
            ((ImageView) findViewById(R.id.background))
                    .setImageBitmap(ShrinkBitmap(width));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

那么

private Bitmap ShrinkBitmap(int width)
        throws FileNotFoundException {

    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.img, bmpFactoryOptions);
    int widthRatio = (int) android.util.FloatMath
            .ceil(bmpFactoryOptions.outWidth / (float) width);


    bmpFactoryOptions.inSampleSize = widthRatio;



    if (bmpFactoryOptions.inSampleSize <= 0)
        bmpFactoryOptions.inSampleSize = 0;
    bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    bmpFactoryOptions.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.img, bmpFactoryOptions);
    return bitmap;

}

还有布局

 <ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
  />

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