49

我有一个方形图像(尽管这个问题也适用于矩形图像)。我想尽可能大地显示图像,必要时拉伸它们以填充它们的父母,同时仍然保持纵横比。图像小于 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>

我使用了多种 scaleTypes: , , ,的组合fill_parent,它们都以正确的纵横比绘制图像,但它们都没有真正放大图像和 ImageView 本身,导致 TextViews 被全部推送远离屏幕、ImageView 内的空白区域、图像未缩放或图像被裁剪。wrap_contentfitCenterfitStartfitEndcenterInside

我无法完全确定正确的组合。

4

7 回答 7

116

这些:

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

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

于 2012-05-31T02:06:12.450 回答
49

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

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

概括:

  • center

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

在此处输入图像描述

  • centerCrop

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

在此处输入图像描述

  • centerInside

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

在此处输入图像描述

  • fitCenter

在此处输入图像描述

  • fitEnd

在此处输入图像描述

  • fitStart

在此处输入图像描述

  • fitXY

在此处输入图像描述

  • matrix

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

在此处输入图像描述

更多细节在这里新文章

于 2013-12-09T18:13:08.040 回答
20

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

        <ImageView
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/screen"/>
于 2017-09-24T06:27:38.933 回答
13

将此代码与视图布局参数一起用作 wrapcontent

机器人:adjustViewBounds =“真”

希望这会奏效。

于 2012-07-25T10:20:38.623 回答
4

我有同样的问题,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"
于 2015-07-02T01:52:51.227 回答
3

您是否尝试以编程方式对其进行调整?我认为如果您计算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);
}
于 2012-06-05T20:53:30.533 回答
0

Set android:layout_height="wrap_content" in imageview. To create an image with width equals screen width, and height proportionally set according to aspect ratio, do the following. Here I mention how to load image to imageview from url.

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

Hope this helps.

于 2017-07-21T09:29:20.910 回答