Blur only Blur

I have BlurActionBarDrawerToggleto blur my background when I open the navigation box:

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;
    }
}

With this method 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;
    }
}

Unfortunately, this is only blurring representations with images such as background images. The following image shows image blur with the background image:

enter image description here

The second figure shows the same navigation box and actionbardrawertoggle in a view without an image:

enter image description here

As you can see, nothing is visible here. Can someone tell me what is going wrong here?

+4
source share
1 answer

This is actually a combination of three factors:

  • , ( 8 ). , , , .
  • "" ( 25), , .
  • ImageView , "" TextView - ( - 1 2).

- blurredImageView, , . - , (, ) . , render():

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

, , , :

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

, ( ).

+4

Source: https://habr.com/ru/post/1570198/


All Articles