根据文档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);
}我在这里叫它:
Bitmap tmpPhoto = BitmapFactory.decodeFile(inputPhotoFile.getAbsolutePath(), tmpOpts);
Bitmap orientedPhoto = getOrientedPhoto(tmpPhoto, orientation);我已经检查过tmpPhoto是不可变的,但是getOrientedPhoto()仍然返回可变的图像,这是tmpPhoto的副本。有没有人知道如何在不创建新位图对象的情况下使用Bitmap.createBitmap(),以及我的代码有什么问题?
发布于 2012-09-21 11:27:05
看起来这种方法的文档确实很不清楚。我在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;
}这意味着源位图只有在不可变且不需要转换的情况下才返回。
https://stackoverflow.com/questions/12511117
复制相似问题