Kevin Murvie
Kevin Murvie

Reputation: 2652

Android - White space below statusbar in screenshot

I'm using this part of code to get from somewhere around SO :

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);
    /*v.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);*/
    return b;
}

to get a screenshot of view. This is how I get the activity's view : getWindow().getDecorView().getRootView()

Everytime I do that, it gets the whole screen, but with an extra white space of StatusBar's height above Toolbar. I'm at lost here, please ask me about any extra info that I should've provided but haven't.

EDIT : I forgot, I use it in conjunction of blurring :

public static Bitmap blur(Context ctx, Bitmap image) {
    final float BITMAP_SCALE = 0.4f;
    final float BLUR_RADIUS = 7.5f;
    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;
}

And the method called in Activity :

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

Here's the SS of the window

Blank White Space on top

Upvotes: 0

Views: 273

Answers (2)

Yogesh Rathi
Yogesh Rathi

Reputation: 6509

Just Update java code as per below

DisplayMetrics dm = new DisplayMetrics();
        ((Activity) this.getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);

    android.view.ViewGroup.LayoutParams params = layout.getLayoutParams();
    int width = params.width;
    int height = params.height;

    Rect rectangle = new Rect();
    Window window = ((Activity) RatioImageAd.this.getContext()).getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectangle);

    float imageProportion = (float) width / (float) height;
    // Get the width of the screen
    int screenWidth = dm.widthPixels;
    int screenHeight = window.findViewById(Window.ID_ANDROID_CONTENT).getHeight() - window.findViewById(Window.ID_ANDROID_CONTENT).getPaddingTop();
    float screenProportion = (float) screenWidth / (float) screenHeight;

    if (imageProportion > screenProportion) {
        params.width = screenWidth;
        params.height = (int) ((float) screenWidth / imageProportion);
    } else {
        params.width = (int) (imageProportion * (float) screenHeight);
        params.height = screenHeight;
    }
    layout.setLayoutParams(params);
}

and pass layout as view in your function

Upvotes: 1

Kevin Murvie
Kevin Murvie

Reputation: 2652

I got the answer when re-read my question lol, I said it is the same height as StatusBar, yes? And so I remembered somewhere in my Utilities class I have this :

public static int getStatusBarHeight(Context context){
    int result = 0;
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen" , "android");
    if(resourceId > 0){
        result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

So I edit my blur into :

private static Bitmap getScreenshot(View v) {
    /*Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight() - getStatusBarHeight(v.getContext()), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);*/
    v.setDrawingCacheEnabled(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    Bitmap bb = Bitmap.createBitmap(b, 0, getStatusBarHeight(v.getContext()), b.getWidth(), b.getHeight() - getStatusBarHeight(v.getContext()), null, true);
    v.setDrawingCacheEnabled(false);
    return bb;
}

Any idea on improving the code? I created two Bitmaps which is concerning..

Upvotes: 0

Related Questions