将图像调整到ImageView中,保持纵横比,然后将ImageView调整为图像尺寸?

jul*_*jul 153 android scale imageview

如何将随机大小的图像拟合到ImageView
什么时候:

  • 最初ImageView尺寸为250dp*250dp
  • 图像的较大尺寸应按比例放大/缩小至250dp
  • 图像应保持其纵横比
  • ImageView缩放后,尺寸应与缩放图像的尺寸相匹配

例如,对于100*150的图像,图像ImageView应为166*250.
例如,对于150*100的图像,图像ImageView应为250*166.

如果我将界限设置为

<ImageView
    android:id="@+id/picture"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:adjustViewBounds="true" />
Run Code Online (Sandbox Code Playgroud)

图像适合在ImageView,但ImageView总是250dp*250dp.

Sar*_*sch 222

对于这个具体问题可能无法回答,但是如果有人像我一样,在maxWidth保留纵横比的同时搜索如何在ImageView中使用有界大小(例如)来匹配图像,然后摆脱ImageView占用的过多空间,那么最简单的解决方案是在XML中使用以下属性:

    android:scaleType="centerInside"
    android:adjustViewBounds="true"
Run Code Online (Sandbox Code Playgroud)

  • 如果您不希望图像放大,如果图像太小,则此方法有效. (13认同)
  • 要放大小图像,请使用scaleType =“centerCrop”。 (2认同)

Jar*_*der 133

(在澄清原始问题后,答案被大量修改)

澄清之后:
不能仅在xml中完成.无法对图像进行缩放,ImageView因此图像的一维尺寸始终为250dp,并且ImageView尺寸与图像的尺寸相同.

这个代码缩放 Drawable为一个ImageView像250dp x 250dp的正方形,一维正好250dp并保持纵横比.然后ImageView调整大小以匹配缩放图像的尺寸.代码用于活动.我通过按钮点击处理程序测试了它.

请享用.:)

private void scaleImage(ImageView view) throws NoSuchElementException  {
    // Get bitmap from the the ImageView.
    Bitmap bitmap = null;

    try {
        Drawable drawing = view.getDrawable();
        bitmap = ((BitmapDrawable) drawing).getBitmap();
    } catch (NullPointerException e) {
        throw new NoSuchElementException("No drawable on given view");
    } catch (ClassCastException e) {
        // Check bitmap is Ion drawable
        bitmap = Ion.with(view).getBitmap();
    }

    // Get current dimensions AND the desired bounding box
    int width = 0;

    try {
        width = bitmap.getWidth();
    } catch (NullPointerException e) {
        throw new NoSuchElementException("Can't find bitmap on given view/drawable");
    }

    int height = bitmap.getHeight();
    int bounding = dpToPx(250);
    Log.i("Test", "original width = " + Integer.toString(width));
    Log.i("Test", "original height = " + Integer.toString(height));
    Log.i("Test", "bounding = " + Integer.toString(bounding));

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.  
    float xScale = ((float) bounding) / width;
    float yScale = ((float) bounding) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;
    Log.i("Test", "xScale = " + Float.toString(xScale));
    Log.i("Test", "yScale = " + Float.toString(yScale));
    Log.i("Test", "scale = " + Float.toString(scale));

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the ImageView 
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    width = scaledBitmap.getWidth(); // re-use
    height = scaledBitmap.getHeight(); // re-use
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    Log.i("Test", "scaled width = " + Integer.toString(width));
    Log.i("Test", "scaled height = " + Integer.toString(height));

    // Apply the scaled bitmap
    view.setImageDrawable(result);

    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);

    Log.i("Test", "done");
}

private int dpToPx(int dp) {
    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);
}
Run Code Online (Sandbox Code Playgroud)

xml代码ImageView:

<ImageView a:id="@+id/image_box"
    a:background="#ff0000"
    a:src="@drawable/star"
    a:layout_width="wrap_content"
    a:layout_height="wrap_content"
    a:layout_marginTop="20dp"
    a:layout_gravity="center_horizontal"/>
Run Code Online (Sandbox Code Playgroud)


感谢有关扩展代码的讨论:http:
//www.anddev.org/resize_and_rotate_image_-_example-t621.html


2012年11月7日更新:
添加空指针检查,如评论中所示

  • 没意识到你可以替换android:用: (79认同)
  • 好。那不能仅在xml中完成。需要Java代码。使用xml既可以缩放图像,也可以缩放“ ImageView”,不能同时缩放两者。 (2认同)
  • Ion是异步网络和图像加载的框架:https://github.com/koush/ion (2认同)

小智 41

<ImageView android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="centerCrop"
           android:adjustViewBounds="true"/>
Run Code Online (Sandbox Code Playgroud)


Nar*_*rma 22

下面的代码使位图与imageview的大小完全相同.获取位图图像的高度和宽度,然后借助imageview的参数计算新的高度和宽度.这为您提供了最佳纵横比所需的图像.

int currentBitmapWidth = bitMap.getWidth();
int currentBitmapHeight = bitMap.getHeight();

int ivWidth = imageView.getWidth();
int ivHeight = imageView.getHeight();
int newWidth = ivWidth;

newHeight = (int) Math.floor((double) currentBitmapHeight *( (double) new_width / (double) currentBitmapWidth));

Bitmap newbitMap = Bitmap.createScaledBitmap(bitMap, newWidth, newHeight, true);

imageView.setImageBitmap(newbitMap)
Run Code Online (Sandbox Code Playgroud)

请享用.

  • 这只会将原始高度减小宽度减小的相同因子.这不会保证newHeight <ivHeight.理想情况下,你应该检查哪个比率更大(currentBitmapHeight/ivHeight,currentBitmapWidth/ivWidth),然后在此基础上做出进一步的决定. (3认同)

Dee*_*our 13

尝试添加android:scaleType="fitXY"到您的ImageView.

  • 如果原始图像不平方,这将修改宽高比. (4认同)

Mik*_*eyB 7

搜索一天后,我认为这是最简单的解决方案:

imageView.getLayoutParams().width = 250;
imageView.getLayoutParams().height = 250;
imageView.setAdjustViewBounds(true);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的好答案,但我认为最好将`adjustViewBounds`添加到XML (2认同)

Nik*_*rma 6

在大多数情况下有效的最佳解决方案是

这是一个例子:

<ImageView android:id="@+id/avatar"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:scaleType="fitXY"/>
Run Code Online (Sandbox Code Playgroud)


Har*_*ngh 6

如果它不适合你,那么用 android:src 替换 android:background

android:src 将发挥主要作用

    <ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:src="@drawable/bg_hc" />
Run Code Online (Sandbox Code Playgroud)

它像魅力一样工作正常

在此处输入图片说明


Vol*_*lyk 5

已编辑的Jarno Argillanders回答:

如何使图像适合您的宽度和高度:

1) 初始化 ImageView 并设置图像:

iv = (ImageView) findViewById(R.id.iv_image);
iv.setImageBitmap(image);
Run Code Online (Sandbox Code Playgroud)

2)现在调整大小:

scaleImage(iv);
Run Code Online (Sandbox Code Playgroud)

编辑scaleImage方法:(您可以替换 EXPECTED 边界值

private void scaleImage(ImageView view) {
    Drawable drawing = view.getDrawable();
    if (drawing == null) {
        return;
    }
    Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int xBounding = ((View) view.getParent()).getWidth();//EXPECTED WIDTH
    int yBounding = ((View) view.getParent()).getHeight();//EXPECTED HEIGHT

    float xScale = ((float) xBounding) / width;
    float yScale = ((float) yBounding) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(xScale, yScale);

    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();
    BitmapDrawable result = new BitmapDrawable(context.getResources(), scaledBitmap);

    view.setImageDrawable(result);

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}
Run Code Online (Sandbox Code Playgroud)

和.xml:

<ImageView
    android:id="@+id/iv_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" />
Run Code Online (Sandbox Code Playgroud)


Thu*_*ick 5

所有这些都可以使用XML完成...其他方法似乎非常复杂。无论如何,您只需将高度设置为dp中所需的高度,然后设置宽度即可包装内容,反之亦然。使用scaleType fitCenter调整图像的大小。

<ImageView
    android:layout_height="200dp"
    android:layout_width="wrap_content"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:src="@mipmap/ic_launcher"
    android:layout_below="@+id/title"
    android:layout_margin="5dip"
    android:id="@+id/imageView1">
Run Code Online (Sandbox Code Playgroud)