【问题标题】:ImageView fills parent's width OR height, but maintains aspect ratioImageView 填充父母的宽度或高度,但保持纵横比
【发布时间】:2012-06-05 06:52:40
【问题描述】:

我有一个方形图像(虽然这个问题也适用于矩形图像)。我想尽可能大地显示图像,必要时拉伸它们以填充它们的父母,同时仍然保持纵横比。图像小于 ImageView。问题是,我无法拉伸图像并“匹配” ImageView 的高度和宽度。

这是我的 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:padding="10dp">

    <ImageView android:id="@+id/image"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:adjustViewBounds="true"
               android:scaleType="fitCenter"
               android:layout_marginTop="10dp"/>

    <TextView android:id="@+id/name"
              android:layout_below="@id/image"
              android:layout_alignLeft="@id/image"
              android:layout_marginTop="20dp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textSize="18dp"/>

    <TextView android:id="@+id/name2"
              android:layout_below="@id/name"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textSize="14dp"/>


</RelativeLayout>

我使用了fill_parentwrap_content 与多个比例类型的许多组合:fitCenterfitStartfitEndcenterInside,它们都以正确的纵横比绘制图像,但是 它们都没有真正放大图像和 ImageView 本身,导致 TextView 被完全推离屏幕、ImageView 内的空白、图像未缩放或图像被裁剪。 p>

我无法确定正确的组合。

【问题讨论】:

  • 试试这里给出的答案:stackoverflow.com/questions/5355130/…
  • @Tim:我已经设置了adjustViewBounds=true。它以某种方式没有调整 ImageView 边界的大小。
  • 你只有一张还是多张?你有没有想过使用ScrollView?您的图像高度是否大于屏幕高度?如果是这样,您希望您的布局看起来如何?你的TextViewname,应该在图片的左边还是下面?如果您希望所有 3 个视图都适合您的屏幕,它们都应该有 android:layout_height="wrap_content"
  • @slybloty 我有多个,但我只显示一个图像。我应该更好地措辞这个问题。它正在显示一张图像。
  • 你试过android:scaleType="fitXY"吗?它会拉伸图像以匹配您指定的宽度和高度(即,它不考虑纵横比)。

标签: android android-layout android-imageview android-relativelayout


【解决方案1】:

这些:

android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"

应调整图像大小并更改边界大小以适应新图像大小。如果它没有在您的设备上执行此操作,请发布您正在使用的图像以及您正在测试的设备。

【讨论】:

  • 不起作用。我不能发布这些图像,因为它们是专有的,但它们只是标准的 128x128 PNG。这也发生在模拟器上。所以这不是设备特定的问题。这些图像比 ImageView 小。我认为这就是问题所在。
  • adjustViewBounds 是不够的,它“不会增加 ImageView 的大小超出可绘制的自然尺寸”:stackoverflow.com/a/7732684/804479
  • 我认为解决方案是使用 scaleType="centerCrop",如果您可以接受要裁剪的图像...
  • 非常适合我。谢谢!
  • @Foam Guy 如果我从服务器获取图像,这会起作用吗?
【解决方案2】:

使用此代码: android:scaleType="fitXY"

有关图像缩放的更多详细信息,请查看本文here中的动图

总结:

  • center

图像在视图中居中,但不执行缩放

  • centerCrop

均匀缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)都等于或大于视图的相应尺寸(减去填充)。然后图像在视图中居中

  • centerInside

均匀缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)都等于或小于视图的相应尺寸(减去填充)

  • fitCenter

  • fitEnd

  • fitStart

  • fitXY

  • matrix

绘图时使用图像矩阵进行缩放

更多详情here新文章

【讨论】:

  • 为什么这个答案没有任何投票?只有这个对我有用!
  • 因为它不保持源图像的纵横比。见here
  • 用每张图片显示所有案例对我很有帮助。谢谢!
  • 如果 ImageView.background=#000000 你看到 ImageView 占用了父布局的空间;
  • 最佳答案现在我都可以理解了
【解决方案3】:

这个 xml 代码可以工作!如果您指定应用程序的宽度始终与窗口相同,则android:adjustViewBounds="true" 将根据图像的比例设置高度。

        <ImageView
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/screen"/>

【讨论】:

  • 愿stackoverflow上帝保佑你的灵魂。我花了一整天的时间,放弃了窄图像或白色边的图像。今天早上回来并想“嘿,让我用谷歌搜索一下”@garbagecollector 应该真的接受这个答案!
  • @ИвоНедев stackoverflow 上帝并没有以某种方式祝福我,它不起作用。图像纵横比未保持。
【解决方案4】:

将此代码与视图布局参数一起用作包装内容

android:adjustViewBounds="true"

希望它会起作用。

【讨论】:

【解决方案5】:

我遇到了同样的问题,android:adjustViewBounds 没有完成它的工作并且在图像顶部有一个填充。进入具有宽度和高度的 match_parent 值的相对布局,我有:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/transparent">

   <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitStart"
    android:adjustViewBounds="true" ...

我将相对布局更改为线性布局,宽度和高度为 wrap_content 值,现在可以了:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

【讨论】:

  • 经过数小时的搜索后,我还将RelativeLayout 更改为LinearLayout。但在我的情况下,没有显示 ImageView。
  • 我的 ImageView: "" 是在相对布局中,ImageView 会正确处理它的高度,但它的左右两侧有额外的填充。阅读此答案和@CoolMind 的评论后,我尝试将其更改为 LinearLayout 和 poof,ImageView 没有更多的侧面填充。我很想了解这种巫术。
  • @MatanKoby,编码愉快! Android 有许多秘密和地下城。
【解决方案6】:

您是否尝试以编程方式对其进行调整?我认为如果您计算TextViews 的高度并根据此调整图像的高度和宽度,它会非常有效。

private void adjustImageView()
{
    //Get the display dimensions
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    //TextView name
    TextView name = (TextView) findViewById(R.id.name);
    name.setText("your name text goes here");
    name.measure(0, 0);

    //name TextView height
    int nameH = name.getMeasuredHeight();

    //TextView name2
    TextView name2 = (TextView) findViewById(R.id.name2);
    name2.setText("your name2 text goes here");
    name2.measure(0, 0);

    //name2 TextView height
    int name2H = name2.getMeasuredHeight();

    //Original image
    Bitmap imageOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.image);

    //Width/Height ratio of your image
    float imageOriginalWidthHeightRatio = (float) imageOriginal.getWidth() / (float) imageOriginal.getHeight();

    //Calculate the new width and height of the image to display 
    int imageToShowHeight = metrics.heightPixels - nameH - name2H;
    int imageToShowWidth = (int) (imageOriginalWidthHeightRatio * imageToShowHeight);

    //Adjust the image width and height if bigger than screen
    if(imageToShowWidth > metrics.widthPixels)
    {
        imageToShowWidth = metrics.widthPixels;
        imageToShowHeight = (int) (imageToShowWidth / imageOriginalWidthHeightRatio);
    }

    //Create the new image to be shown using the new dimensions 
    Bitmap imageToShow = Bitmap.createScaledBitmap(imageOriginal, imageToShowWidth, imageToShowHeight, true);

    //Show the image in the ImageView
    ImageView image = (ImageView) findViewById(R.id.image);
    image.setImageBitmap(imageToShow);
}

【讨论】:

    【解决方案7】:

    在图像视图中设置android:layout_height="wrap_content"。 要创建宽度等于屏幕宽度、高度根据纵横比按比例设置的图像,请执行以下操作。这里我提到了如何从 url 加载图像到 imageview。

    Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
    
                            // creating the image that maintain aspect ratio with width of image is set to screenwidth.
                            int width = imageView.getMeasuredWidth();
                            int diw = resource.getWidth();
                            if (diw > 0) {
                                int height = 0;
                                height = width * resource.getHeight() / diw;
                                resource = Bitmap.createScaledBitmap(resource, width, height, false);
                            }
                                                  imageView.setImageBitmap(resource);
                        }
                    });
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-08-07
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多