如何在 Android L 中将视图转换为带阴影的位图

Gho*_*ing 5 android bitmap android-view

我想将 a 转换ScrollView为 Bitmap,我在下面找到了解决方案。

private Bitmap getBitmapFromView(View v) {
    // store the origin state
    int originWidth = v.getMeasuredWidth();
    int originHeight = v.getMeasuredHeight();
    // capture the view
    int specWidth = View.MeasureSpec.makeMeasureSpec(originWidth, View.MeasureSpec.EXACTLY);
    int specHeight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    v.measure(specWidth, specHeight);
    Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    Canvas c = new Canvas(b);
    c.drawColor(getResources().getColor(R.color.default_background));
    v.draw(c);

    // restore the view
    v.measure(originWidth, originHeight);
    v.layout(0, 0, originWidth, originHeight);
    return b;
}
Run Code Online (Sandbox Code Playgroud)

除了阴影效果很好,因为CardView布局中有,该方法丢失了所有阴影,使图像有点奇怪。

那么有没有什么方法可以在不丢失阴影的情况下进行转换?