【问题标题】:Resizing an image to fit on a imageview调整图像大小以适合图像视图
【发布时间】:2015-03-25 17:14:28
【问题描述】:

我正在开发一个应用程序,我需要在其中将位图适合具有特定尺寸的 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 bitmap imageview


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2013-12-21
    • 1970-01-01
    相关资源
    最近更新 更多