调整图像大小以适合imageview

时间:2015-03-25 17:14:28

标签: android bitmap imageview

我正在开发一个应用程序,我需要在Bitview中使用特定尺寸(假设350dpx50dp - height * width)。

我想做类似的事情: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的表面。像第一张图片一样。

1 个答案:

答案 0 :(得分:1)

为什么不在{x}中向android:scaleType="fitXY"添加ImageView

相关问题