1

I have an activity that jumps every time i open the phone.

i want it's background to be blurry, so whatever activity should open after mine, would be blurry. But i can't know what activity should be behind,

cause it's not an activity from my app, could be from any app.

So i can't use the method that blurs the last activity i used from here

Android, how to blur/glass/frost current activity

I created a blur builder is stated here

Create blurry transparent background effect

like this

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.view.View;
public class BlurBuilder {
    private static final float BITMAP_SCALE = 0.4f;
    private static final float BLUR_RADIUS = 7.5f;

    public static Bitmap blur(View v) {
        return blur(v.getContext(), getScreenshot(v));
    }

    public static Bitmap blur(Context ctx, 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(ctx);
        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;
    }

    private static Bitmap getScreenshot(View v) {
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }
}

but I'm not sure how to use it on the onCreate of my activity.

Thanks!

Community
  • 1
  • 1
Mumfordwiz
  • 1,483
  • 4
  • 19
  • 31

1 Answers1

1

Use this in your onCreate():

For Fragment:

final Activity activity = getActivity();
final View content = activity.findViewById(android.R.id.content).getRootView();
    if (content.getWidth() > 0) {
        Bitmap image = BlurBuilder.blur(content);
        content.setBackground(new BitmapDrawable(activity.getResources(), image));
}

For Activity:

final View content = findViewById(android.R.id.content).getRootView();
if (content.getWidth() > 0) {
        Bitmap image = BlurBuilder.blur(content);
        content.setBackground(new BitmapDrawable(getResources(), image));
}

EDIT:

Solution for Blur in onCreate(), add this:

new CountDownTimer(1, 1) {
public void onTick(long millisUntilFinished) {
        }

public void onFinish() {
            final View content = findViewById(android.R.id.content).getRootView();
            if (content.getWidth() > 0) {
                Bitmap image = BlurBuilder.blur(content);
                content.setBackground(new BitmapDrawable(getResources(), image));
            }

        }
}.start();
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54