2015-12-11 78 views
0

我需要在我的應用程序中獲取視圖的快照。我在RelativeLayout中有幾個不同的東西,包括谷歌地圖片段,3個進度條帶文本。進度條顯示正常,但是當我將視圖轉換爲位圖時,Google地圖是黑色的。 我不想使用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

我發現了一個解決方案,是很容易...

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