仅模糊 模糊图像而不是文本或其他东西

Blur only Blurs Images not Text ore something else

我有一个 BlurActionBarDrawerToggle 可以在我打开导航抽屉时模糊我的背景:

public class BlurActionBarDrawerToggle extends ActionBarDrawerToggle {
    public static int DEFAULT_BLUR_RADIUS = 25;
    public static float DEFAULT_DOWNSCALEFACTOR = 8.0f;
    private Context context = null;
    private DrawerLayout drawerLayout = null;
    private ImageView blurredImageView = null;
    private int blurRadius = DEFAULT_BLUR_RADIUS;
    private float downScaleFactor = DEFAULT_DOWNSCALEFACTOR;
    private boolean prepareToRender = true;
    private boolean isOpening = false;

    public BlurActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
        super(activity, drawerLayout, openDrawerContentDescRes, closeDrawerContentDescRes);

        this.context = activity.getBaseContext();
        this.drawerLayout = drawerLayout;

        this.init();
    }

    public BlurActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
        super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);

        this.context = activity.getBaseContext();
        this.drawerLayout = drawerLayout;

        this.init();
    }

    private void init() {
        this.blurredImageView = new ImageView(context);
        this.blurredImageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        this.blurredImageView.setClickable(false);
        this.blurredImageView.setVisibility(View.GONE);
        this.blurredImageView.setScaleType(ImageView.ScaleType.FIT_XY);

        this.drawerLayout.setScrimColor(this.context.getResources().getColor(android.R.color.transparent));
        this.drawerLayout.post(new Runnable() {
            @Override
            public void run() {
                drawerLayout.addView(blurredImageView, 1);
            }
        });
    }

    @Override
    public void onDrawerSlide(final View drawerView, final float slideOffset) {
        super.onDrawerSlide(drawerView, slideOffset);

        if (slideOffset == 0.f) {
            this.isOpening = false;
        } else {
            this.isOpening = true;
        }

        this.render();
        this.setAlpha(this.blurredImageView, slideOffset, 100);
    }

    @Override
    public void onDrawerClosed(View view) {
        this.prepareToRender = true;
        this.blurredImageView.setVisibility(View.GONE);
    }

    @Override
    public void onDrawerStateChanged(int newState) {
        super.onDrawerStateChanged(newState);

        if (newState == DrawerLayout.STATE_IDLE && !this.isOpening) {
            this.handleRecycle();
        }
    }

    private void render() {
        if (this.prepareToRender) {
            this.prepareToRender = false;

            Bitmap bitmap = loadBitmapFromView(this.drawerLayout);
            bitmap = scaleBitmap(bitmap);
            bitmap = Blur.fastblur(this.context, bitmap, this.blurRadius, false);

            this.blurredImageView.setVisibility(View.VISIBLE);
            this.blurredImageView.setImageBitmap(bitmap);
        }

    }

    public void setRadius(int radius) {
        this.blurRadius = radius < 1 ? 1 : radius;
    }

    public void setDownScaleFactor(float downScaleFactor) {
        this.downScaleFactor = downScaleFactor < 1 ? 1 : downScaleFactor;
    }

    private void setAlpha(View view, float alpha, long durationMillis) {
        final AlphaAnimation animation = new AlphaAnimation(alpha, alpha);

        animation.setDuration(durationMillis);
        animation.setFillAfter(true);

        view.startAnimation(animation);
    }

    private Bitmap loadBitmapFromView(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);

        view.draw(canvas);

        return bitmap;
    }

    private Bitmap scaleBitmap(Bitmap myBitmap) {
        int width = (int) (myBitmap.getWidth() / this.downScaleFactor);
        int height = (int) (myBitmap.getHeight() / this.downScaleFactor);

        return Bitmap.createScaledBitmap(myBitmap, width, height, false);
    }

    private void handleRecycle() {
        Drawable drawable = this.blurredImageView.getDrawable();

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
            Bitmap bitmap = bitmapDrawable.getBitmap();

            if (bitmap != null) {
                bitmap.recycle();
            }

            this.blurredImageView.setImageBitmap(null);
        }

        this.prepareToRender = true;
    }
}

用这个Blur方法:

public class Blur {

    public static Bitmap fastblur(Context context, Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {
        Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

        final RenderScript renderScript = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(renderScript, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);

        final Allocation output = Allocation.createTyped(renderScript, input.getType());
        final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));

        script.setRadius(radius);
        script.setInput(input);
        script.forEach(output);

        output.copyTo(bitmap);

        return bitmap;
    }
}

不幸的是,这只会模糊背景图像等图像的视图。 下图显示了具有图像背景的视图模糊:

第二张图显示了相同的导航抽屉和actionbarddrawertoggle 在没有任何图像的视图上:

如您所见,此处没有模糊的内容。有人能告诉我这里出了什么问题吗?

这实际上是三个因素的组合:

  1. 在模糊之前,图像被缩小(默认为 8 倍)。这意味着非常细的文本被缩小到非常小的尺寸
  2. 模糊然后"disperses"将这几个彩色像素变成更大的区域(系数为25),这意味着模糊文本几乎察觉不到
  3. ImageView 背景是透明的,因此 "real" TextView 在模糊图像后面仍然可见(由于 1 和 2,该区域 大部分透明 ).

最简单的解决方法是为 blurredImageView 设置背景颜色,使用与 activity 的背景颜色相同的值。如果没有其他更改,这意味着其他 UI 元素(文本、行等)将被隐藏。例如,在 render():

this.blurredImageView.setVisibility(View.VISIBLE);
this.blurredImageView.setImageBitmap(bitmap);
this.blurredImageView.setBackgroundColor(Color.RED);  // added

如果你想让它们变得模糊但仍然依稀可见,减小缩小比例和半径,例如:

mDrawerToggle.setDownScaleFactor(2);
mDrawerToggle.setRadius(10);

但是,请注意,这也会减少图像的模糊度(同时会占用更多的位图内存)。