如何拍摄 ImageView 后面区域的屏幕截图/位图/Android 中的任何 UI 组件



假设我有一个ImageView,Grid或任何控件。我想做的是截取此控件背后的内容的屏幕截图,假设另一个包含背景图像或任何可能存在的控件。有什么办法可以做到这一点吗?我想到了getDrawingCache();方法,但这也需要顶部控制。

我想这样做,这样我就可以使顶部控件后面的内容变得模糊透明。我已经有了一种模糊方法,我只需要拍摄背景的确切照片。任何帮助将不胜感激!

public static Bitmap getViewBitmap(View v) {
    v.clearFocus();
    v.setPressed(false);
    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);
    // Reset the drawing cache background color to fully transparent
    // for the duration of this operation
    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);
    if (color != 0) {
        v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    Bitmap cacheBitmap = v.getDrawingCache();
    if (cacheBitmap == null) {
        Log.e(tag, "failed getViewBitmap(" + v + ")", new RuntimeException());
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
    // Restore the view
    v.destroyDrawingCache();
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);
    return bitmap;
}

您可以在任何视图上使用 getDrawingCache() 方法,因此如果您有对背景视图的引用,它应该适用于如下所示的内容:

<your background view>.setDrawingCacheEnabled(true);
Bitmap contentBitmap = Bitmap.createBitmap(<your background view>.getDrawingCache());
<your background view>.setDrawingCacheEnabled(false);

contentBitmap现在应包含该视图中内容的位图。

最新更新