2010-05-10 83 views
96

我会尽力解释我到底需要做什么。将视图转换为位图而不显示在Android中?

我有3个单独的屏幕说A,B,C。还有另一个叫做HomeScreen的屏幕,其中所有3个屏幕位图都应该显示在图库视图中,并且用户可以选择他想要去哪个视图。

我已经能够获得所有3个屏幕的位图,并通过将所有代码放置在HomeScreen活动中,将其显示在图库视图中。现在,这已经使代码复杂了许多,我想简化它。

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

我希望我已经非常清楚地解释了这个问题。

请让我知道这是否可能。

+4

其实,我能做这个。 – sunil 2011-03-23 11:38:27

+1

我不完全确定,但我认为你将无法做到这一点。问题在于活动是要显示给用户的。您可以启动该活动,然后立即隐藏该活动,但该活动对于用户而言仍然可以在瞬间看到。它显示足够长的时间才能被注意,因此屏幕闪烁几次使得应用看起来不专业。但是,可能有一条命令启动一个活动而不显示它;我只是不知道它是否存在。 – 2010-05-10 09:45:16

+0

你好,如果你得到解决方案,那么你能帮助我吗? – Kalpesh 2012-07-10 12:24:49

回答

157

有一种方法可以做到这一点。你必须创建一个Bitmap和一个Canvas并调用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 (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; 
} 

编辑:根据this post传递WRAP_CONTENT作为价值makeMeasureSpec()并没有做任何好的(虽然一些视图类它的工作),以及推荐方法是:

// 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(); 
+0

我试过这个,但我所得到的只是一个半透明的黑盒子。我需要在视图上做些什么来准备好进行位图绘制吗? – Bobbake4 2011-10-14 19:02:50

+4

我实际上不得不将它改成'v.layout(v.getLeft(),v.getTop(),v.getRight(),v.getBottom());'为了让它正常工作,但是感谢代码:) – triad 2012-08-17 19:06:17

+2

我不得不使用v.getWidth()而不是v.getLayoutParams()。width和height。否则,现在工作。 – 2012-09-03 05:55:58

19

这里是我的解决方案:

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

享受:)

+0

+1谢谢你的回答 – 2012-08-01 05:45:25

+0

谢谢。如果身高超过某个值,我在某些设备上遇到问题。我还没有完全测试,但这似乎解决了这个问题。 – 2013-11-13 00:58:20

19

试试这个,

/** 
     * Draw the view into a bitmap. 
     */ 
     private Bitmap getViewBitmap(View v) { 
      v.clearFocus(); 
      v.setPressed(false); 

      boolean willNotCache = v.willNotCacheDrawing(); 
      v.setWillNotCacheDrawing(false); 

      // Reset the drawing cache background color to fully transparent 
      // for the duration of this operation 
      int color = v.getDrawingCacheBackgroundColor(); 
      v.setDrawingCacheBackgroundColor(0); 

      if (color != 0) { 
       v.destroyDrawingCache(); 
      } 
      v.buildDrawingCache(); 
      Bitmap cacheBitmap = v.getDrawingCache(); 
      if (cacheBitmap == null) { 
       Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); 
       return null; 
      } 

      Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); 

      // Restore the view 
      v.destroyDrawingCache(); 
      v.setWillNotCacheDrawing(willNotCache); 
      v.setDrawingCacheBackgroundColor(color); 

      return bitmap; 
     } 
+0

这个开箱即用,谢谢! – muetzenflo 2013-07-04 11:44:44

+0

@muetzenflo欢迎伙伴! – 2013-07-04 11:58:19

+0

如何从我的主要活动课程中使用它? – Si8 2014-02-06 01:19:20

10

我知道这可能是一个陈旧的问题,但我得到任何这些解决方案为我工作遇到的问题。 具体而言,我发现如果对视图进行了充气后对视图进行了任何更改,则这些更改将不会合并到呈现的位图中。

这是最后为我的案件工作的方法。但有一点需要注意。在致电getViewBitmap(View)之前,我夸大了我的观点,并要求它以已知尺寸进行布局。这是需要的,因为我的视图布局会在内容放置到内部之前使其高度/宽度为零。

View view = LayoutInflater.from(context).inflate(layoutID, null); 
//Do some stuff to the view, like add an ImageView, etc. 
view.layout(0, 0, width, height); 

Bitmap getViewBitmap(View view) 
{ 
    //Get the dimensions of the view so we can re-layout the view at its current size 
    //and create a bitmap of the same size 
    int width = view.getWidth(); 
    int height = view.getHeight(); 

    int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); 
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); 

    //Cause the view to re-layout 
    view.measure(measuredWidth, measuredHeight); 
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

    //Create a bitmap backed Canvas to draw the view into 
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b); 

    //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas 
    view.draw(c); 

    return b; 
} 

“神奇武器”,我发现这里: https://groups.google.com/forum/#!topic/android-developers/BxIBAOeTA1Q

干杯,

列维

1

希望这有助于

View view="some view instance";   
view.setDrawingCacheEnabled(true); 
Bitmap bitmap=view.getDrawingCache(); 
view.setDrawingCacheEnabled(false); 
相关问题