转换期没有在Android中显示它为位图? [英] Converting a view to Bitmap without displaying it in Android?

查看:143
本文介绍了转换期没有在Android中显示它为位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽量解释究竟是什么,我需要做的。

I will try to explain what exactly I need to do.

我有3个独立的屏幕说A,B,C。有一个叫说,主屏幕的另一个屏幕,所有的3屏位图应显示在库视图,用户可以选择在认为确实他想走。

I have 3 separate screens say A,B,C. There is another screen called say HomeScreen where all the 3 screens bitmap should be displayed in Gallery view and the user can select in which view does he wants to go.

我已经能够得到所有的3个屏幕的位图,并把所有的code仅在主屏幕上的活动在库视图中显示。现在,这已经复杂的codeA很多,我会想简化它。

I have been able to get the Bitmaps of all the 3 screens and display it in Gallery view by placing all the code in HomeScreen Activity only. Now, this has complicated the code a lot and I will like to simplify it.

所以,我可以打电话从主屏幕的另一个活动,并且不显示它和刚刚获得该屏幕的位图。例如,说我只需要调用的主屏幕,并调用活动A,B,C和没有活动从A,B,C都显示。它只是给该屏幕由getDrawingCache位图()。然后我们就可以在主屏幕上显示的画廊鉴于这些位图。

So, can I call another Activity from HomeScreen and do not display it and just get the Bitmap of that screen. For example, say I just call HomeScreen and it calls Activity A,B,C and none of the Activities from A,B,C are displayed. It just gives the Bitmap of that screen by getDrawingCache(). And then we can display those bitmaps in Gallery view in HomeScreen.

我希望我已经说明了问题很清楚。

I hope I have explained the problem very clearly.

请让我知道,如果这实际上是可能的。

Please let me know if this is actually possible.

推荐答案

还有一个办法做到这一点。你必须创建一个位图和一个画布,并调用view.draw(画布);

there is a way to do this. you have to create a Bitmap and a Canvas and call view.draw(canvas);

这里是code:

public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    v.draw(c);
    return b;
}

如果视图不显示之前它的大小将是零。它可以测量它是这样的:

if the view wasn't displayed before the size of it will be zero. Its possible to measure it like this:

if (v.getMeasuredHeight() <= 0) {
    v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    v.draw(c);
    return b;
}

编辑:根据这个帖子传递 WRAP_CONTENT 的值 makeMeasureSpec()并没有什么好处的(虽然对于一些视图类它的工作),以及推荐的方法是:

according to this post, Passing WRAP_CONTENT as value to makeMeasureSpec() doesn't to do any good (although for some view classes it does work), and the recommended method is:

// Either this
int specWidth = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.AT_MOST);
// Or this
int specWidth = MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED);
view.measure(specWidth, specWidth);
int questionWidth = view.getMeasuredWidth();

这篇关于转换期没有在Android中显示它为位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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