我需要在我的应用程序中获取视图的快照。我在RelativeLayout中有一些不同的东西,包括谷歌地图片段,3个带文字的进度条。进度条显示正常,但是当我将视图转换为位图时,谷歌地图是黑色的。 我不想使用mMap.snapshot(),因为我将必须合并位图,并在我们在地图上绘制路线之后在其上添加绘图。有人有可行的解决方案吗?这是我正在使用的代码。
private Bitmap takeScreenshot() {
Date now = new Date();
RelativeLayout v = (RelativeLayout)findViewById(R.id.screenie_layout);
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
Bitmap bitmap1 = null;
try {
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());
}
bitmap1 = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
return bitmap1;
}
答案 0 :(得分:0)
我找到了一个非常简单的解决方案......
private Bitmap takeScreenshot() {
Date now = new Date();
RelativeLayout v = (RelativeLayout)findViewById(R.id.screenie_layout);
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
Bitmap bitmap1 = null;
try {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
}
bitmap1 = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
return overlay(bitmap1, mMapBitmap);
}
//此方法将地图和另一个位图组合在一起。
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
}