如何使图像适应宽度并保持android的宽高比

时间:2016-10-22 08:28:20

标签: android android-layout imageview android-imageview

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

我希望这个图像能够以填充ImageView宽度的方式缩放,但是要保持宽高比(如果由于高度限制,部分图像不可见,我不在乎)。

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

提前感谢。

EDIT1:

这是我的代码:

这是我的布局。我想用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 个答案:

答案 0 :(得分:1)

首先在你的布局中添加

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自动缩放它!

答案 1 :(得分:0)

假设您有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);

答案 2 :(得分:0)

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

答案 3 :(得分:0)

要创建宽度等于屏幕宽度的图像,并根据宽高比按比例设置高度,请执行以下操作。在这里,我提到如何从url将图像加载到imageview。

.BeforeMap((s, d) =>
{
    // we are going to check if any child has been moved from 1 parent to another, and
    // if so, move the child before the mapping takes place, this way AutoMapper.Collections will not
    // mark the object as orphaned in the first place!
    foreach (var srcParent in s.Sections)
    {
        // only loop through old measures, so ID will not be zero
        foreach (var srcChild in srcParent.Children.Where(e => e.Id != 0))
        {
            // check if the srcChild is in the same dest parent?
            var destChild = d.Sections.SelectMany(e => e.Children).Where(e => e.Id == srcChild.Id).FirstOrDefault();

            // make sure destination measure exists
            if (destChild != null)
            {
                // does the destination child section id match the source section id? If not, child has been moved
                if (destChild.ParentId != srcParent.Id)
                {
                    // now we need to move the child into the new parent, so lets find the destination
                    // parent that the child should be moved into
                    var oldParent = destChild.Parent;
                    var newParent = d.Sections.Where(e => e.Id == srcParent.Id).FirstOrDefault();

                    // remove child from children collection on oldSection and add to newSection
                    oldParent.Children.Remove(destChild);

                    // if newParent is NULL, it is because this is a NEW section, so we need to add this new section
                    // NOTE: Root is my based level object, so your will be different
                    if (newParent == null)
                    {
                        newParent = new Parent();
                        d.Sections.Add(newParent);
                        newParent.Root = d;
                        newParent.RootId = d.Id;
                    }
                    else
                    {
                        // change references on the child
                        destChild.Parent = newParent;
                        destChild.ParentId = newParent.Id;
                    }

                    newParent.Children.Add(destChild);
                }
            }
        }
    }
})

希望这会有所帮助。 :)