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

时间:2014-12-15 16:00:18

标签: android bitmap android-view

我想将ScrollView转换为位图,我找到了以下解决方案。

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

除阴影外,它的工作正常,因为布局中有CardView,方法会丢失所有阴影,这会使图像有点奇怪。

那么有没有什么方法可以转换而不会丢失阴影?

0 个答案:

没有答案