创建位图,从视图,使视线中消失,如何让视图印刷品吗? [英] Create bitmap from view makes view disappear, how to get view canvas?

查看:160
本文介绍了创建位图,从视图,使视线中消失,如何让视图印刷品吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现从创建一个视图位图的方法有两种。但是,一旦我做到这一点,认为消失,我不能再使用它。我怎样才能产生重绘我的位图后的看法?

I found two ways of creating a Bitmap from a view. But once I do that, the view disappears and I can't use it anymore. How can I redraw the view after generating my bitmap?

1

public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) 
    bgDrawable.draw(canvas);
else 
    canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}

2日:

Bitmap viewCapture = null;

theViewYouWantToCapture.setDrawingCacheEnabled(true);

viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());

theViewYouWantToCapture.setDrawingCacheEnabled(false);

修改

所以,我想我明白在第一个会发生什么,我们基本上去除它的原始画布视图,并绘制它别的地方与位图有关。我们能以某种方式存储原始的帆布,然后设置视图中有重绘?

So, I think I understand what happens on the first one, we are basically removing the view from it's original canvas and drawing it somewhere else associated with that bitmap. Can somehow we store the original canvas and then set the view to be redrawn there?

推荐答案

对不起,我不是这非常懂行。但我用下面的code:

Sorry, I'm not immensely knowledgeable on this. But I use the following code:

public Bitmap getBitmapFromView(View view, int width, int height) {
    Bitmap returnedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable = view.getBackground();
    if (view==mainPage.boardView) { 
        canvas.drawColor(BoardView.BOARD_BG_COLOR);
    } else if (bgDrawable!=null) { 
        bgDrawable.draw(canvas);
    } else { 
        canvas.drawColor(Color.WHITE);
    }
    view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    view.layout(0, 0, width, height); 
    view.draw(canvas);
    return returnedBitmap;
}

这是如此的相似,你我怀疑我们复制和放大器;从同一个地方进行编辑。

which is so similar to yours I suspect we copy&edited from the same place.

我已经从原来的图纸树消失的观点没有问题。矿呼吁ViewGroups而不是普通视图。

I have no trouble with the view disappearing from the original drawing tree. Mine is called for ViewGroups rather than plain Views.

这篇关于创建位图,从视图,使视线中消失,如何让视图印刷品吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆