将视图转换为位图而不显示在Android?

我会尽力解释我到底需要做什么。

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

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

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

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

请让我知道这是否可能。

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

编辑:根据这篇文章 , 传递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(); 

这里是我的解决方案:

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

请享用 :)

尝试这个,

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

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

这是最后为我的案件工作的方法。 有一个警告,但是。 在调用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

干杯,

列维

希望这可以帮助

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