2017-09-27 22 views
0

我有以下代码:位图被看作是不可变的()

Bitmap mutableBitmap = result.bitmap.copy(Bitmap.Config.ARGB_8888, true); 
Matrix matrix = new Matrix(); 
matrix.postRotate(-result.rotationDegrees); 
Bitmap rotatedBitmap = Bitmap.createBitmap(mutableBitmap, 0, 0, mutableBitmap.getWidth(), mutableBitmap.getHeight(), matrix, true); 
int[] intValues = new int[INPUT_SIZE*INPUT_SIZE]; 
rotatedBitmap.getPixels(intValues, 0, rotatedBitmap.getWidth(), 0, 0, rotatedBitmap.getWidth(), rotatedBitmap.getHeight()); 

IllegalStateException一个抛出在eraseColor方法与消息不能抹去不可变位图rotatedBitmap.getPixels被调用。显然,mutableBitmaprotatedBitmap都是可变的位图,这就是它们存在的原因,调试器也证实了这一点。为了完整起见,INPUT_SIZE是屏幕宽度,而result是来自FotoApparatPhotoResult对象。

那么为什么会抛出这个异常呢?

回答

2

我觉得你的具体createBitmap方法不创建一个可变的位图:“返回从源位图的子集的不可变位,通过可选的矩阵变换”

https://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap(android.graphics.Bitmap, int, int, int, int, android.graphics.Matrix, boolean)

+0

但是,如果是这样的话: 为什么调试器会告诉我'mIsMutable'对于'rotateBitmap'是真的? 为什么它不工作,或者当我尝试了另一种方式,例如: 'Bitmap rotatedBitmap = Bitmap.createBitmap(result.bitmap,0,0,result.bitmap.getWidth(),result.bitmap.getHeight (),matrix,true);' '位图mut​​ableBitmap = rotatedBitmap.copy(Bitmap.Config.ARGB_8888,true);' – thedjdoorn

相关问题