将视图转换为位图而不在 Android 中显示它? [英] Converting a view to Bitmap without displaying it in Android?

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

问题描述

我会尽量解释我到底需要做什么.

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

我有 3 个独立的屏幕,比如 A、B、C.还有一个叫做 say HomeScreen 的屏幕,所有 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.

通过仅将所有代码放置在 HomeScreen Activity 中,我已经能够获取所有 3 个屏幕的位图并将其显示在图库视图中.现在,这使代码变得很复杂,我想简化它.

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.

那么,我可以从 HomeScreen 调用另一个 Activity 并且不显示它而只获取该屏幕的位图.例如,假设我只调用 HomeScreen 并且它调用了 Activity A、B、C,并且没有显示 A、B、C 中的任何活动.它只是通过 getDrawingCache() 给出该屏幕的位图.然后我们可以在 HomeScreen 的 Gallery 视图中显示这些位图.

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(canvas);

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

代码如下:

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天全站免登陆