我会试着解释我到底需要做什么。
我有3个单独的屏幕,说A,B,C。还有一个叫做HomeScreen的屏幕,所有3个屏幕的位图都应该显示在图库视图中,用户可以选择他想要去的视图。
我已经能够获得所有3个屏幕的位图,并通过将所有的代码放在HomeScreen活动中显示在画廊视图中。现在,这使得代码变得非常复杂,我想要简化它。
那么,我是否可以从HomeScreen调用另一个活动,而不显示它,而只获取该屏幕的位图。例如,假设我只调用了HomeScreen,它调用了Activity A、B、C,但没有显示A、B、C中的任何活动。它只是通过getDrawingCache()给出了屏幕的位图。然后我们可以在HomeScreen的图库视图中显示这些位图。
我希望我已经很清楚地解释了这个问题。
如果这真的可行,请告诉我。
发布于 2012-03-07 13:04:46
以下是我的解决方案:
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;
}
享受:)
发布于 2012-09-14 19:57:25
尝尝这个,
/**
* Draw the view into a bitmap.
*/
public static 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;
}
发布于 2013-10-19 02:00:35
我知道这可能是一个陈旧的问题,但我在让这些解决方案中的任何一个为我工作时遇到了问题。具体地说,我发现如果视图在膨胀后进行了任何更改,这些更改将不会合并到渲染的位图中。
以下是最终适用于我的情况的方法。然而,有一个警告。在调用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
干杯,
Levi
https://stackoverflow.com/questions/2801116
复制相似问题