如何使图像适合宽度并将纵横比保持在 android

how to fit image to width and keep the aspect ratio in android

我有一个固定高度和宽度的 ImageView(比如宽度为 100dp,高度为 50dp)。我需要将图像动态加载到此图像视图。

我希望此图像以填充 ImageView 宽度的方式缩放,但要保持纵横比(我不在乎是否由于高度限制图像的一部分不可见)。

我想要的与centerCrop非常相似,但我不希望我的图像垂直居中!

提前致谢。

编辑1:

这是我的代码:

这是我的布局。我想使用 background id.

为 imageView 设置图像
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<ImageView
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/footer"
    android:layout_alignBottom="@id/footer"
    />

<RelativeLayout
    android:id="@+id/footer"
    android:layout_width="match_parent"
    android:layout_height="100dp">

</RelativeLayout>

注意 imageView 绑定到第二个 RelativeLayout 的大小:

    android:layout_alignTop="@+id/footer"
    android:layout_alignBottom="@id/footer"

假设您有 Bitmap 图片,即 imageBitmap,和 ImageView,即 myImageView。以下是如何调整它的大小,使其充满图像视图的整个宽度,同时保持纵横比:

float imageOriginalWidthHeightRatio = (float) imageBitmap.getWidth() / (float) imageBitmap.getHeight();

int imageToShowWidth = myImageView.getWidth()
int imageToShowHeight = (int) (imageToShowWidth / imageOriginalWidthHeightRatio);

Bitmap imageToShow = Bitmap.createScaledBitmap(imageBitmap, imageToShowWidth, imageToShowHeight, true);
myImageView.setImageBitmap(imageToShow);

我认为您可以将 ImageView 的高度设置为例如50dp,并尝试将 scaleType 更改为 link:https://developer.android.com/reference/android/widget/ImageView.ScaleType.html 中描述的其中一种。我不记得哪个做什么了。

首先在您的布局中添加

android:scaleType="matrix"

到您的 imageView。

然后在您的 java 代码中添加:

ackground.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.background_image);

            float imageRatio = (float) backgroundBitmap.getWidth() / (float) backgroundBitmap.getHeight();

            int imageViewWidth = background.getWidth();
            int imageRealHeight = (int) (imageViewWidth / imageRatio);

            Bitmap imageToShow = Bitmap.createScaledBitmap(backgroundBitmap, imageViewWidth, imageRealHeight, true);
            background.setImageBitmap(imageToShow);

        }
    });

我会为你分解:

在 java 代码中,您创建了一个具有所需宽度(您的 imageView 宽度)的 Bitmap 对象,并将其设置为您的 imageView。但你需要将你的 imageView scaleType 设置为矩阵以防止 android 自动缩放它!

要创建宽度等于屏幕宽度、高度根据纵横比按比例设置的图像,请执行以下操作。这里我提到如何从 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);
                    }
                });

希望这对您有所帮助。 :)