2013-10-31 174 views
2

我有一個自定義的平鋪視圖,直到最近基於SurfaceView。我現在已經改變它來擴展TextureView。屏幕截圖TextureView是使用DrawingCache黑色

基本上我得到一個大的圖像瓷磚位。

我需要捕獲屏幕的所有內容並將其保存爲位圖,該位圖已與所有視圖(包括SurfaceView)一起使用。但是,當我現在使用相同的方法時,TextureView的區域是黑色的。我已經讀過,這與HW加速有關。

有什麼方法捕捉紋理視圖的圖像? 以下是截屏方法。

public File takeScreenShot(View contentView) 
{ 
    File result = null; 

    String mPath = this.cacheDir + "/screenshot.png"; 
    File imageFile = new File(mPath); 
    if (imageFile.exists()) { 
     imageFile.delete(); 
     Log.i("ImageManager","Old screenshot image was deleted"); 
    } 

    // create bitmap screen capture 
    Bitmap bitmap; 
    View v1 = contentView; 
    v1.setDrawingCacheEnabled(true); 
    v1.setDrawingCacheBackgroundColor(Color.WHITE); 
    bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
    v1.setDrawingCacheEnabled(false); 

    OutputStream outputStream = null; 

    try { 
     outputStream = new FileOutputStream(imageFile); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream); 
     outputStream.flush(); 
     outputStream.close(); 
     result = imageFile; 

    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return result; 
} 

編輯:新嘗試 獲取所有TextureViews並調用getBitmap。這是有效的,但是將它們放在較大的位圖頂部看起來與來自TilingView self或getLocation OnScreen()或getLocationInWindow()的座標不正確。

public List<TilingTextureView> getAllTextureViews(View view) 
{ 
    List<TilingTextureView> tilingViews = new ArrayList<TilingTextureView>(); 
    if (view instanceof TilingTextureView) { 
     tilingViews.add((TilingTextureView)view); 
    } 
    else if(view instanceof ViewGroup) 
    { 
     ViewGroup viewGroup = (ViewGroup)view; 
     for (int i = 0; i < viewGroup.getChildCount(); i++) { 
      tilingViews.addAll(getAllTextureViews(viewGroup.getChildAt(i))); 
     } 
    } 
return tilingViews; 
} 

在獲取整個視圖的屏幕截圖後,該位將添加到takeScreenShot方法中。

List<TilingTextureView> tilingViews = getAllTextureViews(contentView); 
if (tilingViews.size() > 0) { 
    Canvas canvas = new Canvas(bitmap); 
    for (TilingTextureView tilingTextureView : tilingViews) { 
     Bitmap b = tilingTextureView.getBitmap(tilingTextureView.getWidth(), tilingTextureView.getHeight()); 
     int[] location = new int[2]; 
     tilingTextureView.getLocationInWindow(location); 
     int[] location2 = new int[2]; 
     tilingTextureView.getLocationOnScreen(location2); 
     canvas.drawBitmap(b, location[0], location[1], null); 
    } 
} 
+0

這實際上工作。我只需根據父視圖相對於屏幕位置來修正圖像。 – MHolst

+4

如果你解決了你的問題,在回答中發佈解決方案並接受它。 –

回答

0

有些事情需要注意:

Bitmap textureViewBitmap = textureView.getBitmap(); 

View v1 = getWindow().getDecorView().getRootView(); 
v1.setDrawingCacheEnabled(true); 
v1.setDrawingCacheBackgroundColor(Color.TRANSPARENT); 
Bitmap viewBitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
viewBitmap.setHasAlpha(true); 
v1.setDrawingCacheEnabled(false); 

Canvas canvas = new Canvas(textureViewBitmap); 
canvas.drawBitmap(viewBitmap, 0, 0, paint); 

FileOutputStream outputStream = new FileOutputStream(imageFileDest); 
int quality = 100; 
cameraBitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 
outputStream.flush(); 
outputStream.close(); 

假設textureView是全屏,只有一個。如果您希望覆蓋視圖覆蓋TextureView,viewBitmap.setHasAlpha(true);非常重要,否則組合位圖在「textureView零件」上仍爲黑色。