我希望将左右边缘模糊



我在stackoverflow上看到了许多答案,并尝试了许多可能的组合,但我仍然无法获得预期的bllure图像,如下所示:边缘边缘

我尝试过的代码(使用帆布,然后叠加机器人图像(

public static Bitmap blur(Context context, Bitmap image) {
    int width = Math.round(image.getWidth());
    int height = Math.round(image.getHeight());
    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(25);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}
public static Bitmap overlayBitmap(Bitmap bitmapBackground, Bitmap bitmapImage) {
    Bitmap resized_bitmap = Bitmap.createScaledBitmap(bitmapImage, (int) (bitmapBackground.getWidth()*0.8), bitmapBackground.getWidth(),true);
    Bitmap bmOverlay = Bitmap.createBitmap(bitmapBackground.getWidth(), bitmapBackground.getHeight(), bitmapBackground.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bitmapBackground, new Matrix(), null);
    canvas.drawBitmap(resized_bitmap,bitmapBackground.getWidth()/5,bitmapBackground.getHeight()/5, null);
    return bmOverlay;
}

没有使用此代码来获得所需的模糊尝试了几种组合,但仍然没有成功。

试图模糊我的图像视图的两面时,我使用了以下代码:

  private void applyBlur(final ImageView image) {
    image.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            image.getViewTreeObserver().removeOnPreDrawListener(this);
            image.buildDrawingCache();
            Bitmap bmp = image.getDrawingCache();
            blur(bmp, image);
            return true;
        }
    });
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void blur(Bitmap mBitmap, View view) {
    long startMs = System.currentTimeMillis();
    float radius = 20;
    Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth()),
            (int) (view.getMeasuredHeight()), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);

    canvas.translate(-view.getLeft(), -view.getTop());
    canvas.drawBitmap(mBitmap, 100, 0,null);
    canvas.drawBitmap(mBitmap, -100, 0,null);
    RenderScript rs = RenderScript.create(getApplicationContext());
    Allocation overlayAlloc = Allocation.createFromBitmap(
            rs, overlay);
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
            rs, overlayAlloc.getElement());
    blur.setInput(overlayAlloc);
    blur.setRadius(radius);
    blur.forEach(overlayAlloc);
    overlayAlloc.copyTo(overlay);
    view.setBackground(new BitmapDrawable(
            getResources(), overlay));
    rs.destroy();
}//end blur

模糊图像视图的两侧而不是左侧

的关键

canvas.drawBitmap中的第二个参数更改为负值

eg 100变为-100

因此,在canvas.drawBitmap(mBitmap, 100, 0,null);之后,您添加canvas.drawBitmap(mBitmap, -100, 0,null);

希望这会有所帮助。

最新更新