如何在安卓中将图片适应宽度并保持纵横比

3
我有一个具有固定高度和宽度的ImageView(例如,宽度为100dp,高度为50dp)。我需要动态加载图像到这个ImageView中。
我希望这个图像按照一种填充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"
4个回答

4
在你的布局中首先添加:
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代码中,您需要创建一个Bitmap对象,并设置其宽度为所需的宽度(即imageView的宽度),然后将其设置到您的imageView中。但是,您需要将imageView的scaleType设置为matrix,以防止Android自动缩放它!


有没有可能在不将可绘制对象转换为位图的情况下完成这个精确的操作?对于我的使用来说,这样做非常慢。 - fri sa
2
你有任何想象过我已经搜索了多久才找到这个!+1 - instanceof

0

它不行!我已经和它斗争了好几天了。我找到的最接近的比例类型是centerCrop,但我不想要中心特性!!! - fri sa

0
假设您已经拥有了一个名为imageBitmapBitmap图像和一个名为myImageViewImageView。以下是如何调整大小以使其填充整个图像视图并保持纵横比的方法:
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);

谢谢你的回答。我稍微改了一下就可以用了 :) - fri sa

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

希望这可以帮到你。:)


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