我尝试使用RenderScript来模糊图像并且它可以正常工作。我想知道RenderScript如何用于模糊部分图像。我试过下面的代码,但它不起作用:
Bitmap overlay = Bitmap.createBitmap(
mWidth,
mHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.drawBitmap(bitmap, -mletf,
-mTop, null);
RenderScript rs = RenderScript.create(mContext);
Allocation overlayAlloc = Allocation.createFromBitmap(
rs, overlay);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
rs, overlayAlloc.getElement());
blur.setInput(overlayAlloc);
blur.setRadius(mRadius);
blur.forEach(overlayAlloc);
overlayAlloc.copyTo(overlay);
rs.destroy();
return overlay;
变量mHeight
,mWidth
是要模糊的部分的高度和宽度,mTop
,mletf
是模糊开始的地方。
答案 0 :(得分:0)
使用LaunchOptions API:
LaunchOptions lo = new LaunchOptions();
lo.setX(mLeft, mLeft+mWidth);
lo.setY(mTop, mTop+mHeight);
blur.forEach(overlayAlloc, lo);
答案 1 :(得分:-1)
使用此库enter link description here
和
defaultConfig {
applicationId "com.javatechig"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
// Add the following two lines
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
}
以下代码段可用于使用RenderScript API在Android中创建位图模糊效果。
//Set the radius of the Blur. Supported range 0 < radius <= 25
private static final float BLUR_RADIUS = 25f;
public Bitmap blur(Bitmap image) {
if (null == image) return null;
Bitmap outputBitmap = Bitmap.createBitmap(image);
final RenderScript renderScript = RenderScript.create(this);
Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);
//Intrinsic Gausian blur filter
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
您可以使用上面的代码段来模糊ImageView,如下所示。
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature);
Bitmap blurredBitmap = blur(bitmap);
imageView.setImageBitmap(blurredBitmap);