尝试使用遮罩裁剪位图会抛出 IllegalArgumentException:
trying to crop bitmap using mask throws IllegalArgumentException:
我正在使用以下代码
public void cropSelection(){
Bitmap bitmap = annotationBitmap.copy(annotationBitmap.getConfig(), true);
Canvas canvas = new Canvas(bitmap);
Paint p = new Paint();
// p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawBitmap(imageBitmap, 0, 0, p); // this line throws error
imageBitmap = bitmap;
}
更令人惊讶的是,当我在 onDraw()
中使用同一行 canvas.drawBitmap(imageBitmap, 0, 0, p)
时,它不会抛出任何错误。效果很好。
堆栈跟踪
2020-01-17 11:20:07.815 21388-21388/com.mayank.picturemagic E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mayank.picturemagic, PID: 21388
java.lang.IllegalArgumentException: Software rendering doesn't support hardware bitmaps
at android.graphics.BaseCanvas.onHwBitmapInSwMode(BaseCanvas.java:550)
at android.graphics.BaseCanvas.throwIfHwBitmapInSwMode(BaseCanvas.java:557)
at android.graphics.BaseCanvas.throwIfCannotDraw(BaseCanvas.java:69)
at android.graphics.BaseCanvas.drawBitmap(BaseCanvas.java:109)
at android.graphics.Canvas.drawBitmap(Canvas.java:1456)
at com.mayank.picturemagic.MainView.cropSelection(MainView.java:434)
at com.mayank.picturemagic.k.onClick(k.java:330)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access00(View.java:785)
at android.view.View$PerformClick.run(View.java:25921)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
终于有了一个漂亮的解决方案
imageBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888, true);
基本上从位图中删除硬件配置。
为了解决这个问题,早些时候我尝试 imageBitmap = imageBitmap.copy(imageBitmap.getConfig(), true);
在这种情况下,配置是硬件,它会抛出错误硬件位图总是不可变的。
但是应该有更有效的解决方案以及问题的一部分 - why this works inside onDraw() but not outside remains
我正在使用以下代码
public void cropSelection(){
Bitmap bitmap = annotationBitmap.copy(annotationBitmap.getConfig(), true);
Canvas canvas = new Canvas(bitmap);
Paint p = new Paint();
// p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawBitmap(imageBitmap, 0, 0, p); // this line throws error
imageBitmap = bitmap;
}
更令人惊讶的是,当我在 onDraw()
中使用同一行 canvas.drawBitmap(imageBitmap, 0, 0, p)
时,它不会抛出任何错误。效果很好。
堆栈跟踪
2020-01-17 11:20:07.815 21388-21388/com.mayank.picturemagic E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mayank.picturemagic, PID: 21388
java.lang.IllegalArgumentException: Software rendering doesn't support hardware bitmaps
at android.graphics.BaseCanvas.onHwBitmapInSwMode(BaseCanvas.java:550)
at android.graphics.BaseCanvas.throwIfHwBitmapInSwMode(BaseCanvas.java:557)
at android.graphics.BaseCanvas.throwIfCannotDraw(BaseCanvas.java:69)
at android.graphics.BaseCanvas.drawBitmap(BaseCanvas.java:109)
at android.graphics.Canvas.drawBitmap(Canvas.java:1456)
at com.mayank.picturemagic.MainView.cropSelection(MainView.java:434)
at com.mayank.picturemagic.k.onClick(k.java:330)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access00(View.java:785)
at android.view.View$PerformClick.run(View.java:25921)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
终于有了一个漂亮的解决方案
imageBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888, true);
基本上从位图中删除硬件配置。
为了解决这个问题,早些时候我尝试 imageBitmap = imageBitmap.copy(imageBitmap.getConfig(), true);
在这种情况下,配置是硬件,它会抛出错误硬件位图总是不可变的。
但是应该有更有效的解决方案以及问题的一部分 - why this works inside onDraw() but not outside remains