为什么Bitmap.createBitmap()返回与源位图不同的可变位图?

bir*_*rdy 2 java android bitmap bitmapfactory

根据文件Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)方法:

从源位图的子集返回不可变位图,由可选矩阵转换.新位图可以是与源相同的对象,也可以是副本.它使用与原始位图相同的密度进行初始化.如果源位图是不可变的并且请求的子集与源位图本身相同,则返回源位图并且不创建新的位图.

我有一个方法,将方向应用于现有的位图:

private Bitmap getOrientedPhoto(Bitmap bitmap, int orientation) {
        int rotate = 0;
        switch (orientation) {
            case ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            default:
                return bitmap;
        }

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix mtx = new Matrix();
        mtx.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }
Run Code Online (Sandbox Code Playgroud)

我是从这里打电话的:

Bitmap tmpPhoto = BitmapFactory.decodeFile(inputPhotoFile.getAbsolutePath(), tmpOpts);
Bitmap orientedPhoto = getOrientedPhoto(tmpPhoto, orientation);
Run Code Online (Sandbox Code Playgroud)

我已经检查过它tmpPhoto是不可变的,但getOrientedPhoto()仍然会返回可变图像,这是tmpPhoto的副本.有没有人知道如何使用Bitmap.createBitmap()而不创建新的位图对象和我的代码有什么问题?

bir*_*rdy 6

看起来它真的只是不清楚这种方法的文档.我在Bitmap crateBitmap()方法中找到了这段代码:

// check if we can just return our argument unchanged
if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
    height == source.getHeight() && (m == null || m.isIdentity())) {
    return source;
}
Run Code Online (Sandbox Code Playgroud)

这意味着只有在源位图不可变且不需要转换的情况下才返回源位图.