Android - 屏幕截图

时间:2016-08-29 03:25:52

标签: android screenshot

我正在使用这部分代码从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;
}

获取视图的屏幕截图。这就是我获取活动视图的方式: getWindow().getDecorView().getRootView()

每当我这样做时,它会获得整个屏幕,但StatusBar的高度为Toolbar以上的额外空白区域。我迷失在这里,请问我有关我应该提供的任何额外信息,但没有。

编辑: 我忘记了,我将它与模糊结合使用:

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

Activity中调用的方法:

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

这是窗口的SS

Blank White Space on top

2 个答案:

答案 0 :(得分:1)

只需按照以下内容更新java代码

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

并将layout作为您的函数中的视图

答案 1 :(得分:0)

我在重新阅读我的问题时得到了答案大声笑,我说它与StatusBar的高度相同,是吗?所以我记得在Utilities课程的某个地方我有这个:

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

所以我将模糊编辑成:

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

有关改进代码的想法吗?我创建了两个有关的Bitmaps ..