Java 为什么位图有时是相同的对象?
在我的代码中,我做了如下操作:Java 为什么位图有时是相同的对象?,java,android,matrix,bitmap,Java,Android,Matrix,Bitmap,在我的代码中,我做了如下操作: public void doStuff() { Bitmap scaledBitmap = decodeFileAndResize(captureFile); saveResizedAndCompressedBitmap(scaledBitmap); Bitmap rotatedBitmap = convertToRotatedBitmap(scaledBitmap); driverPhoto.setImageBitmap(rot
public void doStuff() {
Bitmap scaledBitmap = decodeFileAndResize(captureFile);
saveResizedAndCompressedBitmap(scaledBitmap);
Bitmap rotatedBitmap = convertToRotatedBitmap(scaledBitmap);
driverPhoto.setImageBitmap(rotatedBitmap);
if (rotatedBitmap != scaledBitmap) {
scaledBitmap.recycle();
scaledBitmap = null;
System.gc();
}
}
private Bitmap convertToRotatedBitmap(Bitmap scaledBitmap) throws IOException {
ExifInterface exifInterface = new ExifInterface(getCaptureFilePath());
int exifOrientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
float orientationDegree = getRotationDegree(exifOrientation);
Matrix rotateMatrix = new Matrix();
rotateMatrix.postRotate(orientationDegree);
return Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), rotateMatrix, true);
}
一切正常,但当我评论if(rotatedBitmap!=scaledbtmap){时,我在使用循环位图时出错
Android是否会在每个位图上创建新位图。createBitmap
调用以及如何避免位图之间的比较?createBitmap(位图源、int x、int y、int width、int height、矩阵m、布尔过滤器)
从源位图的子集返回一个不可变位图,通过可选矩阵进行转换。
Create BitMap method将返回相同的位图方法,如果满足以下所有条件,则返回相同的位图方法
- 如果源位图不可变且
- 像素从0,0 x-y和
- 预期的宽度和高度与原始宽度和高度相同
- 矩阵为空
if (!source.isMutable() && x == 0 && y == 0
&& width == source.getWidth() && height == source.getHeight()
&& (m == null || m.isIdentity())) {
return source;
}
从这里查看BitMap.java的源代码
或
因为您使用的
createBitmap(…)
方法返回一个不可变的位图,所以很可能它实际上是将scaledBimat
存储为一个成员,而不是从头开始创建一个新的位图。实际上,createBitmap(…)中有一个if-子句
返回与给定参数相同的位图。可能您的代码正好符合这种情况,使缩放位图==旋转位图
。我认为您在这里做错了什么。代码行旋转位图!=缩放位图
将始终为真。因为您正在比较两个对象,而它们不是原语。So两个对象不相等。如果我删除这一行,我的工作位图可以循环使用