调整图像大小以适合图像视图

Resizing an image to fit on a imageview

我正在开发一个应用程序,我需要在其中将位图放入具有特定尺寸的 Imageview(假设 350dpx50dp - 高度*宽度)。

我想做类似这样的事情:http://gyazo.com/d739d03684e46411feb58d66acea1002

我在这里寻找解决方案。我找到了这个缩放位图并将其放入imageview的代码,但问题是当我将位图添加到他时imageview变大了:

private void scaleImage(Bitmap bitmap, ImageView view)
{
    // Get current dimensions AND the desired bounding box
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int bounding = dpToPx(350);

    // 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;

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

    // Apply the scaled bitmap
    view.setImageBitmap(scaledBitmap);
}

使用这段代码我可以得到这个:http://gyazo.com/e9871db2130ac33668156fc0cf773594

但这不是我想要的,我想保留imageview的尺寸并将位图添加到imageview中而不修改imageview的尺寸并占据imageview的所有表面。喜欢第一张图。

为什么不直接将 android:scaleType="fitXY" 添加到 xml 中的 ImageView