在 android 中适合位图中的图像

Fit an image within a BitMap in android

我想创建一个具有定义大小的新位图,例如 200x200

Bitmap b = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);

现在我的理解是在该位图中放入一张 SD 卡的图像,但将 SD 图像调整为我创建的位图的大小。

使用 ImageView 做我能做的事:

<ImageView android:src="@drawable/landscape"
    android:adjustViewBounds="false" />

我查找的结果如下:

提前致谢。

编辑:post 我的代码

  public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,  int reqWidth,  int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
//        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

--

imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.paisaje, 200, 200));

outWidthoutHeight in BitmapFactory.Options 不设置结果图像的宽度和高度。它们仅在您更改 inSampleSizeinJustDecodeBounds 设置为 false 时更改,否则它们用于 读取 图像的高度和宽度。

您可以尝试 createScaledBitmap 缩放位图和 returns 新位图。

public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

    return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, resId), reqWidth, reqHeight, true);
}