我正在使用以下代码为我的 imageview 构建模糊背景。
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.2f;
private static final float BLUR_RADIUS = 25f;
public static Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
}
Blur 在大多数情况下都构建得很好,但随着时间的推移会变得很糟糕,最终我的应用程序崩溃而没有任何崩溃日志。在模拟器上运行时,经过严格测试,我看到以下错误:
09-01 15:05:03.230 4624-9758/? E/RenderScript: rsAssert failed: ret == bytes || mShutdown, in frameworks/rs/rsFifoSocket.cpp at 83
09-01 15:05:03.238 4624-9781/? E/RenderScript: rsAssert failed: ret == bytes || mShutdown, in frameworks/rs/rsFifoSocket.cpp at 83
09-01 15:05:03.242 4624-10128/? E/RenderScript: rsAssert failed: ret == bytes || mShutdown, in frameworks/rs/rsFifoSocket.cpp at 83
09-01 15:05:03.242 4624-9610/? E/RenderScript: rsAssert failed: ret == bytes || mShutdown, in frameworks/rs/rsFifoSocket.cpp at 83
09-01 15:05:03.242 4624-9781/? E/RenderScript: rsAssert failed: ret == bytes || mShutdown, in frameworks/rs/rsFifoSocket.cpp at 83
09-01 15:05:03.250 4624-9767/? E/RenderScript: rsAssert failed: ret == bytes || mShutdown, in frameworks/rs/rsFifoSocket.cpp at 83
09-01 15:05:03.254 4624-9758/? E/RenderScript: rsAssert failed: ret == bytes || mShutdown, in frameworks/rs/rsFifoSocket.cpp at 83
... (same error - many more times and then crash)
在这方面我有以下疑问:
我正在为渲染脚本使用以下 gradle 配置:
compileSdkVersion 23 buildToolsVersion "23.0.3"
defaultConfig { applicationId "package" minSdkVersion 19 targetSdkVersion 23 multiDexEnabled true renderscriptTargetApi 19 renderscriptSupportModeEnabled true }
推荐的 renderscript 库版本是什么?我应该从android.support.v8.renderscript.Allocation or
android.renderscript` 包中导入依赖项吗?
- 我可以在不使用 renderscript 库的情况下实现模糊效果吗?